@juraj.masiar Below is the kind of thing I am doing currently (with some stuff removed that is not relevant):
First I have a function that fires the inject code. This is itâs own function because it gets triggers by a listener that triggers when the option is changed to âInjectâ:
async function insertBootstrap(e) {
if(!window.insertBootstrapReg){
window.insertBootstrapReg = await browser.userScripts.register({
matches: ["<all_urls>"],
js: [{ code: 'link=document.createElement(\'script\'); link.src=\'https://' + host + '/' + localStorage.clientId + space + '/Bootstrap.js\';document.getElementsByTagName(\'head\')[0].appendChild(link);' }]
});
}else{
window.insertBootstrapReg.register({
matches: ["<all_urls>"],
js: [{ code: 'link=document.createElement(\'script\'); link.src=\'https://' + host + '/' + localStorage.clientId + space + '/Bootstrap.js\';document.getElementsByTagName(\'head\')[0].appendChild(link);' }]
});
}
}
Then I have this code to unregister when the optioin is changed or the tool is turned off:
if(window.insertBootstrapReg){
window.insertBootstrapReg.unregister();
}
This seems to work until you turn it back on after turning âInjectâ off. Then it no longers works, I get no console errors or anything like that. I suspect it is because the window variable is alreayd there. I did have this slightly different before but on every page load an extra script is added, so the new register on each event caused duplicates (that is why I added the window variable to stop that). But once it is unregistered I cannot reinstate it without getting duplicates.
Is there a way to almost âre-registerâ the initial script when it is re-toggled by the user or something similar?