Brendan Abolivier 8 år sedan
förälder
incheckning
5e7206dca1
4 ändrade filer med 40 tillägg och 57 borttagningar
  1. 11
    8
      eventPage.js
  2. 3
    4
      manifest.json
  3. 5
    16
      popup.html
  4. 21
    29
      popup.js

+ 11
- 8
eventPage.js Visa fil

@@ -3,7 +3,7 @@ sites = {}
3 3
 
4 4
 function getSitesAndMirrors() {
5 5
     $.getJSON("http://host.brendanabolivier.com/files/56bf24564ab24.json")
6
-                                    .done(function(data) { sites = data })
6
+                    .done(function(data) { chrome.storage.local.set({"sites": data}) })
7 7
 }
8 8
 
9 9
 function getCurrentTabUrl(callback) {
@@ -47,16 +47,19 @@ function getCurrentTabUrl(callback) {
47 47
 
48 48
 function updateTab() {
49 49
     getCurrentTabUrl(function(url) {
50
-        if(url.match(/:\/\/(.+)\//)[1] in sites) {
51
-            chrome.browserAction.setIcon({path: 'icon-red.png'})
52
-        }
53
-        else {
54
-            chrome.browserAction.setIcon({path: 'icon.png'})
55
-        }
50
+        chrome.storage.local.get("sites", function(sites){
51
+            sites = sites.sites
52
+            if(url.match(/:\/\/(.+)\//)[1] in sites) {
53
+                chrome.browserAction.setIcon({path: 'icon-red.png'})
54
+            }
55
+            else {
56
+                chrome.browserAction.setIcon({path: 'icon.png'})
57
+            }
58
+        })
56 59
     })
57 60
 }
58 61
 
59 62
 chrome.runtime.onStartup.addListener(getSitesAndMirrors)
60
-chrome.runtime.onInstall.addListener(getSitesAndMirrors)
63
+chrome.runtime.onInstalled.addListener(getSitesAndMirrors)
61 64
 chrome.tabs.onActivated.addListener(updateTab)
62 65
 chrome.tabs.onUpdated.addListener(updateTab)

+ 3
- 4
manifest.json Visa fil

@@ -2,7 +2,7 @@
2 2
     "manifest_version": 2,
3 3
 
4 4
     "name": "Collateral Freedom",
5
-    "description": "kthxbye",
5
+    "description": "Find mirrors for blocked websites",
6 6
     "version": "1.0",
7 7
 
8 8
     "browser_action": {
@@ -10,9 +10,8 @@
10 10
         "default_popup": "popup.html"
11 11
     },
12 12
     "permissions": [
13
-        "tabs",
14
-        "https://ajax.googleapis.com/",
15
-        "http://host.brendanabolivier.com/"
13
+        "storage",
14
+        "tabs"
16 15
     ],
17 16
 
18 17
     "background": {

+ 5
- 16
popup.html Visa fil

@@ -1,9 +1,4 @@
1 1
 <!doctype html>
2
-<!--
3
- This page is shown when the extension button is clicked, because the
4
- "browser_action" field in manifest.json contains the "default_popup" key with
5
- value "popup.html".
6
- -->
7 2
 <html>
8 3
   <head>
9 4
     <title>Getting Started Extension's Popup</title>
@@ -11,8 +6,6 @@
11 6
       body {
12 7
         font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
13 8
         font-size: 100%;
14
-      }
15
-      #status {
16 9
         /* avoid an excessively wide status text */
17 10
         white-space: pre;
18 11
         text-overflow: ellipsis;
@@ -20,18 +13,14 @@
20 13
         max-width: 400px;
21 14
       }
22 15
     </style>
23
-
24
-    <!--
25
-      - JavaScript and HTML must be in separate files: see our Content Security
26
-      - Policy documentation[1] for details and explanation.
27
-      -
28
-      - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
29
-     -->
16
+    <script src="jquery.js"></script>
30 17
     <script src="popup.js"></script>
31 18
   </head>
32 19
   <body>
33
-    <div id="status"></div>
34
-    <img id="image-result" hidden>
20
+    <div id="mirror" style="display:none">This website may be blocked in your region.<br />
21
+        <button>Take me to a mirror</button>
22
+    </div>
23
+    <div id="nomirror">This website has no mirror provided by RWB</div>
35 24
   </body>
36 25
 </html>
37 26
 

+ 21
- 29
popup.js Visa fil

@@ -1,7 +1,3 @@
1
-// Copyright (c) 2014 The Chromium Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style license that can be
3
-// found in the LICENSE file.
4
-
5 1
 /**
6 2
  * Get the current URL.
7 3
  *
@@ -9,48 +5,44 @@
9 5
  *   is found.
10 6
  */
11 7
 function getCurrentTabUrl(callback) {
12
-  // Query filter to be passed to chrome.tabs.query - see
13
-  // https://developer.chrome.com/extensions/tabs#method-query
14 8
   var queryInfo = {
15 9
     active: true,
16 10
     currentWindow: true
17 11
   };
18 12
 
19 13
   chrome.tabs.query(queryInfo, function(tabs) {
20
-    // chrome.tabs.query invokes the callback with a list of tabs that match the
21
-    // query. When the popup is opened, there is certainly a window and at least
22
-    // one tab, so we can safely assume that |tabs| is a non-empty array.
23
-    // A window can only have one active tab at a time, so the array consists of
24
-    // exactly one tab.
25 14
     var tab = tabs[0];
26 15
 
27
-    // A tab is a plain object that provides information about the tab.
28
-    // See https://developer.chrome.com/extensions/tabs#type-Tab
29 16
     var url = tab.url;
30 17
 
31
-    // tab.url is only available if the "activeTab" permission is declared.
32
-    // If you want to see the URL of other tabs (e.g. after removing active:true
33
-    // from |queryInfo|), then the "tabs" permission is required to see their
34
-    // "url" properties.
35 18
     console.assert(typeof url == 'string', 'tab.url should be a string');
36 19
 
37 20
     callback(url);
38 21
   });
39
-
40
-  // Most methods of the Chrome extension APIs are asynchronous. This means that
41
-  // you CANNOT do something like this:
42
-  //
43
-  // var url;
44
-  // chrome.tabs.query(queryInfo, function(tabs) {
45
-  //   url = tabs[0].url;
46
-  // });
47
-  // alert(url); // Shows "undefined", because chrome.tabs.query is async.
48 22
 }
49 23
 
50
-function renderStatus(statusText) {
51
-  document.getElementById('status').textContent = statusText;
24
+function takeMeTo(url) {
25
+    updateProperties = {
26
+        url: "https://" + url
27
+    }
28
+    chrome.tabs.update(updateProperties=updateProperties)
52 29
 }
53 30
 
54 31
 getCurrentTabUrl(function(url) {
55
-    renderStatus(url.match(/:\/\/(.+)\//)[1])
32
+    chrome.storage.local.get("sites", function(sites){
33
+        sites = sites.sites
34
+        domain = url.match(/:\/\/(.+)\//)[1]
35
+        if(domain in sites) {
36
+            proxies = sites[url.match(/:\/\/(.+)\//)[1]]
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
+    })
56 48
 })