如何自动重启chrome扩展?

时间:2012-10-27 21:30:07

标签: google-chrome-extension application-restart

我构建了一个泄漏内存的chrome扩展程序。我正在努力修复内存泄漏,但与此同时,一些朋友已经在使用它了。

作为临时措施,我想要包含一个补丁,它会定期自动重启扩展。

如何执行此操作?,即从扩展程序本身重新启动chrome扩展程序。

感谢,

1 个答案:

答案 0 :(得分:5)

您可以有两个扩展程序。并从另一个重新启动目标扩展。如果这适合你,请继续阅读。

content.js

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

background.js

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

manifest.json(parent)

{
    "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/*"
    ]
}

manifest.json(孩子)

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

目录结构

.
├── background.js
├── child
│   ├── manifest.json
│   └── style.css
├── content.js
└── manifest.json

现在在分屏中打开about:extensionshttp://localhost。单击按钮,查看每次刷新子扩展的方式。你也可以看看控制台。甚至尝试在about:extensions内禁用子扩展名 - 只要父扩展程序正在运行,它就不可能。

相关问题