這是我的背景腳本;
/**
* CSS to hide everything on the page,
*/
const hidePage = `body > : {
display: none;
}`;
/**
* When a tab is loaded, check if the plugin is on. If the plugin is on, check if the url matches the page.
* If it does, block it
*/
browser.tabs.onActivated.addListener(function (activeInfo) {
browser.storage.local.get("onOff")
.then((result) => {
if (Object.entries(result).length === 0 || !result.onOff.value) {
return;
}
browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
let tab = tabs[0];
console.log(tab.url);
browser.tabs.insertCSS({code: hidePage})
}, console.error)
});
});
這是我的 manifest.json
{
"manifest_version": 2,
"name": "Beastify",
"version": "1.0",
"description": "Adds a browser action icon to the toolbar. Click the button to choose a beast. The active tab's body content is then replaced with a picture of the chosen beast. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#beastify",
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/beastify",
"icons": {
"48": "icons/beasts-48.png"
},
"permissions": [
"tabs",
"activeTab",
"storage"
],
"browser_action": {
"default_icon": "icons/beasts-32.png",
"default_title": "BlockIt",
"default_popup": "popup/blockit.html"
},
"background": {
"scripts": ["background_scripts/blocker.js"]
},
"web_accessible_resources": [
"beasts/frog.jpg",
"beasts/turtle.jpg",
"beasts/snake.jpg"
]
}
那里有一些多余的東西,因為我正在從Firefox 擴展教程構建我的附加組件。
導致的確切代碼
Uncaught (in promise) Error: Missing host permission for the tab是
browser.tabs.insertCSS({code: hidePage})
第 23 行 blocker.js
我相信我確實有從這個后臺腳本插入 css 的正確權限,所以我無法弄清楚為什么會發生這個錯誤。我還嘗試執行內容腳本,而不是運行上面拋出錯誤的行,但失敗并出現相同的錯誤。
uj5u.com熱心網友回復:
activeTab僅當用戶按照WebExtensions和Chrome 擴展程式的檔案中所述顯式呼叫您的擴展程式時才有效)。顯然,它的名字具有很強的誤導性:它應該是類似activeTabWhenInvoked.
為了在沒有事先與擴展程式互動的情況下訪問任何選項卡,您必須在 manifest.json 中添加廣泛的主機權限:
"permissions": ["<all_urls>"],
現在不需要activeTab,但您可能仍希望保留tabsFirefox 86 以上版本的權限,請參閱檔案中的注釋。
PS 最好在onOff為 false時完全洗掉偵聽器,這樣擴展就不會徒勞無功。您可以通過對 onActivated 偵聽onOff器使用全域命名函式并通過 browser.storage.onChanged 事件觀察更改來實作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403049.html
標籤:
