Pārlūkot izejas kodu

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

Brendan Abolivier 7 gadus atpakaļ
vecāks
revīzija
1eb01c88ed
Parakstījis: Brendan Abolivier <contact@brendanabolivier.com> GPG atslēgas ID: 8EF1500759F70623
1 mainītis faili ar 18 papildinājumiem un 15 dzēšanām
  1. 18
    15
      index.js

+ 18
- 15
index.js Parādīt failu

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