Chrome Extension Page Action not showing next to omnibar

随声附和 提交于 2019-12-13 20:08:43

问题


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:

  1. The variable tab, which is used in checkURL, is nowhere defined.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!