Chrome is not defined

Andrew McKay amckay at mozilla.com
Mon Oct 30 21:42:20 UTC 2017


In Firefox both are available:

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Chrome_incompatibilities#Firefox_supports_both_chrome_and_browser_namespaces

On 30 October 2017 at 14:39, Michael Buckley <support at codefisher.org> wrote:
> In Firefox the global variable to access the API is called browser, not
> chrome.  So you either need to change things like chrome.runtime to
> browser.runtime or add something like this to the top of the script.
>
> if(type(chrome) == 'undefined') {
>     var chrome = browser;
> }
>
> God Bless
> Michael
>
>
>
> On 28/10/17 5:39 am, Daniel Travelstead wrote:
>>
>> Hi,
>>
>> My name is Daniel and I am working on porting over a Chrome extension to
>> Firefox however with the temporary add on installed in Firefox, I keep
>> getting ReferenceError: chrome is not defined. I am trying to send a message
>> from my content script to my background.js file in the extension. Everything
>> works perfectly in Chrome but that message that I’m trying to send allows
>> the extension to know if I’m logged into the website and am verified to use
>> the our API. Because of this, due to other code that processes, the
>> extension pops up, it’s just can’t log me in. Is there any reason you all
>> can think of why chrome would be undefined in Firefox if it should be
>> compatible with Chrome? I tried browser.runtime as well and get the same
>> issue “ReferenceError: browser is not defined. Any help would be greatly
>> appreciated.
>>
>> Thank you,
>> Daniel
>>
>>
>> If it helps here is my background.js
>>
>> chrome.runtime.onMessageExternal.addListener(
>>      function(request, sender, sendResponse) {
>>          if (request) {
>>              if (request.message) {
>>                  if (request.message == "version") {
>>                      sendResponse({version: 1.0});
>>                  }
>>              }
>>              if (request.token == "not added") {
>>                  window.token_string = "";
>>                  localStorage.setItem("giftibly_token", "");
>>              }
>>              else{
>>                  window.token_string = request.token;
>>                  localStorage.setItem("giftibly_token", token_string);
>>              }
>>          }
>>          return true;
>>      }
>> );
>> var isChromium = window.chrome,
>>      winNav = window.navigator,
>>      vendorName = winNav.vendor;
>> if (isChromium !== null && typeof isChromium !== "undefined" && vendorName
>> === "Google Inc.") { //if Chrome
>>       chrome.extension.onConnect.addListener(function(port) {
>>            port.onMessage.addListener(function(msg) {
>>                 console.log(msg);
>>                 if(msg == "token not added"){
>>                     port.postMessage(token_string);
>>                 }
>>            });
>>       });
>> }
>> else{
>>       browser.runtime.onConnect.addListener(function(port) { //if not
>> Chrome
>>            port.onMessage.addListener(function(msg) {
>>                 console.log(msg);
>>                 if(msg == "token not added"){
>>                     port.postMessage(token_string);
>>                 }
>>            });
>>       });
>> }
>>
>>
>>
>> And my content script. The turbolinks:load function is due to my project
>> using Ruby on Rails.
>>
>>      <script>
>>
>>          $(document).on('turbolinks:load', function() {
>>              var isChromium = window.chrome,
>>                  winNav = window.navigator,
>>                  vendorName = winNav.vendor;
>> //            if (isChromium !== null && typeof isChromium !== "undefined"
>> && vendorName === "Google Inc.") {  //detect if Chrome is the Browser
>>              var editorExtensionId = "<%=
>> Rails.application.config.editor_extension_id %>";
>>              console.log(editorExtensionId);
>>              <% if logged_in? %>
>>                  console.log(chrome);
>>                  var hasExtension = false;
>>                  var token_string = '<%= current_customer.auth_token %>';
>>                  console.log(token_string);
>>                  chrome.runtime.sendMessage(editorExtensionId, { message:
>> "version",  token: token_string},
>>                      function (reply) {
>>                          if (reply) {
>>                              if (reply.version >= "1.0") {
>>                                  hasExtension = true;
>>                                  $('#gift_assistant_link').remove();
>>                                  localStorage.setItem("giftibly_token",
>> "added");
>>                              }
>>                          }
>>                          else {
>>                            hasExtension = false;
>>                          }
>>                      }
>>                  );
>>                   <% else %>  // if not logged in
>>                      var token_string = 'not added';
>>                      chrome.runtime.sendMessage(editorExtensionId, {token:
>> token_string});
>>                      localStorage.setItem("giftibly_token", "");
>>              <% end %>
>> //            }
>>
>>          });
>>      </script>
>> _______________________________________________
>> Dev-addons mailing list
>> Dev-addons at mozilla.org
>> https://mail.mozilla.org/listinfo/dev-addons
>
>
> _______________________________________________
> Dev-addons mailing list
> Dev-addons at mozilla.org
> https://mail.mozilla.org/listinfo/dev-addons


More information about the Dev-addons mailing list