简体   繁体   English

删除chrome扩展中的注入脚本

[英]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". 代码注入一个id为“pluginHolder”的html div。 I can manually remove it using the basic js code document.getElementById('pluginHolder').remove(); 我可以使用基本的js代码document.getElementById('pluginHolder').remove();手动删除它document.getElementById('pluginHolder').remove(); .

How to do this process dynamically? 如何动态地完成这个过程?

Thanks in advance 提前致谢

i think the simpleset solution 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 js code and not DOM. 如果你想影响js代码而不是DOM。 you can use play with event listenrs , 你可以使用事件听众玩,

 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);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM