Преглед на файлове

Wait for document to be ready to be manipulated before manipulating it

Brendan Abolivier преди 7 години
родител
ревизия
5eba7d75ea
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
променени са 1 файла, в които са добавени 26 реда и са изтрити 17 реда
  1. 26
    17
      index.js

+ 26
- 17
index.js Целия файл

@@ -28,21 +28,30 @@ function takeMeTo(url) {
28 28
     chrome.tabs.update(updateProperties=updateProperties)
29 29
 }
30 30
 
31
-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
-    })
31
+// Wait for the DOM to load before doing anything
32
+jQuery(document).ready(function() {
33
+	// Check if there's a mirror for the current URL. If so, display the message
34
+	// about it and the button to redirect to a random mirror (if there's more than
35
+	// one).
36
+	getCurrentTabUrl(function(url) {
37
+		chrome.storage.local.get("sites", function(sites){
38
+			sites = sites.sites
39
+			// Grab the domain part of the URL
40
+			domain = url.match(/:\/\/(www\.)?([^\/]+)\//).slice(-1)[0]
41
+			if(domain in sites) {
42
+				proxies = sites[domain]
43
+				// Offer the user to redirect them to a mirror
44
+				$("#mirror").css("display", "block")
45
+				$("#nomirror").css("display", "none")
46
+				$("#mirror button").on("click", function() {
47
+					takeMeTo(proxies[Math.floor(Math.random()*proxies.length)])
48
+				})
49
+			}
50
+			else {
51
+				// Tell the user there's no mirror available
52
+				$("#mirror").css("display", "none")
53
+				$("#nomirror").css("display", "block")
54
+			}
55
+		})
56
+	})
48 57
 })