admin管理员组

文章数量:1402918

Is it possible to change button background color on page load event but so far I did not find any such method at all. If it is not possible then I am happy to use some pre-defined images that loads up after page load.

I have tried the following, without success:

script.js

// chrome.browserAction.setIcon("red.png");
chrome.browserAction.setIcon({path:'red.png'});

manifest.json

{
  "name": "Domain Colors",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [{
      "matches": ["http://*/*"],
      "js": ["script.js"]
  }],
  "permissions": [ "tabs", "http://*/*" ],
  "browser_action": {
    "default_title": "Colry",
    "default_icon": "blue.png"
  }
}

Is it possible to change button background color on page load event but so far I did not find any such method at all. If it is not possible then I am happy to use some pre-defined images that loads up after page load.

I have tried the following, without success:

script.js

// chrome.browserAction.setIcon("red.png");
chrome.browserAction.setIcon({path:'red.png'});

manifest.json

{
  "name": "Domain Colors",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [{
      "matches": ["http://*/*"],
      "js": ["script.js"]
  }],
  "permissions": [ "tabs", "http://*/*" ],
  "browser_action": {
    "default_title": "Colry",
    "default_icon": "blue.png"
  }
}
Share Improve this question edited Nov 26, 2012 at 21:08 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Nov 26, 2012 at 11:50 Volatil3Volatil3 15k40 gold badges149 silver badges298 bronze badges 5
  • This is the code. pastie/5438480 I was talking about address bar button – Volatil3 Commented Nov 26, 2012 at 18:18
  • for some weird reason I am getting setBadgeBackgroundColor or setIcon method undefined. – Volatil3 Commented Nov 26, 2012 at 18:35
  • See Can chrome.* extension API's be used inside content scripts? – Rob W Commented Nov 26, 2012 at 18:59
  • Aah. So what you remend? Message passing? – Volatil3 Commented Nov 26, 2012 at 19:20
  • I am getting a bit confuse. Should I send a message to sendIcon() method or sendBadgeText by passing paramer as a JSON Object? – Volatil3 Commented Nov 26, 2012 at 19:31
Add a ment  | 

1 Answer 1

Reset to default 8

In order to use the browserAction API, a background page is required. If you want to keep your current control flow (update icon via a content script), you need to pass messages. Here's the bare minimum:

// script.js
chrome.extension.sendMessage('');
// background.js
chrome.extension.onMessage.addListener(function(message, sender) {
    chrome.browserAction.setBadgeBackgroundColor({
        color: 'red',
        tabId: sender.tab.id
    });
});

The manifest file needs one additional entry:

"background": {
    "scripts": ["background.js"]
}

You don't need content scripts for the purpose of detecting page loads. A single event listener can be used (in the background page):

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
    if (changeInfo.status === 'loading') {
        chrome.browserAction.setBadgeBackgroundColor({
            color: 'red',
            tabId: tabId
        });
    }
});

Have a close look at the documentation of chrome.browserAction.setBadgeBackgroundColor. There are also lots of examples using the browserAction API, you should be able to get a working extension yourself by looking at these samples.

本文标签: javascriptChrome Extension Change Address bar Button Background color at runtimeStack Overflow