chrome.contextMenus排除网址?

时间:2013-04-22 14:56:14

标签: google-chrome-extension

我使用chrome.contextMenus documentUrlPatterns来过滤显示上下文菜单的位置。

我尝试过前期' - '对模式但不改变任何东西......

function recreateStuff(){
        if(logoncmid!=-1){}
        var hosts=[];
        var ihosts=[];
        for (var i = savedData.length - 1; i >= 0; i--) {
          hosts.push(savedData[i].host);
          ihosts.push("-"+savedData[i].host);
          //console.log(savedData[i]);
        };
        // hosts created ...
        chrome.contextMenus.update(logoncmid,{documentUrlPatterns: hosts});
        chrome.contextMenus.update(regcmid,{documentUrlPatterns: ihosts});


}

是否可以制作排除列表?

2 个答案:

答案 0 :(得分:2)

我认为目前不可能,因为documentUrlPatterns采用match patterns数组,并且无法表达负匹配模式。

您可以open a feature request使用机制来表达排除的匹配模式,as is done for content scriptsexclude_matchesexclude_globs

答案 1 :(得分:1)

黑客怎么样? :)

var windowTopTab = [];
var curWin = 1;
var curUrl = "";

function fetchCurUrl() {
  if (!windowTopTab[curWin]) return;
  chrome.tabs.get(windowTopTab[curWin], function(tab) {
    curUrl = url;
  });
}

chrome.tabs.onActivated.addListener(function(activeInfo) {
  windowTopTab[activeInfo.windowId] = activeInfo.tabId;
  fetchCurUrl();
  recreateStuff();
});
chrome.windows.onFocusChanged.addListener(function(windowId) {
  curWin = windowId;
  fetchCurUrl();
  recreateStuff();
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (windowTopTab[curWin])
    if (windowTopTab[curWin] == tabId) {
      fetchCurUrl();
      recreateStuff();
    }
});

var contextm = chrome.contextMenus.create({
  title: "* Fill RegForm",
  contexts: ["editable"],
  onclick: genericOnClick
});

function recreateStuff() {
  var curHN = getHostname(curUrl);
  var reg = true;
  for (var i = savedData.length - 1; i >= 0; i--) {
    if (savedData[i].host == curHN) reg = false;
  }

  if (reg) {
    chrome.contextMenus.update(contextm, {
      title: "* RegForm",
      onclick: genericOnClick
    });
  } else {
    chrome.contextMenus.update(contextm, {
      title: "* LoginForm",
      onclick: genericOnClick2
    });
  }
}