Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove injected script in chrome extension

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

like image 867
abhilash Avatar asked Nov 11 '22 01:11

abhilash


1 Answers

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);
} 
like image 128
doron aviguy Avatar answered Nov 14 '22 22:11

doron aviguy