简体   繁体   English

Chrome 扩展:在页面加载前注入 JS

[英]Chrome extension: Inject JS before page load

Is it possible to inject JS before page load, or is it necessary to use content scripts and way for the document to finish?是否可以在页面加载之前注入 JS,或者是否需要使用内容脚本和方式来完成文档?

For example, is there a faster way to execute JS that turns the page red as soon as it's opened?比如有没有更快的方式来执行一打开页面就变红的JS?

Declare a content script in the manifest file with "run_at": "document_start" to get it to run as soon as possible, ie right after constructing the document root (when <head> does not exist yet).在清单文件中使用"run_at": "document_start"声明内容脚本以使其尽快运行,即在构建文档根目录之后(当<head>尚不存在时)。

For your very specific example, it might be better to declare a content style instead, similar to content scripts, but using the "css" key instead of "js" .对于您非常具体的示例,最好改为声明内容样式,类似于内容脚本,但使用"css"键而不是"js"

If you want to dynamically run a script as soon as possible, then call chrome.tabs.executeScript when the chrome.webNavigation.onCommitted event is triggered.如果您想尽快动态运行脚本,则在触发chrome.webNavigation.onCommitted事件时调用chrome.tabs.executeScript

For Manifest V3 there was a new field added to content scripts in 102.0.5005.58 Stable: world对于 Manifest V3,在 102.0.5005.58 稳定版中的内容脚本中添加了一个新字段: world

And there are also a lot of chrome bugs related to that topic: #634381 , #1054624 , #1207006还有很多与该主题相关的 chrome 错误: #634381#1054624#1207006

You have to use "world": "main" at the content script together with "run_at": "document_start" and a CSP to allow the injection from the Extension.您必须在内容脚本中使用"world": "main"以及"run_at": "document_start"和 CSP 以允许从扩展中注入。 Otherwise the injected script gets rejected with:否则注入的脚本会被拒绝:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval'".拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' 'wasm-unsafe-eval'”。 Either the 'unsafe-inline' keyword, a hash ('sha256-*'), or a nonce ('nonce-...') is required to enable inline execution.启用内联执行需要“不安全内联”关键字、hash(“sha256-*”)或随机数(“nonce-...”)。

"world": "MAIN": “世界”:“主要”:

[Extensions] Add main world injections for dynamic content scripts [扩展] 为动态内容脚本添加主世界注入

This CL adds a new field "world" for dynamic content scripts which allows the extension to specify if the script will run in the isolated or main world.此 CL 为动态内容脚本添加了一个新字段“world”,允许扩展指定脚本是在独立世界还是在主世界中运行。 By default, scripts which do not specify this field will run in the isolated world .默认情况下,不指定该字段的脚本将在隔离世界中运行。

Valid attributes are "ISOLATED" (default) or "MAIN" .有效属性是"ISOLATED" (默认)或"MAIN"

Example files i used:我使用的示例文件:

manifest.json清单.json

{
  "name": "script injection",
  "version": "0",
  "manifest_version": 3,
  "minimum_chrome_version": "103.0",
  "content_scripts": [
    {
      "matches": ["*://*/*"],
      "js": ["inject.js"],
      "run_at": "document_start",
      "world": "MAIN"
    }
  ],
  "web_accessible_resources": [{
    "resources": ["injected.js"],
    "matches": ["<all_urls>"]
  }],
  "content_security_policy": {
    "extension_pages": "default-src 'self' 'wasm-unsafe-eval';"
  }
}

inject.js注入.js

let el = document.createElement("script");
el.src = chrome.runtime.getURL("injected.js");
document.documentElement.appendChild(el);
console.log("injected");

injected.js注入.js

console.log(typeof alert); // "function"
console.log("injected file");
delete window.alert;
console.log(typeof alert); // "undefined"

[ [在此处输入图像描述 ] ]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM