获取Chrome pageAction图标以显示JSON中的URL列表

Get Chrome pageAction icon to appear for an URL list in JSON

本文关键字:JSON 中的 URL 列表 显示 Chrome pageAction 图标 获取      更新时间:2023-09-26

我目前正在开发一个Chrome扩展,我希望pageAction图标显示大量URL(大约500个)。

在我的background.js文件(以及其他代码)中,我有这样的:

// Called when the url of a tab changes
function checkForValidUrl(tabId, changeInfo, tab) {
    // If the tabs url starts with "http://specific_domain"
    if (tab.url.indexOf('http://specific_domain') == 0) {
    // Shows the page action
    chrome.pageAction.show(tabId);
    }
};
// Listen for any changes to the URL of any tab
chrome.tabs.onUpdated.addListener(checkForValidUrl);

我已经可以在特定网站上使用它了,但我想更改http://specific_domain'添加到允许的网站列表中,就像白名单一样。我可以访问一个联机的JSON文件。

这里有一个片段:

{
  "antelife.com": {
    "fbid": "AntElifecom",
    "type": "store",
    "name": "AntElife"
  },
  "amazon.com": {
    "fbid": "Amazon",
    "type": "store",
    "name": "Amazon"
  }, 
  "ebay.com": {
    "fbid": "eBay",
    "type": "store",
    "name": "eBay"
  },
  "mobilegeeks.com": {
    "fbid": "mobilegeekscom",
    "type": "publisher",
    "name": "Mobile Geeks"
  }
}

我想提取域,并以某种方式迭代所有域,如果它们在列表中,则应显示pageAction图标。类似这样的东西:

function checkForValidUrl(tabId, changeInfo, tab) {
  for (var i = 0, iLength = whiteListUrl.length; i < iLength; i++) {
    if (tab.url.indexOf('http://www.'+whiteListUrl[i]) > -1) {
      chrome.pageAction.show(tabId);
      notification.show();
      break;
    }
  }
};
chrome.tabs.onUpdated.addListener(checkForValidUrl);

任何形式的帮助都应该是有用的。非常感谢。

如果你有一个json字符串,你可以通过这种方式获得url的列表;

var urlObj = JSON.parse(jsonString);
var whiteListUrl = Object.keys(urlObj);