如何在Chrome扩展程序中使用带有declarativeContent侦听器的addRules调用任意函数?

时间:2015-12-11 19:20:52

标签: javascript google-chrome google-chrome-extension

我正在尝试更改我从浏览器操作到页面操作的扩展程序。目前,扩展程序会检测选项卡更改,如果选项卡的URL位于我们的域中,则会请求JSON数据以获取状态信息,因此可以将好图标或错误图标显示为状态指示符。我的浏览器操作代码是:

chrome.tabs.onActivated.addListener(function(activeInfo) {
    chrome.tabs.get(activeInfo.tabId, function(tab) {
        var url = tab.url,
            matches = url.match(/(domain1)|(domain2)\.com/g);

        if (url && matches) {
            // Query for JSON data and change the icon based on it.
            loadReach(url);
        } else {
            // Change the icon bad
        }
    });
});

我有基本的侦听器来插入declarativeContent侦听器并显示初始图标,但我不确定在哪里放置我的回调来查询JSON数据:

// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
  // Replace all rules ...
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    // With a new rule ...
    chrome.declarativeContent.onPageChanged.addRules([
      {
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { hostContains: 'domain1.com' },
          }),
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { hostContains: 'domain2.com' },
          })
        ],
        // And shows the extension's page action.
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});

我可以在第二个代码块中运行该回调,或者此方法不支持?

0 个答案:

没有答案