residue
residue

Reputation: 207

Basic Firefox Addon - browser.browserAction.onClicked.addListener(...) and browser.tabs.executeScript(...) not working

My manifest file:

{
   "manifest_version": 2,
      "name": "myAddon",
      "version": "1.0",

      "description": "myAddon.",

      "icons": {
        "48": "icons/myAddon-48.png",
        "96": "icons/myAddon-96.png"
      },

      "content_scripts": [
        {
          "matches": ["https://mytestmatchwhichworkedfine.pl/*"],
          "js": ["jquery-3.3.1.min.js", "myAddon.js"]
        }
      ],

    "permissions": [
        "activeTab"
    ],

    "browser_action": {
        "default_icon": "icons/myAddon-32.png",
        "default_title": "myAddon"
    }
}

myAddon.js

browser.browserAction.onClicked.addListener((tab) => {
    browser.tabs.executeScript(tab.id, {
        code: `document.body.style.border = "5px solid red"`
    })
})

I want the script to be executed after I click the addon button. When I write document.body.style.border = "5px solid red" alone in the script - it does execute. What I did wrong?

Upvotes: 0

Views: 1370

Answers (1)

residue
residue

Reputation: 207

Browser Action click listener can be set only in background script. The provided code will still be executed on page (tab), because browser.tabs.executeScript(...) executes content script.

  "content_scripts": [
    {
      "matches": ["https://mytestmatchwhichworkedfine.pl/*"],
      "js": ["jquery-3.3.1.min.js"]
    }
  ],

  "background": {
    "scripts": ["myAddon.js"]
  },

Upvotes: 2

Related Questions