最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension: Change background Color on pageLoad - Stack Overflow

programmeradmin2浏览0评论

I'm trying to change the background color of web page onLoad. But, the script is not being executed Here is my code.

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

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

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

code.js

document.body.style.backgroundColor="red"

It is working when I click on the Extension Icon. But, code.js is not being executed.

I'm trying to change the background color of web page onLoad. But, the script is not being executed Here is my code.

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

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

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

code.js

document.body.style.backgroundColor="red"

It is working when I click on the Extension Icon. But, code.js is not being executed.

Share Improve this question asked May 7, 2014 at 6:40 Muaaz KhalidMuaaz Khalid 2,2593 gold badges33 silver badges53 bronze badges 1
  • 3 Try to change permission from activeTab to tabs (see developer.chrome./extensions/tabs) – devnull69 Commented May 7, 2014 at 7:19
Add a ment  | 

1 Answer 1

Reset to default 5

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>"]

documentation: https://developer.chrome./extensions/content_scripts

发布评论

评论列表(0)

  1. 暂无评论