简体   繁体   English

从带有 chrome 扩展 v3 的内容脚本中注入 javascript

[英]Inject javascript from content script with a chrome extension v3

I'm migration my extension from V2 to V3.我正在将我的扩展从 V2 迁移到 V3。 Now all is working fine except for one thing.现在一切正常,除了一件事。 In my V2 version I did在我的 V2 版本中,我做了

const actualCode = '(' + function () { 'console.log("demo");' } + `)();`;
const script = document.createElement('script');
script.textContent = actualCode;
(document.head || document.documentElement).appendChild(script);
script.remove();

Note that the console.log("demo") is a simplification of what I need to inject:)请注意, console.log("demo")是对我需要注入的内容的简化:)

I need to inject some javascript for my chrome-extension-magic to take place.我需要注入一些 javascript 以使我的 chrome-extension-magic 发生。

Now, in V3 this doesn't work anymore.现在,在 V3 中,这不再起作用了。 I get the following error in my devtools-console我的 devtools-console 中出现以下错误

content.js:23114 
    
   Refused to execute inline script because it violates the following 
   ContentSecurity Policy directive: "script-src 'self'". Either the 
   'unsafe-inline' keyword, a hash ('sha256-tN52+5...6d2I/Szq8='), or a nonce
   ('nonce-...') is required to enable inline execution.

In the migration guide I noticed this section在迁移指南中,我注意到了这个部分

"content_security_policy": {
   "extension_pages": "...",
   "sandbox": "..."
}

but there is not much description there, so this is magic to me.但是那里没有太多描述,所以这对我来说很神奇。 So I hope someone know can help me with this?所以我希望有人知道可以帮助我吗?

Refer to Use a content script to access the page context variables and functions请参阅使用内容脚本访问页面上下文变量和函数

Since content scripts are executed in an "isolated world" environment, we can't do some special dom operations in content_script js.由于content scripts是在“孤立的世界”环境中执行的,所以我们不能在 content_script js 中做一些特殊的dom操作。

This example will show you how to inject inject.js to web page before document start : 此示例将向您展示如何在文档开始之前将 inject.js 注入web页面:

// document_start.js
var s = document.createElement('script');
s.src = chrome.runtime.getURL('inject.js');
s.onload = function() {
    this.remove();
};
(document.head || document.documentElement).appendChild(s);

manifest.json example for ManifestV3 manifest.json ManifestV3 示例

"content_scripts": [
      {
            "matches": ["<all_urls>"],
            "js": ["document_start.js"],
            "run_at": "document_start" //default document end
      }
]
"web_accessible_resources": [{
  "resources": ["inject.js"],
  "matches": ["<all_urls>"]
}]

I tried the same code but when I am calling appendChild(s) it's throwing exception "Refused to execute inline script because it violates the following Content Security Policy directive script-src 'self' 'unsafe-eval'. Either the unsafe-inline keyword, a hash or a nonce is required to enable inline execution."我尝试了相同的代码,但是当我调用 appendChild(s) 时,它抛出异常“拒绝执行内联脚本,因为它违反了以下内容安全策略指令 script-src'self''unsafe-eval'。unsafe-inline 关键字,需要 hash 或随机数来启用内联执行。” Even adding "unsafe-inline" is not fixing the issue.即使添加“不安全内联”也不能解决问题。

Can anyone guide me how to inject javascript for Chrome V3.谁能指导我如何为 Chrome V3 注入 javascript。 With V2 I am using appendchild and it's working fine.使用 V2,我正在使用 appendchild,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Manifest V3 将 FormData/File 对象从内容脚本传递到 chrome 扩展中的后台脚本 - Passing FormData/File Object from content script to background script in chrome extension with Manifest V3 如何在 chrome 扩展 v3 中加载内容脚本 - How to load content script in chrome extension v3 从后台执行内容脚本和关联的 CSS (Manifest V3) - Chrome 扩展 - Execute Content Script and associated CSS from Background (Manifest V3) - Chrome Extension Chrome扩展程序内容脚本 - 在页面代码之前注入Javascript - Chrome Extension Content Script - Inject Javascript before page code 将按钮注入网站(Chrome扩展程序内容脚本) - Inject button into site (Chrome Extension content script) Chrome扩展程序:创建标签然后将内容脚本注入其中 - Chrome extension: create tab then inject content script into it 以编程方式在扩展程序上注入内容脚本(Chrome扩展程序) - Inject content script on extension reload programmatically (chrome extension) Chrome 扩展清单 v3 内容安全政策 - Chrome extension manifest v3 Content Security Policy Chrome 扩展程序 - 尝试使用清单 v3 从后台脚本获取 () 时出现 CORS 错误 - Chrome Extension - Getting CORS error when trying to fetch() from background script with manifest v3 Chrome 扩展 - 从清单 v2 迁移到 v3 - Chrome extension - migrate from manifest v2 to v3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM