Ale*_*yes 1 google-chrome-extension
我按照 chrome 扩展的“入门”教程进行操作,但出现以下错误。
我搜索谷歌,有人说无法访问 content.js 中的“executeScript”,但错误来自 popup.js。
我曾尝试将 'chrome.scripting.executeScript' 更改为 'chrome.tabs.executeScript',它也不起作用。
清单文件
{
"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
}Run Code Online (Sandbox Code Playgroud)
弹出窗口.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;
};Run Code Online (Sandbox Code Playgroud)
Yuv*_*san 12
您需要脚本权限才能访问脚本 API。添加scripting到您的manifest.js文件。
"permissions": ["scripting", ...]
Run Code Online (Sandbox Code Playgroud)
小智 10
您的权限manifest.json缺少一项,即"scripting".
它应该是这样的:
…
"permissions": ["storage", "declarativeContent", "activeTab", "scripting"],
…
Run Code Online (Sandbox Code Playgroud)
这实际上可以在此处的“入门”页面上看到。
| 归档时间: |
|
| 查看次数: |
2340 次 |
| 最近记录: |