首页 > 解决方案 > 在不加载活动选项卡的情况下运行内容脚本

问题描述

如何在不重新加载活动选项卡的情况下运行内容脚本?

内容脚本仅在我刷新活动选项卡时才有效。

清单.js

{
  "name": "CMS",
  "description": "A simple CMS solution that can update a website contents and images without modifying the current website structure.",
  "options_page": "options.html",
  "background": {
    "page": "background.html"
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": "icon48.png"
  },
  "icons": {
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*", "<all_urls>"],
      "js": ["contentScript.bundle.js"],
      "css": ["content.styles.css"]
    }
  ],
  "web_accessible_resources": [
    "content.styles.css",
    "icon128.png",
    "icon48.png"
  ],
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

内容脚本

let paragraphs = document.querySelectorAll('*');
for (const elt of paragraphs) {
  elt.style['border'] = '1px solid green';
}

标签: javascriptgoogle-chrome-extension

解决方案


推荐阅读