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
1 Answer
Reset to default 8In 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
版权声明:本文标题:javascript - Chrome Extension: Change Address bar Button Background color at runtime - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744357358a2602371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论