三天前,我開始使用chrome擴展程序,我非常喜歡它。我遇到了一個問題:我最小化了重現我的問題的腳本:如果我 stackoverflow.com,我可以單擊該圖標,如果它向后臺腳本發送消息并收到消息“工作”,則會打開一個彈出窗口,上面寫著“工作”。如果我現在重新啟動瀏覽器,我會得到一個彈出窗口,說明開發人員模式下的擴展可能是有害的,以及我是否要停用它們。我關閉此消息,當我現在單擊擴展名時,它不起作用,我收到以下錯誤:Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.我發現后臺腳本沒有運行(我沒有收到來自后臺的警報.js或打印到后臺.js控制臺)。我想擴展程序以及背景.js可能被chrome阻止啟動,因為它是開發人員模式下的擴展程序。使擴展再次運行的唯一方法是從 chrome://extensions 刷新擴展。我嘗試使用持久和非持久背景腳本,但這并沒有改變行為。我的分析是否正確,因此是否是在 Webstore 上部署腳本以使其正常運行的唯一解決方案?或者,是否有其他方法可以在Chrome啟動時使開發人員模式下的應用程序啟動?下面是最小示例:manifest.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 }}背景.jschrome.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);}
Chrome擴展程序:后臺腳本未啟動,僅在刷新后
互換的青春
2022-09-02 10:26:14