我正在看这个网站:
尝试从我的扩展中注入一个基本的内容脚本,在document_start,它只是在听到DOMContentLoaded事件时登录到控制台。什么都没发生。我查看了很多网站,这通常不是问题。
清单:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"exclude_globs": ["*chrome-extension*", "*google*"],
"js": [ "scripts/page-event-listener.js"],
"run_at": "document_start"
},
],
页面事件监听器:
function SendRuntimeMessage(pageStatus) { chrome.runtime.sendMessage({pageStatus: pageStatus}, function() {} ); }
function SendBeforeUnloadMessage() { SendRuntimeMessage("complete"); document.removeEventListener('readystatechange', pageSniffer, false); }
var pageSniffer = function(evt){
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var screenSize = [width, height];
if (evt.target.readyState === "loading") { SendRuntimeMessage("loading"); }
else if (evt.target.readyState === "interactive") {
chrome.runtime.sendMessage( { pageStatus: "interactive", screenSize: JSON.stringify(screenSize) }, function() {} );
//setTimeout so we have a hook in cases where the page never hits the complete event, 5 secs to hit it
setTimeout(function(){
if (evt.target.readyState === "interactive") { // this stops the timer from firing even when we've reached the 5 seconds, if we've hit the complete event
SendRuntimeMessage("complete");
document.removeEventListener('readystatechange', pageSniffer, false);
window.removeEventListener("beforeunload", SendBeforeUnloadMessage);
}
}, 5000);
}
else if (evt.target.readyState === "complete") {
SendRuntimeMessage("complete");
window.removeEventListener("beforeunload", SendBeforeUnloadMessage);
}
};
document.addEventListener('readystatechange', pageSniffer, false);
//add an event listener in case the page is unloaded before the timeout or the complete event is hit
window.addEventListener("beforeunload", SendBeforeUnloadMessage);
我查看了网站的来源,我的脚本没有出现在内容脚本列表中。我还注意到该站点报告了服务工作者错误。
我的一般问题是:服务工作者是否应该干扰脚本注入沙箱的操作?有没有人注意到有关服务工作者的事情?这是Chrome错误还是我错过了什么?
汤姆