什么导致我的Chrome扩展的background.js冻结,而不是回复消息 [英] What is causing my chrome extension's background.js to freeze up and not respond to messages

查看:170
本文介绍了什么导致我的Chrome扩展的background.js冻结,而不是回复消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当偶尔我的Chrome扩展的 background.js 页面冻结,我不知道是什么导致它。



background.js 文件已冻结,它不再响应来自内容脚本的消息,并且当我尝试通过扩展管理器打开后台页面以检查它时,窗口弹出,但它保持空白,并且没有界面出现。



后台页面中唯一需要做的事情是传递消息并检索本地存储变量。



我无法弄清楚是什么导致了这个问题,这个错误似乎只发生在我从 chrome.extension api转换到新的 chrome.runtime api后发生。



任何人都可以告诉我这里出了什么问题吗?或帮我弄明白?谢谢!



继承 background.js 文件的完整代码

  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;


$ b 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({}); //发送空响应
});

是否有可能发生冻结页面的死锁情况?它不会导致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},
图标:{
128:ET-128x128.png,
48:ET-48x48.png
},
键:xxxxxxxxxxxxxxxxxxxx,
名称:扩展测试,
short_名称:ET,
description:ET这个和那个,但是没有电话回家,
version:999,
homepage_url:http ://www.etphonehome.com

只有禁用并重新启用扩展程序让它再次开始工作,一旦背景页面被冻结

以下是冻结的后台页面检查窗口的屏幕截图:

解决方案

localStorage API存在问题,因为在chrome中它是一个同步API,用于一个固有的异步操作(由renderer进程调用,然后必须与浏览器进程进行通信,读取/写入支持存储文件系统并可能回复到渲染器进程)。虽然从理论上说不应该导致网页或扩展代码出现死锁,但可能在chrome的实现中存在错误。

您可能会尝试的一件事是从localStorage切换到 chrome.storage.local 。由于它具有异步API,因此它的使用稍微复杂一点,但不会遭受与localStorage相同的实现复杂性。

例如

  sendResponse({data:localStorage [request.key ]}); 

变为

  chrome.storage.local.get(request.key,function(storageResult){
sendResponse(storageResult [request.key]);
});


Every once in a while my chrome extension's background.js page freezes, i have no idea what is causing it.

When the background.js file has frozen, it no longer responds to messages from the content script, and when I try to open the background page via the extensions manager to inspect it, the window pops up but it stays blank, and no interface appears.

The only things im doing in the background page are message passing and retrieving localstorage variables.

I cant figure out what is causing this, the bug only seems to have happened since i transitioned to the new chrome.runtime api, from the chrome.extension api

Can anyone tell me what is going wrong here? or help me figure it out? Thanks!

Heres the background.js file's code in its entirety

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

Is it possible a deadlock situation is occurring that is freezing the page? It doesnt cause the CPU to go mad, so im guessing its not an endless loop.

Update here is the manifest.json as requested

{
   "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"
}

Only disabling and re-enabling the extension get it to start working again, once the background page has frozen

Below is a screenshot of the frozen background page inspection window:

解决方案

The localStorage API is problematic because in chrome it is a synchronous API to an inherently asynchronous operation (called from a renderer process, which must then communicate with the browser process that reads from / writes to a backing store in the filesystem and possibly replies back to the renderer process). While it should not in theory be possible to cause deadlocks from webpage or extension code, it's possible there are bugs in chrome's implementation.

One thing you might try is switching from localStorage to chrome.storage.local. It is a little more work to use since it has an asynchronous API, but does not suffer from the same implementation complexity as localStorage.

E.g.

sendResponse({data: localStorage[request.key]});

becomes

chrome.storage.local.get(request.key, function(storageResult) {
  sendResponse(storageResult[request.key]);
});

这篇关于什么导致我的Chrome扩展的background.js冻结,而不是回复消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆