Chrome扩展程序页面操作不会显示在omnibar旁边 [英] Chrome Extension Page Action not showing next to omnibar
问题描述
{
manifest_version:2,
name:Optimize url,
description:优化网址,
page_action:{
default_icon:{
19:url-icon16.png,
38:url-icon48.png
},
default_title:优化网址
},
background:{
scripts:[background.js]
},
version:0.1,
permissions:[
tabs,
https://url.com/*
}
<后台JS:
函数checkURL(){
var host = parseURL(tab.url).host;
if(host.indexOf(url.com)> = 0){
chrome.pageAction.show(tabId);
}
}
chrome.tabs.onUpdated.addListener(checkURL);
然而,当我将它添加到正在开发的扩展页面中时。它没有出现在任何地方。我原本打算把它作为一个浏览器动作,但是由于它只会专注于一个网站,所以它更多地使用它作为页面动作。
任何人都可以向我解释我可能做错了什么?
您的代码存在以下问题:
$ b
-
变量
tab
,用于checkURL
无法定义。 它不像你所假设的那样是一个内置函数)。 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).
它也是一个好主意要过滤 因此,请将 background.js 代码替换为以下代码: onUpdated
事件寻找 status:'complete'
,因为多个 onUpdated <
var hostRegex = / ^ [^:] +:\ / \ / [^ \ / ] * url.com / I;
函数checkURL(tabId,info,tab){
if(info.status ===complete){
if(hostRegex.test(tab.url)){
chrome.pageAction.show(tabId);
}
}
}
chrome.tabs.onUpdated.addListener(checkURL);
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?
There are the following problems with your code:
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);
这篇关于Chrome扩展程序页面操作不会显示在omnibar旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!