Javascript 是什么导致我的chrome扩展';s background.js冻结,不响应消息
每隔一段时间,我的chrome扩展名的background.js页面就会冻结,我不知道是什么原因造成的 当background.js文件冻结时,它不再响应来自内容脚本的消息,当我试图通过extensions manager打开后台页面检查它时,窗口弹出,但它保持空白,并且没有界面出现 我在后台页面中所做的唯一事情就是传递消息和检索本地存储变量 我不知道是什么原因导致了这种情况,这个错误似乎只是在我从chrome.extensionapi转换到新的chrome.runtimeapi之后发生的 有人能告诉我这里出了什么问题吗?还是帮我弄明白?谢谢 下面是background.js文件的完整代码Javascript 是什么导致我的chrome扩展';s background.js冻结,不响应消息,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,每隔一段时间,我的chrome扩展名的background.js页面就会冻结,我不知道是什么原因造成的 当background.js文件冻结时,它不再响应来自内容脚本的消息,当我试图通过extensions manager打开后台页面检查它时,窗口弹出,但它保持空白,并且没有界面出现 我在后台页面中所做的唯一事情就是传递消息和检索本地存储变量 我不知道是什么原因导致了这种情况,这个错误似乎只是在我从chrome.extensionapi转换到新的chrome.runtimeapi之后发生的 有人
if (!chrome.runtime) {
// Chrome 20-21
chrome.runtime = chrome.extension;
} else if(!chrome.runtime.onMessage) {
// Chrome 22-25
chrome.runtime.onMessage = chrome.extension.onMessage;
chrome.runtime.sendMessage = chrome.extension.sendMessage;
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getLocalStorage")
sendResponse({data: localStorage[request.key]}); // decodeURIComponent
else if (request.method == "setLocalStorage")
sendResponse({data: localStorage[request.key]=request.value});
else
sendResponse({}); // send empty response
});
是否有可能出现死锁情况,导致页面冻结?它不会导致CPU发疯,所以我猜这不是一个无止境的循环
更新
这是所请求的manifest.json
{
"manifest_version": 2,
"content_scripts": [ {
"exclude_globs": [ "http://*.facebook.com/ajax/*", "https://*.facebook.com/ajax/*" , "http://www.facebook.com/ai.php?*", "https://www.facebook.com/ai.php?*", "http://www.facebook.com/ajax/*", "https://www.facebook.com/ajax/*"],
"include_globs": [ "http://*.facebook.com/*", "https://*.facebook.com/*" ],
"js": [ "script.js" ],
"matches": [ "http://*.facebook.com/*", "https://*.facebook.com/*" ],
"run_at": "document_start"
} ],
"converted_from_user_script": true,
"background": {"scripts": ["background.js"],
"persistent": false},
"icons": {
"128": "ET-128x128.png",
"48": "ET-48x48.png"
},
"key": "xxxxxxxxxxxxxxxxxxxx",
"name": "Extension Test",
"short_name": "ET",
"description": "ET Does this and that, but doesnt phone home",
"version": "999",
"homepage_url": "http://www.etphonehome.com"
}
只有禁用并重新启用扩展才能使其在后台页面冻结后重新开始工作
下面是冻结背景页检查窗口的屏幕截图:
本地存储API有问题,因为在chrome中,它是一个同步API,用于固有的异步操作(从渲染器进程调用,然后必须与浏览器进程通信,该进程从文件系统中的备份存储读取/写入,并可能回复到渲染器进程)。虽然理论上不可能导致网页或扩展代码死锁,但chrome的实现中可能存在漏洞 您可以尝试的一件事是从本地存储切换到本地存储。由于它有一个异步API,但与localStorage的实现复杂性不同,因此使用它的工作量要多一点 例如 变成
chrome.storage.local.get(request.key, function(storageResult) {
sendResponse(storageResult[request.key]);
});
您使用的是持久性后台页面还是事件页面?是否有可能卸载?能否显示manifest.json?这里解释了事件页面:我使用的是非持久性背景页面,但我切换到了持久性背景页面,并注意到错误仍然偶尔发生。我已将清单文件的内容添加到OP中,它显示“持久”:false。但我也尝试了“持久性”:没错,但有时仍然会出错。你是使用长期连接还是这里描述的简单一次性请求?它使用一次性请求而不是长期连接谢谢你的信息和建议,我会尝试一下,干杯。我也有同样的问题,安东尼·萨金特的建议行得通吗?还没试过,现在问题可能已经解决了,也许他们在chrome中修复了一些东西。我不能肯定。
chrome.storage.local.get(request.key, function(storageResult) {
sendResponse(storageResult[request.key]);
});