繁体   English   中英

隐藏特定网址的页面操作

[英]Hide page action for specific url

我正在为reddit.com构建chrome扩展程序,并且为此使用了page action 现在,我希望page_action icon仅对于特定的网址格式可见,即

http://www.reddit.com   [allowed]
http://www.reddit.com/r/*   [allowed]
http://www.reddit.com/r/books/comments/*   [not allowed]

因此,如上所述,我不希望在涉及comments url of redddit 3rd case看到扩展页面操作图标。

目前,我在background.js使用以下代码来实现此目的:

function check(tab_id, data, tab){
    if(tab.url.indexOf("reddit.com") > -1 && tab.url.indexOf("/comments/") == -1){
        chrome.pageAction.show(tab_id);
    }
};
chrome.tabs.onUpdated.addListener(check);

我还在我的manifest.json添加了以下行,以禁用评论页面上的扩展名

 "exclude_matches": ["http://www.reddit.com/r/*/comments/*"],

因此,我的问题是这是从特定页面/ URL禁用和隐藏扩展名的正确/理想方法吗?

为什么不是Zoidb-我的意思是正则表达式?

var displayPageAction = function (tabId, changeInfo, tab) {
    var regex = new RegExp(/.../); //Your regex goes here
    var match = regex.exec(tab.url); 
    // We only display the Page Action if we are inside a tab that matches
    if(match && changeInfo.status == 'complete') {
      chrome.pageAction.show(tabId);
    }
};

chrome.tabs.onUpdated.addListener(displayPageAction);

关于这种方法,我认为使用onUpdated.addListener是正确的方法。 作为一种好习惯,除非您的应用程序要求另有说明,否则请尝试仅在加载选项卡后才显示页面“操作”。

您可以使用此工具生成正则表达式,如果需要帮助,请随时询问,我们将帮助您汇编所需的正则表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM