[英]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.