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
JavaScript
x
8
1
chrome.tabs.onActivated.addListener(tab => {
2
chrome.tabs.get(tab.tabId, current_tab_info => {
3
if (/^https://www.test.ca/.test(current_tab_info.url)) {
4
chrome.tabs.executeScript(null, {file: './foreground.js'}, () => console.log('I injected'))
5
}
6
})
7
});
8
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:
JavaScript
1
7
1
"content_scripts": [
2
{
3
"matches": ["<all_urls>"],
4
"js": ["foreground.js"]
5
}
6
]
7
Instead of:
JavaScript
1
10
10
1
"permissions": [
2
"tabs",
3
"https://www.test.ca/*",
4
"webNavigation", "*://*/*"
5
]
6
7
"background": {
8
"scripts": ["foreground.js"]
9
}
10