how to make change to web page using chrome.tabs.executeScript()

196 Views Asked by At

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?

1

There are 1 best solutions below

1
Umbro On
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