我目前的Chrome扩展程序始终在响应时延迟。在它工作之前我总是要打开两次。为什么会这样呢?
后台脚本:
var toggle = false;
chrome.browserAction.onClicked.addListener(function(tab) {
toggle = !toggle;
if(toggle){
chrome.browserAction.setIcon({path: "turnon.png", tabId:tab.id});
chrome.tabs.executeScript(tab.id, {file:"on.js"});
}
else{
chrome.browserAction.setIcon({path: "turnoff.png", tabId:tab.id});
chrome.tabs.executeScript(tab.id, {file:"off.js"});
}
});
注入的HTML代码:
var jqlib = document.createElement('script');
jqlib.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
jqlib.id= 'jqlibjs';
document.getElementsByTagName('head')[0].appendChild(jqlib);
var jqlib2 = document.createElement('script');
jqlib2.type = 'text/javascript';
jqlib2.id = 'jqlib2';
document.head.appendChild(jqlib2);
document.getElementById('jqlib2').innerHTML = "$('body').on('click', function(){$(this).hide();});";