Remove injected script in chrome extension

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(
      null, {file : "app.js"});                                                                                                                                                                         
});

I embed code like this in the extension when clicked. But I want to delete this entered code when the user clicks on the extension icon a second time. How is this possible. Injects html div code with id "pluginHolder". I can manually delete it using the main js code document.getElementById('pluginHolder').remove();.

How to make this process dynamic?

Thanks in advance

+4
source share
1 answer

I think the solution is simpleet somthing like

if(document.getElementById('pluginHolder')) {
    document.getElementById('pluginHolder').remove()
} else {
   var pluginHolder = document.createElement('div');
   document.body.appendChild(pluginHolder);
}

if you want to affect the js code, not the DOM. you can use the game with listening to events,

 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);
} 
+1
source

All Articles