注意使用chrome.tabs.onUpdated.addListener需要在manifest.json声明tabs权限
permissions: [‘tabs’]
并且需要重新加载插件,如重新加载还是无法生效,请移除插件重新导入
/** * 监听tab页面变化(用于处理页面logo问题) */ chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){ if( changeInfo.url == undefined){return false;} // 检查是否是wish页面的tab if(tab.url.indexOf(\'pinterest.com\') > 0){ // 通知对应的tab页面url变化了,需要优化为离开立即移除,进入则加载完毕再添加 if (tab.status === \'loading\'){ chrome.tabs.sendMessage(tabId,{type:\'tabUpdate\', tab:tab}, function(response) { console.log(\'来自content的回复:\'+response); }); } } });
script_content接收
/** * 接收后台发给content-script的消息 * */ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if(request.type == \'tabUpdate\'){ // GhandleInject_tabUpdate 自定义的一个函数,如果有定义这个函数则执行该函数,如果没有则不处理 typeof(GhandleInject_tabUpdate) == \'function\' && GhandleInject_tabUpdate(); } // sendResponse(\'我收到了你的消息!\'); });