Reputation: 865
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
Upvotes: 5
Views: 5885
Reputation: 2944
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);
}
Upvotes: 1