我正在尝试编写一个Chrome扩展来检测进程崩溃。
首先,我进入了Chrome的about:flags页面,开启了“实验性扩展API”。
这是我写的扩展:
manifest.json
{
"manifest_version": 2,
"name": "CrashDetect",
"description": "Detects crashes in processes.",
"version": "1.0",
"permissions": [
"experimental","tabs"
],
"background": {
"scripts": ["background.js"]
}
}backround.js
chrome.experimental.processes.onExited.addListener(function(integer processId, integer exitType, integerexitCode) {
chrome.tabs.getCurrent(function(Tab tab) {
chrome.tabs.update(tab.id, {url:"http:\\127.0.0.1\""});
};)
});然后我访问了Chrome的about://crash页面。但onExited侦听器不执行。我在manifest.json或background.js中做错了什么吗?
发布于 2013-04-28 11:54:08
您的代码中有几个错误。首先,在函数声明中具有参数类型,将其更改为:
function(processId, exitType, integerexitCode){其次,您将});放入};)。尝试使用inspecting the background page查看语法错误。
好吧,由于我不熟悉这个特殊的API,在尝试了一些之后,我发现如果我没有包含一个onUpdated的处理程序,那么所有的事件都不会触发。我真的很怀疑这是否是预期的行为,我会检查是否有关于它的bug报告。现在,只需这样做就可以让它正常工作:
chrome.experimental.processes.onUpdated.addListener(function(process){});
chrome.experimental.processes.onExited.addListener(function(processId, exitType, integerexitCode){
chrome.tabs.query({active:true, currentWindow:true},function(tabs){
chrome.tabs.update(tabs[0].id, {url:"http:\\127.0.0.1"});
});
});请注意,我确实用chrome.tabs.query替换了您的getCurrent,因为前者会给您一个错误。这确实会导致这样的行为:如果您关闭一个选项卡,则下一个选项卡将被重定向。也许您可以尝试按exitType进行过滤,而不包括正常的出口。
https://stackoverflow.com/questions/16259383
复制相似问题