问题
Manifest:
{
"manifest_version":2,
"name":"Optimize url",
"description":"Optimize url",
"page_action":{
"default_icon":{
"19":"url-icon16.png",
"38":"url-icon48.png"
},
"default_title":"Optimize url"
},
"background":{
"scripts":["background.js"]
},
"version":"0.1",
"permissions":[
"tabs",
"https://url.com/*"
]
}
Background JS:
function checkURL(){
var host = parseURL(tab.url).host;
if (host.indexOf("url.com") >= 0) {
chrome.pageAction.show(tabId);
}
}
chrome.tabs.onUpdated.addListener(checkURL);
Yet when I add it to the developing Extensions page. It doesn't show up anywhere. I was originally going to have this as a browser action but it made more since to use it as a page action since it's only going to be focused for one website only.
Can anyone explain to me what I may be doing wrong?
回答1:
There are the following problems with your code:
The variable
tab
, which is used incheckURL
, is nowhere defined.The function
parseURL
is also nowhere defined (it is not a built-in function as you seem to assume).
It is, also, a good idea to filter the onUpdated
events looking for status: 'complete'
, because several onUpdated
events are triggered during a single tab update.
So, replace your background.js code with the following:
var hostRegex = /^[^:]+:\/\/[^\/]*url.com/i;
function checkURL(tabId, info, tab) {
if (info.status === "complete") {
if (hostRegex.test(tab.url)) {
chrome.pageAction.show(tabId);
}
}
}
chrome.tabs.onUpdated.addListener(checkURL);
来源:https://stackoverflow.com/questions/20183957/chrome-extension-page-action-not-showing-next-to-omnibar