forked from fo0nikens/collateral-freedom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (57 loc) · 1.71 KB
/
index.js
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
55
56
57
58
59
60
61
/**
* Get the current URL.
*
* @param {function(string)} callback - called when the URL of the current tab
* is found.
*/
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);
});
}
// Redirect to mirror url
function takeMeTo(url) {
let updateProperties = {
url: "https://" + url
}
chrome.tabs.update(updateProperties=updateProperties)
}
// Wait for the DOM to load before doing anything
jQuery(document).ready(function() {
// Check if there's a mirror for the current URL. If so, display the message
// about it and the button to redirect to a random mirror (if there's more than
// one).
getCurrentTabUrl(function(url) {
chrome.storage.local.get("sites_list", function(stored_sites){
sites = stored_sites.sites_list.sites
// Grab the domain part of the URL
let domain = url.match(/:\/\/(www\.)?([^\/]+)\//).slice(-1)[0]
console.log(domain);
console.log(sites);
let match = sites.find(function(item) {
return (domain === item.main_domain && item.available_mirrors.length > 0);
});
if(typeof match !== 'undefined') {
let proxies = match.available_mirrors;
// Offer the user to redirect them to a mirror
$("#mirror").css("display", "block")
$("#nomirror").css("display", "none")
$("#mirror button").on("click", function() {
takeMeTo(proxies[Math.floor(Math.random()*proxies.length)])
})
}
else {
// Tell the user there's no mirror available
$("#mirror").css("display", "none")
$("#nomirror").css("display", "block")
}
})
})
})