jas*_*son 3 google-chrome-extension application-restart
我建立了一个Chrome扩展程序,该扩展程序会泄漏内存。我正在修复内存泄漏,但与此同时,一些朋友已经在使用它。
作为一种临时措施,我想提供一个补丁,该补丁会定期自动重启扩展程序。
该怎么做?即从扩展程序本身内部重启chrome扩展程序。
谢谢,
您可以有两个扩展名。并从另一个启动目标扩展。如果适合您,请继续阅读。
window.addEventListener('load', function (e) {
// create a button and add it to the page
var btn = document.createElement('button');
btn.innerHTML = 'Restart child extension';
btn.addEventListener('click', function (e) {
// on button click send message to the background script
chrome.extension.sendMessage({restart: true}, function (res) {
console.log(res);
});
}, false);
var body = document.querySelector('body');
body.appendChild(btn);
}, false);
Run Code Online (Sandbox Code Playgroud)
// first get your target (child) extension by it's name
var child = null;
chrome.management.getAll(function (info) {
for (var i=0; i < info.length; i++) {
if (info[i].name == 'Test child extension') {
child = info[i];
break;
}
}
});
function disable (cb) {
chrome.management.setEnabled(child.id, false, cb);
}
function enable (cb) {
chrome.management.setEnabled(child.id, true, cb);
}
function afterEnable () {
// notify the content script
resRestart({restarted: true});
}
var resRestart = null;
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request);
// if we receive request with restart variable, save a reference to the
// sendResponse function and disable the child extension
switch (true) {
case request.restart: resRestart = sendResponse; disable(); break;
}
return true;
});
chrome.management.onDisabled.addListener(function (extension) {
// this one is fired when extension is restarted
// check if this is our child extension and re-enable it
if (extension.name == 'Test child extension') {
enable(afterEnable);
}
});
Run Code Online (Sandbox Code Playgroud)
{
"manifest_version": 2,
"name" : "Test parent extension",
"version" : "1.0",
"description" : "Whatever",
"background" : {
"scripts": [
"background.js"
]
},
"content_scripts": [
{
"matches": [
"*://localhost/*"
],
"js": [
"content.js"
],
"run_at": "document_end",
"all_frames": true
}
],
"permissions": [
"tabs",
"management",
"*://localhost/*"
]
}
Run Code Online (Sandbox Code Playgroud)
{
"manifest_version": 2,
"name" : "Test child extension",
"version" : "1.0",
"description" : "Whatever",
"content_scripts": [
{
"matches": [
"*://localhost/*"
],
"css": [
"style.css"
],
"run_at": "document_end",
"all_frames": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
.
??? background.js
??? child
? ??? manifest.json
? ??? style.css
??? content.js
??? manifest.json
Run Code Online (Sandbox Code Playgroud)
现在打开about:extensions并http://localhost在拆分屏幕中。单击按钮,查看子扩展名每次刷新的方式。您也可以检出控制台。甚至尝试从内部禁用子扩展about:extensions-只要父扩展正在运行,这是不可能的。
| 归档时间: |
|
| 查看次数: |
6092 次 |
| 最近记录: |