我正在尝试在页面中加载(注入)javascript 代码。javascript 文件是扩展程序的本地文件。文件路径是“js/somefile.js”。
const basePath = chrome.runtime.getURL('');
fetch(chrome.runtime.getURL(filePath), { mode: 'same-origin' }) // <-- important
.then((_res) => _res.blob())
.then((_blob) => {
const reader = new FileReader();
reader.addEventListener('loadend', (data) => {
callback(data.currentTarget.result, basePath);
});
reader.readAsText(_blob);
});
const scriptTag = document.createElement('script');
scriptTag.innerHTML = scriptText;
scriptTag.type = 'text/javascript';
const scriptElement = document[injectLocation].appendChild(scriptTag);
if (removeImmediately) document[injectLocation].removeChild(scriptElement);
Run Code Online (Sandbox Code Playgroud)
我的网络可访问资源是:
"web_accessible_resources": [{
"resources": [
"js/*.js",
],
"matches": ["<all_urls>"]
}],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'",
"sandbox": "sandbox allow-scripts; script-src 'self' 'https://apis.google.com/' 'https://www.gstatic.com/' 'https://*.firebaseio.com' 'https://www.googleapis.com' 'https://ajax.googleapis.com'; object-src …
Run Code Online (Sandbox Code Playgroud) javascript google-chrome-extension chrome-extension-manifest-v3
我正在尝试将 Chrome 扩展程序从 Manifest 2 迁移到 Manifest 3,但出现以下错误
拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' 'wasm-unsafe-eval'”。启用内联执行需要“unsafe-inline”关键字、哈希值(“sha256-ClANdr6bWuUdXWELI09IBiITbU5zbvg6V1dZp9mr55Q=”)或随机数(“nonce-...”)。
对于代码
我尝试添加
"content_security_policy": {
"extension_page": "script-src 'self' 'sha256-ClANdr6bWuUdXWELI09IBiITbU5zbvg6V1dZp9mr55Q='"
},
Run Code Online (Sandbox Code Playgroud)
清单,但没有帮助
我该如何解决?
我正在关注 chrome 官方网站https://developer.chrome.com/docs/extensions/mv3/getstarted/中发布的如何使 chrome 扩展入门
我复制并粘贴了所有代码并以相同的方式运行。但就我而言,当我运行时chrome.scripting.executeScript
,它会导致"Uncaught (in promise) Error: Cannot access a chrome:// URL"
错误。
我不知道有什么问题。这是我从上面的链接复制的代码。
{
"name": "Getting Started Example",
"description": "Build an Extension!",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": ["storage", "activeTab", "scripting"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
},
"icons": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
}
Run Code Online (Sandbox Code Playgroud)
let color = '#3aa757';
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ …
Run Code Online (Sandbox Code Playgroud) google-chrome google-chrome-extension chrome-extension-manifest-v3
我正在尝试将 Chrome 扩展程序切换为使用 Manifest v3。
除了我正在使用的地方之外,我已经得到了一切localStorage
:
if (localStorage.getItem(lastchecked[0]) < Date.now() - 2500000000) {
localStorage.setItem(lastchecked[0], Date.now());
} else {
const remover = Date.now() - 2500000000;
Object.entries(localStorage).forEach(([k, v]) => {
if (v < remover) {
delete localStorage[k];
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
ReferenceError: localStorage is not defined
Run Code Online (Sandbox Code Playgroud)
据我所知,这是因为我将扩展从使用背景切换script
为使用service worker
,这似乎无法访问localStorage
.
localStorage
除了它不可用之外,还有什么简单的方法可以将其切换为使用其他东西吗?
google-chrome google-chrome-extension chrome-extension-manifest-v3
我在 background.js 中发现“未捕获(承诺中)错误:侦听器通过返回 true 指示异步响应,但消息通道在收到响应之前已关闭”错误。我的 chrome 扩展可以工作,但开发工具在 background.js 上显示很多此类错误
我最近将 chrome 扩展从 Manifest V2 迁移到 Manifest V3,从那时起我发现了错误。
这是错误似乎产生的地方:
const browser = chrome || browser;
// Listen for messages from content script
browser.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
var frameId = sender.frameId || 0;
if (request.type == "connect") {
connect(sender.tab, frameId);
sendResponse({ success: true });
} else if (request.type == "disconnect") {
disconnect(sender.tab, frameId);
sendResponse({ success: true });
} else if (request.type == "send") {
sendNativeMessage(request.data, sender.tab);
document.addEventListener('nativeMessage',
function …
Run Code Online (Sandbox Code Playgroud) javascript google-chrome-extension promise chrome-native-messaging chrome-extension-manifest-v3
我开发了一个扩展来从网页上抓取一些内容,到目前为止它工作正常,但自从我切换到清单 v3 后,解析不再工作。
我使用以下脚本来读取源代码:
chrome.scripting.executeScript(
{
target: {tabId: tab.id, allFrames: true},
files: ['GetSource.js'],
}, async function(results)
{
// GETTING HTML
parser = new DOMParser();
content = parser.parseFromString(results, "text/html");
Run Code Online (Sandbox Code Playgroud)
... ETC ... 该代码曾经工作正常,但现在我在控制台中收到以下消息:
未捕获(承诺中)ReferenceError:DOMParser 未定义
代码是承诺的一部分,但我不认为承诺是这里的问题。我基本上需要将源代码加载到变量中,以便之后可以解析它。
我检查了文档,但没有发现 DOMParser 不适用于 v3 的内容。
任何想法?
谢谢
我有一个背景脚本定义如下
"background": {
"service_worker": "background.js"
},
Run Code Online (Sandbox Code Playgroud)
和内容脚本
"content_scripts": [
{
"matches": [
"*://youtube.com/*"
],
"js": ["content-script.js"]
}
Run Code Online (Sandbox Code Playgroud)
背景与往常一样
runtime.onMessage.addListener
Run Code Online (Sandbox Code Playgroud)
和内容脚本
chrome.runtime.sendMessage({
Run Code Online (Sandbox Code Playgroud)
但是,在某些罕见的情况下(大约 2-5% 的用户),尽管 YouTube 的选项卡已打开,但 Service Worker 仍保持不活动状态,因此 sendMessage 调用不会返回任何内容。
这只能通过关闭并重新打开扩展来解决。
如果服务工作者睡着了,有没有办法以编程方式唤醒它?是否有可能导致 Service Worker 无法醒来的原因?未捕获的异常,清单设置?
我真的很迷失在这里,调试起来非常困难,因为在一个月内我从未发生过这种情况,而且只有当用户同意进行通话和共享屏幕时我才能调试它。
该扩展程序拥有超过一百万用户,但这简直要了我的命。
该扩展可以在https://github.com/Anarios/return-youtube-dislike/tree/main/Extensions/combined看到
最新版本的 chrome 上已确认发生此情况。
google-chrome google-chrome-extension chrome-extension-manifest-v3
我需要使用清单版本 3(MV3) 来处理 Chrome 扩展上的 Google 登录。鉴于文档中只提到它无法完成,我该怎么办?
google-chrome google-chrome-extension google-signin manifest.json chrome-extension-manifest-v3
链接到 Xcode 项目的存储库来测试问题:测试 CORS 问题
\n这个扩展使用 Manifest v3,我example.com
有host_permissions
"host_permissions": [\n "*://example.com/*"\n ]\n
Run Code Online (Sandbox Code Playgroud)\n当我运行fetch("https://example.com")
时,background.js
我收到此错误:
[Error] Fetch API cannot load https://example.com/ due to access control checks.\n[Error] Failed to load resource: Origin safari-web-extension://e0b5d7c7-c079-484b-8825-44d261383cb6 is not allowed by Access-Control-Allow-Origin. Status code: 200 (example.com, line 0)\n
Run Code Online (Sandbox Code Playgroud)\n在 Safari 版本 16.0(17614.1.25.9.10、17614)和 Safari 技术预览版 153(Safari 16.0、WebKit 17615.1.4.1)中进行了测试
\n在代码中,我使用chrome
命名空间(在 Chrome 浏览器中测试它),但我遇到了与browser
命名空间相同的问题。
如果我在 Chrome 浏览器中运行此扩展程序而不进行任何更改,它会完美运行\n(chrome://extensions/ \xe2\x86\x92 Load Unpacked)
\n有没有办法在不更改服务器访问控制设置的情况下避免 …
google-chrome-extension safari-extension chrome-extension-manifest-v3
我已经尝试过这里的一切:
我无法过去:
WebAssembly.instantiate(buffer, {wasi_snapshot_preview1: ""})
WebAssembly.instantiate(buffer, {go: {debug: ""}})
// (i'm making them empty strings just to show I need to add the key but obviously they are not strings.)
Run Code Online (Sandbox Code Playgroud)
即我收到如下错误:
Failed to load WebAssembly module: TypeError: WebAssembly.instantiate(): Import #0 module="wasi_snapshot_preview1" error: module is not an object or function
Run Code Online (Sandbox Code Playgroud)
或模块debug
或runtime.resetMemoryDataView
或所有内容wasm_exec.js
但如果我使用simple.wasm
来自:
https://github.com/inflatablegrade/Extension-with-WASM
有用!那 wasm 一定是用 c 或 rust 编译的而不是 golang 吗?有没有办法让这项工作从 go 开始?
PS,这Extension-with-WASM
是清单 2.0,但我让它与 3.0 一起工作: …
javascript go google-chrome-extension webassembly chrome-extension-manifest-v3