6

While migrating my old Firefox add-on to WebExtension API, failed to understand while I am getting this error:

TypeError: browser.browserAction is undefined

Here is the manifest.json:

{

  "manifest_version": 2,
  "name": "My Login",
  "version": "3.0",

  "description": "Login to my page",
  "homepage_url": "https://localhost",
  "icons": {
    "48": "icons/button-1.png"
  },

  "permissions": [
    "activeTab", "storage"
  ],

  "browser_action": {
    "default_icon": "icons/button-1.png",
    "default_title": "Login"
  },


  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["index.js"]
    }
  ],

  "options_ui": {
    "page": "options.html"
  }
    }

Here is the index.js:

function handleClick() {


    var loginUserName, loginPassword;

    var URL = window.content.location.href;

    var doc = window.content.document;


}

browser.browserAction.onClicked.addListener(handleClick);

So I am getting TypeError: browser.browserAction is undefined in the browser console when I go to about:debugging and load my add-on as a temporary addon.

Options.html, options.js and button-1.png do exist - I just don't place them here.

Observed in Firefox 55.0.3 (32-bit). Any idea why this error happens?

Thanks, Racoon

Racoon
  • 951
  • 3
  • 14
  • 32
  • 3
    As this question is unfortunately marked as a duplicate I can't add another answer to this. I had a similar problem due to the fact that I simply forgot to define a `browser_action` in my manifest.json. See here: https://stackoverflow.com/a/54494884/594832 – Jan Köhler Feb 02 '19 at 16:16

1 Answers1

9

browser.browserAction should be called in a background script and not in a content script like you're doing. So assuming this code is in background.js:

function handleClick() {
    console.log("do something.");
    // If you want to something with the content, you will need a content script and messaging
}

browser.browserAction.onClicked.addListener(handleClick);

You add the background_scripts key in manifest.json:

{

  "manifest_version": 2,
  "name": "My Login",
  "version": "3.0",

  "description": "Login to my page",
  "homepage_url": "https://localhost",
  "icons": {
    "48": "icons/button-1.png"
  },
  "background": {
    "scripts": ["background.js"]
  }
}
Smile4ever
  • 3,491
  • 2
  • 26
  • 34
  • Thanks, it seems to work, but I was going to use a content script to change the value of some web elements. Something like doc.getElementById("loginUserName").value = loginUserName; Can I do this with a background script, or I need to use a combination of a background and content scripts? – Racoon Sep 07 '17 at 10:31
  • 1
    It looks like I do need a content script in addition to the background script: https://stackoverflow.com/questions/46098697/modifying-a-web-page-from-firefox-add-on-webextension-api-how-to-access-the-e/46098867#46098867 – Racoon Sep 07 '17 at 14:30
  • 1
    Yes, you will need both a background script (for most WebExtension APIs) and a content script to alter page contents. – Smile4ever Sep 07 '17 at 18:17