0

I want to click my extension icon in the browser and have "test" appear in the console for the web page/tab i currently have selected in the browser.

manifest.json:

{
    "name": "some name",
    "version": "1.0",
    "description": "some description",
    "manifest_version": 2,
    "permissions": ["storage", "tabs", "activeTab"],


    "browser_action": {
      "default_title": "hello!",
      "default_popup": "popup.html",
      "default_icon": "icon.png"
    },

    "background": {
      "scripts": ["background.js"],
      "persistent": true
    }

}

background.js:

chrome.browserAction.onClicked.addListener(function(){

chrome.tabs.executeScript(null, {
    code: "test"
})

I've used 'tabs' and 'activeTab' for permissions as i believe these are required for running this code.

If i click on the extension it doesn't show an error but it also doesn't show "test" in the console log for the web page i'm on.

Is there an obvious reason this isn't working from the code i've supplied?

Steve
  • 625
  • 2
  • 5
  • 17
  • Replace `"test"` with `"console.log('test')"` – wOxxOm Jun 22 '19 at 16:13
  • I stumbled upon this which is where the problem lay: https://stackoverflow.com/questions/18766886/unable-to-trigger-chrome-browseraction-onclicked-addlistener-with-google-chrome – Steve Jun 23 '19 at 09:07
  • Presumably it doesn't allow the onClicked event when there's a popup.html because the event can be placed inside the popup.js instead. – Steve Jun 23 '19 at 09:10

1 Answers1

0
chrome.browserAction.onClicked.addListener(function(){
  browser.tabs.executeScript({
    code: `console.log('test');`
  });
});

Source: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript

Umbro
  • 1,984
  • 12
  • 40
  • 99