Chrome扩展程序页面操作不会显示在omnibar旁边 [英] Chrome Extension Page Action not showing next to omnibar

查看:133
本文介绍了Chrome扩展程序页面操作不会显示在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


  1. 变量 tab ,用于 checkURL 无法定义。

  2. 它不像你所假设的那样是一个内置函数)。


    它也是一个好主意要过滤 onUpdated 事件寻找 status:'complete',因为多个 onUpdated <

    因此,请将 background.js 代码替换为以下代码:

    p>

      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:

    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);
    

    这篇关于Chrome扩展程序页面操作不会显示在omnibar旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆