Skip to content
Advertisement

Automaticly loading script from Chrome Extension for a specyfic website

First of all, js is not my cup of tea.

I’ve create a liltle chrome extension that works well, but for the moment, the script only execute when I change tab and come back on the previous tab.

Here’s the code for the background.js

    chrome.tabs.onActivated.addListener(tab => {
    chrome.tabs.get(tab.tabId, current_tab_info => {
        if (/^https://www.test.ca/.test(current_tab_info.url)) {
            chrome.tabs.executeScript(null, {file: './foreground.js'}, () => console.log('I injected'))
        }
    })
});

So is there a way that when I click on the website, the script automaticly runs instead of switching to another tab then come back on the previous tab?

I’ve made some research, but I don’t find any anwser. Thank you!

Advertisement

Answer

FOUND IT!

So I just modify the manifest for:

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

Instead of:

"permissions": [
        "tabs",
        "https://www.test.ca/*",
        "webNavigation", "*://*/*" 
    ]

"background": {
        "scripts": ["foreground.js"]
    }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement