I'm brand new to Chrome extensions, but I wanted to start messing around with them for the fun of it. I know that it is against convention to always display a page_action
icon, however for the purpose of learning I'd like to display it for every tab.
I've been doing reading and I'm having a hard time figuring out what's deprecated and what's not. The docs don't seem to note that some methods are deprecated.
Here's what I have so far -- it's been a while since I've messed with Javascript so bear with me; this is all very simple stuff.
So what would be the easiest way to always show a page_action
icon? Am I even close to the right track? Thanks.
manifest.json
{
"manifest_version" : 2,
"name": "Hello World",
"version": "0.1",
"description": "Hello World",
"page_action": {
"default_icon": "images/blackWhite.jpg",
"default_title": "HW!"
},
"background": {
"scripts": ["scripts/background.js"]
}
}
background.js
chrome.tabs.getSelected(null, function(tab {
var id = tab.id
chrome.pageAction.show(id)
}))
Your code would execute only once, while it should execute on every tab update.
One approach to do so is to listen to chrome.tabs.onUpdated
, as done in a sample extension .
chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
if (change.status == "complete") {
chrome.pageAction.show(tabId);
}
});
This will make the page action appear when the page finishes loading.
If you want it to appear sooner, you can, for instance, listen to onUpdated
, onCreated
and onReplaced
without filters (you don't care if you call show()
several times).
Edit: Actually, another good combination is onUpdated
(to detect URL change) and onActivated
(to detect tab change).
Or, you can employ a content script that loads in all pages at document_start
and messages the background to show the page action.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.