背景.js:
chrome.app.runtime.onLaunched.addListener(function() {
console.log('launched');
chrome.app.window.create('index.html', {
innerBounds: {
width: 800,
height: 600,
minWidth: 200,
minHeight: 200,
}
});
})
chrome.app.window.onClosed.addListener(function() {
console.log('close bg');
});
mainfest.json:
{
"name": "xxx",
"description": "xxx",
"version": "3.4.0",
"manifest_version": 2,
"icons": {
"16": "16.png",
"48": "48.png",
"128": "128.png"
},
"permissions": [
{"socket": [
"tcp-listen:*:*",
"tcp-connect",
"resolve-host",
"udp-bind:*:*",
"udp-send-to:*:*"
]}
],
"app": {
"background": {
"scripts": ["background.js"]
}
}
}
我试图在用户使用红色 X 时检测 Chrome 应用程序窗口的关闭。但是,“close bg”控制台日志从未出现,因此我认为它不会触发。
我错过了什么?
我已更新我的 .json
以包含持久的后台页面。我想也许 bg 页面关闭得太快以至于事件无法触发。
{
"name": "xxx",
"description": "xxx": "3.4.0",
"manifest_version": 2,
"icons": {
"16": "16.png",
"48": "48.png",
"128": "128.png"
},
"permissions": [
{"socket": [
"tcp-listen:*:*",
"tcp-connect",
"resolve-host",
"udp-bind:*:*",
"udp-send-to:*:*"
]}
],
"app": {
"background": {
"page": "background.html",
"persistent": true
}
}
}
我已经添加了以下用户的“获取”想法:
chrome.app.runtime.onLaunched.addListener(function() {
console.log('launched');
chrome.app.window.create('index.html', {
id:'cci',
innerBounds: {
width: 800,
height: 600,
minWidth: 200,
minHeight: 200,
}
});
})
chrome.app.window.get('cci').onClosed.addListener(function() {
console.log('close bg');
});
但是,在启动时我收到此错误。我认为 ID 的分配速度不够快,因此“cci”无效。
最佳答案
您需要将监听器附加到特定窗 Eloquent 能使其工作,而不是附加到 chrome.app.window 全局对象。 chrome.app.window.create() 函数中的第三个参数允许您指定接收新创建的窗口对象作为参数的回调。
chrome.app.runtime.onLaunched.addListener(function() {
console.log('launched');
chrome.app.window.create('index.html', {
innerBounds: {
width: 800,
height: 600,
minWidth: 200,
minHeight: 200,
}
}, function(window){
window.onClosed.addListener(function() {
console.log('close bg');
});
});
})
关于javascript - 使用 X 关闭 Chrome 应用程序时 onClosed() 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35096060/