gpt4 book ai didi

javascript - 获取 Chrome pageAction 图标以显示 JSON 格式的 URL 列表

转载 作者:行者123 更新时间:2023-11-28 07:37:44 32 4
gpt4 key购买 nike

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

在我的 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);

关于javascript - 获取 Chrome pageAction 图标以显示 JSON 格式的 URL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28409066/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com