简体   繁体   English

Chrome扩展程序:更改页面上的背景颜色

[英]Chrome Extension: Change background Color on pageLoad

I'm trying to change the background color of web page onLoad. 我正在尝试更改网页onLoad的背景颜色。 But, the script is not being executed Here is my code. 但是,脚本没有被执行这是我的代码。

manifest.json 的manifest.json

{
  "name": "Page Redder",
  "description": "Make the current page red",
  "version": "2.0",
  "permissions": [
    "activeTab"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Make this page red"
  },
  "manifest_version": 2
}

background.js background.js

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete')
        chrome.tabs.executeScript(tabId, {file:"code.js"});
});

chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({
    code: 'document.body.style.backgroundColor="green"'
});
});

code.js code.js

document.body.style.backgroundColor="red"

It is working when I click on the Extension Icon. 单击扩展图标时它正在工作。 But, code.js is not being executed. 但是, code.js没有被执行。

use content scripts. 使用内容脚本。 They are much simpler than doing it directly from the background. 它们比直接从后台进行简单得多。

content scripts are automatically inserted into your new loaded tab if you use the match "matches": ["<all_urls>"] 如果您使用匹配"matches": ["<all_urls>"]内容脚本将自动插入到新加载的选项卡"matches": ["<all_urls>"]

documentation: https://developer.chrome.com/extensions/content_scripts 文档: https//developer.chrome.com/extensions/content_scripts

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

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