我按照 chrome 扩展的“入门”教程进行操作,但出现以下错误。
我搜索谷歌,有人说无法访问 content.js 中的“executeScript”,但错误来自 popup.js。
我曾尝试将“chrome.scripting.executeScript”更改为“chrome.tabs.executeScript”,但它也不起作用。
错误图像 https://i.stack.imgur.com/LmLI3.png
清单.json
{
"name": "Getting Started Example",
"version": "1.0",
"description": "Build an Extension!",
"permissions": ["storage", "declarativeContent", "activeTab"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"page_action": {
"default_popup": "popup.html"
},
"options_page": "options.html",
"manifest_version": 2
}
popup.js
let changeColor = document.getElementById('changeColor')
chrome.storage.sync.get('color', function(data) {
changeColor.style.backgroundColor = data.color;
changeColor.setAttribute('value', data.color)
});
changeColor.addEventListener('click', () =>
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.scripting.executeScript(
tabs[0].id,
{ function: setColor })
})
);
async function setColor() {
let {color} = await chrome.storage.sync.get(['color']);
document.body.style.backgroundColor = color;
};
整个扩展代码 https://drive.google.com/file/d/12-vtRsEa5WXrrhfUhAw3QnCjpJ86hLpV/view?usp=sharing
参考:chrome开发者->入门 https://developer.chrome.com/docs/extensions/mv3/getstarted/
您的权限manifest.json
缺少一项,即"scripting"
.
它应该看起来像这样:
…
"permissions": ["storage", "declarativeContent", "activeTab", "scripting"],
…
这实际上是在此处的入门页面 https://developer.chrome.com/docs/extensions/mv3/getstarted/#logic.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)