Browse Source

Reindent + doc

Brendan Abolivier 7 years ago
parent
commit
6c8ae15f80
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
3 changed files with 99 additions and 90 deletions
  1. 35
    30
      backgroundPage.js
  2. 26
    27
      index.html
  3. 38
    33
      index.js

+ 35
- 30
backgroundPage.js View File

@@ -2,45 +2,50 @@ sites_url = "https://raw.githubusercontent.com/RSF-RWB/collateralfreedom/master/
2 2
 sites = {}
3 3
 domain_regexp = /:\/\/(www\.)?([^\/]+)\//
4 4
 
5
+// Retrieve the list of mirrors
5 6
 function getSitesAndMirrors() {
6
-    $.getJSON(sites_url).done(function(data) { chrome.storage.local.set({"sites": data}) })
7
+	$.getJSON(sites_url).done(function(data) { chrome.storage.local.set({"sites": data}) })
7 8
 }
8 9
 
10
+// Get the URL on the current tab
9 11
 function getCurrentTabUrl(callback) {
10
-  var queryInfo = {
11
-    active: true,
12
-    currentWindow: true
13
-  };
14
-
15
-  chrome.tabs.query(queryInfo, function(tabs) {
16
-    var tab = tabs[0];
17
-
18
-    var url = tab.url;
19
-
20
-    console.assert(typeof url == 'string', 'tab.url should be a string');
21
-
22
-    callback(url);
23
-  });
12
+	var queryInfo = {
13
+		active: true,
14
+		currentWindow: true
15
+	};
16
+
17
+	chrome.tabs.query(queryInfo, function(tabs) {
18
+		var tab = tabs[0];
19
+		var url = tab.url;
20
+		console.assert(typeof url == 'string', 'tab.url should be a string');
21
+		callback(url);
22
+	});
24 23
 }
25 24
 
25
+// Check if the URL on the current tab is bound to a list of mirrors. If so,
26
+// indicate it to the user by turning the icon red.
26 27
 function updateTab() {
27
-    getCurrentTabUrl(function(url) {
28
-        chrome.storage.local.get("sites", function(sites){
29
-            sites = sites.sites
30
-            if(url.match(domain_regexp)) {
31
-                // Skipping detection for about:* pages
32
-                domain = url.match(domain_regexp).slice(-1)[0]
33
-                if(domain in sites) {
34
-                    chrome.browserAction.setIcon({path: 'rsc/icon-red.png'})
35
-                }
36
-                else {
37
-                    chrome.browserAction.setIcon({path: 'rsc/icon.png'})
38
-                }
39
-            }
40
-        })
41
-    })
28
+	getCurrentTabUrl(function(url) {
29
+		chrome.storage.local.get("sites", function(sites){
30
+			sites = sites.sites
31
+			if(url.match(domain_regexp)) {
32
+				// Skipping detection for about:* pages
33
+				domain = url.match(domain_regexp).slice(-1)[0]
34
+				if(domain in sites) {
35
+					chrome.browserAction.setIcon({path: 'rsc/icon-red.png'})
36
+				}
37
+				else {
38
+					chrome.browserAction.setIcon({path: 'rsc/icon.png'})
39
+				}
40
+			}
41
+		})
42
+	})
42 43
 }
43 44
 
45
+// Run the updateTab() function on tab switch (Ctrl+Tab), creation or target
46
+// update
44 47
 chrome.tabs.onActivated.addListener(updateTab)
45 48
 chrome.tabs.onUpdated.addListener(updateTab)
49
+
50
+// Fetch the list of mirrors on startup
46 51
 getSitesAndMirrors()

+ 26
- 27
index.html View File

@@ -1,30 +1,29 @@
1 1
 <!doctype html>
2 2
 <html>
3
-  <head>
4
-    <title>Collateral Freedom</title>
5
-    <style>
6
-      body {
7
-        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
8
-        font-size: 100%;
9
-        text-overflow: ellipsis;
10
-        overflow: hidden;
11
-        width: 400px;
12
-        text-align:center;
13
-      }
14
-      button {
15
-        width: 100%;
16
-        height: 50px;
17
-        margin-top: 10px;
18
-      }
19
-    </style>
20
-    <script src="jquery.js"></script>
21
-    <script src="index.js"></script>
22
-  </head>
23
-  <body>
24
-    <div id="mirror" style="display:none">This website may be blocked in your region.<br />
25
-        <button>Take me to a mirror</button>
26
-    </div>
27
-    <div id="nomirror">This website has no mirror provided by RWB</div>
28
-  </body>
3
+	<head>
4
+		<title>Collateral Freedom</title>
5
+		<style>
6
+			body {
7
+				font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
8
+				font-size: 100%;
9
+				text-overflow: ellipsis;
10
+				overflow: hidden;
11
+				width: 400px;
12
+				text-align:center;
13
+			}
14
+			button {
15
+				width: 100%;
16
+				height: 50px;
17
+				margin-top: 10px;
18
+			}
19
+		</style>
20
+		<script src="jquery.js"></script>
21
+		<script src="index.js"></script>
22
+	</head>
23
+	<body>
24
+		<div id="mirror" style="display:none">This website may be blocked in your region.<br />
25
+				<button>Take me to a mirror</button>
26
+		</div>
27
+		<div id="nomirror">This website has no mirror provided by RWB</div>
28
+	</body>
29 29
 </html>
30
-

+ 38
- 33
index.js View File

@@ -2,47 +2,52 @@
2 2
  * Get the current URL.
3 3
  *
4 4
  * @param {function(string)} callback - called when the URL of the current tab
5
- *   is found.
5
+ * is found.
6 6
  */
7 7
 function getCurrentTabUrl(callback) {
8
-  var queryInfo = {
9
-    active: true,
10
-    currentWindow: true
11
-  };
8
+	var queryInfo = {
9
+		active: true,
10
+		currentWindow: true
11
+	};
12 12
 
13
-  chrome.tabs.query(queryInfo, function(tabs) {
14
-    var tab = tabs[0];
13
+	chrome.tabs.query(queryInfo, function(tabs) {
14
+		var tab = tabs[0];
15
+		var url = tab.url;
16
+		console.assert(typeof url == 'string', 'tab.url should be a string');
15 17
 
16
-    var url = tab.url;
17
-
18
-    console.assert(typeof url == 'string', 'tab.url should be a string');
19
-
20
-    callback(url);
21
-  });
18
+		callback(url);
19
+	});
22 20
 }
23 21
 
22
+// Redirect to mirror url
24 23
 function takeMeTo(url) {
25
-    updateProperties = {
26
-        url: "https://" + url
27
-    }
28
-    chrome.tabs.update(updateProperties=updateProperties)
24
+	updateProperties = {
25
+			url: "https://" + url
26
+	}
27
+	chrome.tabs.update(updateProperties=updateProperties)
29 28
 }
30 29
 
30
+// Check if there's a mirror for the current URL. If so, display the message
31
+// about it and the button to redirect to a random mirror (if there's more than
32
+// one).
31 33
 getCurrentTabUrl(function(url) {
32
-    chrome.storage.local.get("sites", function(sites){
33
-        sites = sites.sites
34
-        domain = url.match(/:\/\/(www\.)?([^\/]+)\//).slice(-1)[0]
35
-        if(domain in sites) {
36
-            proxies = sites[domain]
37
-            $("#mirror").css("display", "block")
38
-            $("#nomirror").css("display", "none")
39
-            $("#mirror button").on("click", function() {
40
-                takeMeTo(proxies[Math.floor(Math.random()*proxies.length)])
41
-            })
42
-        }
43
-        else {
44
-            $("#mirror").css("display", "none")
45
-            $("#nomirror").css("display", "block")
46
-        }
47
-    })
34
+	chrome.storage.local.get("sites", function(sites){
35
+			sites = sites.sites
36
+			// Grab the domain part of the URL
37
+			domain = url.match(/:\/\/(www\.)?([^\/]+)\//).slice(-1)[0]
38
+			if(domain in sites) {
39
+					proxies = sites[domain]
40
+					// Offer the user to redirect them to a mirror
41
+					$("#mirror").css("display", "block")
42
+					$("#nomirror").css("display", "none")
43
+					$("#mirror button").on("click", function() {
44
+							takeMeTo(proxies[Math.floor(Math.random()*proxies.length)])
45
+					})
46
+			}
47
+			else {
48
+					// Tell the user there's no mirror available
49
+					$("#mirror").css("display", "none")
50
+					$("#nomirror").css("display", "block")
51
+			}
52
+	})
48 53
 })