Repository navigation
Expand file tree
/
Copy pathbackgroundPage.js
More file actions
54 lines (48 loc) · 1.6 KB
/
Copy pathbackgroundPage.js
File metadata and controls
54 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var sites_url = "https://raw.githubusercontent.com/OpenTechFund/bypass-mirrors/master/mirrorSites.json"
var sites = {}
var domain_regexp = /:\/\/(www\.)?([^\/]+)\//
// Retrieve the list of mirrors
function getSitesAndMirrors() {
$.getJSON(sites_url).done(function(data) { chrome.storage.local.set({"sites_list": data}) })
}
// Get the URL on the current tab
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
callback(url);
});
}
// Check if the URL on the current tab is bound to a list of mirrors. If so,
// indicate it to the user by turning the icon red.
function updateTab() {
getCurrentTabUrl(function(url) {
chrome.storage.local.get("sites_list", function(stored_sites){
sites = stored_sites.sites_list.sites
if(url.match(domain_regexp)) {
// Skipping detection for about:* pages
let domain = url.match(domain_regexp).slice(-1)[0]
let exists = sites.findIndex(function(item) {
return (domain === item.main_domain && item.available_mirrors.length > 0);
});
if(exists !== -1) {
chrome.browserAction.setIcon({path: 'rsc/icon-red.png'})
}
else {
chrome.browserAction.setIcon({path: 'rsc/icon.png'})
}
}
})
})
}
// Run the updateTab() function on tab switch (Ctrl+Tab), creation or target
// update
chrome.tabs.onActivated.addListener(updateTab)
chrome.tabs.onUpdated.addListener(updateTab)
// Fetch the list of mirrors on startup
getSitesAndMirrors()