Chrome 扩展:后台脚本未启动,仅在刷新后启动

Mic*_*sLE 1 html javascript google-chrome google-chrome-extension

三天前我开始使用 chrome 扩展,我非常喜欢它。我遇到了一个问题:我最小化了重现问题的脚本:

如果我在 stackoverflow.com,如果它向后台脚本发送消息并收到“工作”消息,我可以单击该图标,然后会打开一个弹出窗口,显示“工作”。

如果我现在重新启动浏览器,我会收到一个弹出窗口,提示开发人员模式下的扩展可能有害,以及我是否要停用它们。我忽略了此消息,当我现在单击扩展程序时,它不起作用,并且出现以下错误:

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
Run Code Online (Sandbox Code Playgroud)

我发现后台脚本没有运行(我没有收到来自background.js的警报或打印到background.js控制台)。我想扩展程序和background.js可能被chrome阻止启动,因为它是开发人员模式下的扩展程序。让扩展再次运行的唯一方法是从 chrome://extensions 刷新扩展。

我尝试使用持久和非持久后台脚本,但这并没有改变行为。

我的分析是否正确,因此是在网上商店部署脚本以使其正常运行的唯一解决方案?或者是否有另一种方法可以使应用程序在开发者模式下在 chrome 启动时启动?

这是最小的例子:

清单.json

{
    "name": "BackgroundScript Error",
    "version": "0.0.1",
    "manifest_version": 2,
    "description": "When starting Chrome, background doesn't start, but when refreshing, background starts",
    "page_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": ["declarativeContent", "<all_urls>"],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    }
}
Run Code Online (Sandbox Code Playgroud)

背景.js

chrome.runtime.onInstalled.addListener(function() {
    console.log('Background script is running');
    alert("Background is running");
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([{
        conditions: [new chrome.declarativeContent.PageStateMatcher({
            pageUrl: {hostEquals: 'stackoverflow.com'},
        })],
        actions: [new chrome.declarativeContent.ShowPageAction()]
    }]);
    });

    console.log('setting up connection to popup');
    chrome.runtime.onConnect.addListener(connected);
});


chrome.runtime.onMessage.addListener(function(request, sender) {
    console.log(request.action);
});

function connected(p) {
    console.log("connected to "+p);
    p.postMessage({action: 'didSomething', result: 'worked' });
    p.onDisconnect.addListener(disconnected);
}

function disconnected(p) {
    console.log("disconnected from "+p);
}
Run Code Online (Sandbox Code Playgroud)

弹出窗口.js

var myPort

function onWindowLoad() {
    var message = document.getElementById('message');

    myPort = chrome.runtime.connect({name:"port-from-cs"});
    myPort.postMessage({request: "doSomething"});
    myPort.onMessage.addListener(function(m) {
        console.log("In popup script, received message from background script: ");
        message.innerHTML = m.result;
    });
}

window.onload = onWindowLoad;

Run Code Online (Sandbox Code Playgroud)

弹出窗口.html

<!DOCTYPE html>
<html style=''>
    <head>
        <script src='popup.js' ></script>
    </head>
    <body>

        <div id="message">Popup</div>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Tom*_*Tom 5

不,您不需要部署到网上商店即可使其正常工作。

您遇到问题是因为您已将大部分后台脚本活动附加到chrome.runtime.onInstalled侦听器 - 仅在安装扩展或清单版本更改时才会触发。当您第二次重新启动浏览器时,您的扩展程序已安装,清单版本保持不变,因此该事件不会触发。