so what I am trying to do is that on one tab I want to press a button on my Chrome extension which will open a new tab and I want some script to be executed in that new tab. BUT ONLY WHEN I PRESS THE BUTTON ON THE EXTENSION.
here is my manifest.json
{
"manifest_version": 2,
"name": "extension",
"description": "extension",
"version": "1.0",
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["https://*.somewebsite.com/*"],
"js": ["myScript.js"]
}
],
"permissions": ["tabs", "<all_urls>", "http://*/",
"https://*/"]
}
popup.html
<!doctype html>
<html>
<head><title>Extension</title></head>
<body>
<h2>My Extension</h2>
<button id="myButton">Press Me!</button>
<script src="popup.js"></script>
</body>
</html>
popup.js (this opens a new tab from the current tab by executing "openTab.js")
function injectTheScript() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {file: "openTab.js"});
});
}
document.getElementById('myButton').addEventListener('click', injectTheScript)
background.js
chrome.runtime.onInstalled.addListener(() => {
console.log('extension is on!');
});
Now this all works but the thing is the myScript.js is executed all the time on all new tabs because of being declared in manifest.json. However, I want it to be executed on new tabs ONLY when I press the "Press Me!" button.