chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(
null, {file : "app.js"});
});
I am injecting a code like this to the extension on click. But I want to remove this injected code when the user clicks the extension Icon second time. How is it possible. The code Injects an html div with id "pluginHolder". I can manually remove it using the basic js code document.getElementById('pluginHolder').remove();
.
How to do this process dynamically?
Thanks in advance
I think the simplest solution something like
if(document.getElementById('pluginHolder')) {
document.getElementById('pluginHolder').remove()
} else {
var pluginHolder = document.createElement('div');
document.body.appendChild(pluginHolder);
}
If you want to affect js code and not DOM.
you can use play with eventListener
,
function logMouse() {
console.log(e.x+':'+e.y);
}
// Initialize on first run to true else use old value NOT(!) ( last time injected script)
isEnable = isEnable ? !isEnable : !!isEnable;
if(isEnable) {
window.addEventListener('mousemove', logMouse);
} else {
window.removeEventListener('mousemove', logMouse);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With