Bläddra i källkod

FIRST (hi imma 2010)

Brendan Abolivier 8 år sedan
incheckning
6bbb014bf2
4 ändrade filer med 171 tillägg och 0 borttagningar
  1. Binär
      icon.png
  2. 16
    0
      manifest.json
  3. 37
    0
      popup.html
  4. 118
    0
      popup.js

Binär
icon.png Visa fil


+ 16
- 0
manifest.json Visa fil

@@ -0,0 +1,16 @@
1
+{
2
+  "manifest_version": 2,
3
+
4
+  "name": "Collateral Freedom",
5
+  "description": "kthxbye",
6
+  "version": "1.0",
7
+
8
+  "browser_action": {
9
+    "default_icon": "icon.png",
10
+    "default_popup": "popup.html"
11
+  },
12
+  "permissions": [
13
+    "activeTab",
14
+    "https://ajax.googleapis.com/"
15
+  ]
16
+}

+ 37
- 0
popup.html Visa fil

@@ -0,0 +1,37 @@
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
+<html>
8
+  <head>
9
+    <title>Getting Started Extension's Popup</title>
10
+    <style>
11
+      body {
12
+        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
13
+        font-size: 100%;
14
+      }
15
+      #status {
16
+        /* avoid an excessively wide status text */
17
+        white-space: pre;
18
+        text-overflow: ellipsis;
19
+        overflow: hidden;
20
+        max-width: 400px;
21
+      }
22
+    </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
+     -->
30
+    <script src="popup.js"></script>
31
+  </head>
32
+  <body>
33
+    <div id="status"></div>
34
+    <img id="image-result" hidden>
35
+  </body>
36
+</html>
37
+

+ 118
- 0
popup.js Visa fil

@@ -0,0 +1,118 @@
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
+/**
6
+ * Get the current URL.
7
+ *
8
+ * @param {function(string)} callback - called when the URL of the current tab
9
+ *   is found.
10
+ */
11
+function getCurrentTabUrl(callback) {
12
+  // Query filter to be passed to chrome.tabs.query - see
13
+  // https://developer.chrome.com/extensions/tabs#method-query
14
+  var queryInfo = {
15
+    active: true,
16
+    currentWindow: true
17
+  };
18
+
19
+  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
+    var tab = tabs[0];
26
+
27
+    // A tab is a plain object that provides information about the tab.
28
+    // See https://developer.chrome.com/extensions/tabs#type-Tab
29
+    var url = tab.url;
30
+
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
+    console.assert(typeof url == 'string', 'tab.url should be a string');
36
+
37
+    callback(url);
38
+  });
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
+}
49
+
50
+/**
51
+ * @param {string} searchTerm - Search term for Google Image search.
52
+ * @param {function(string,number,number)} callback - Called when an image has
53
+ *   been found. The callback gets the URL, width and height of the image.
54
+ * @param {function(string)} errorCallback - Called when the image is not found.
55
+ *   The callback gets a string that describes the failure reason.
56
+ */
57
+function getImageUrl(searchTerm, callback, errorCallback) {
58
+  // Google image search - 100 searches per day.
59
+  // https://developers.google.com/image-search/
60
+  var searchUrl = 'https://ajax.googleapis.com/ajax/services/search/images' +
61
+    '?v=1.0&q=' + encodeURIComponent(searchTerm);
62
+  var x = new XMLHttpRequest();
63
+  x.open('GET', searchUrl);
64
+  // The Google image search API responds with JSON, so let Chrome parse it.
65
+  x.responseType = 'json';
66
+  x.onload = function() {
67
+    // Parse and process the response from Google Image Search.
68
+    var response = x.response;
69
+    if (!response || !response.responseData || !response.responseData.results ||
70
+        response.responseData.results.length === 0) {
71
+      errorCallback('No response from Google Image search!');
72
+      return;
73
+    }
74
+    var firstResult = response.responseData.results[0];
75
+    // Take the thumbnail instead of the full image to get an approximately
76
+    // consistent image size.
77
+    var imageUrl = firstResult.tbUrl;
78
+    var width = parseInt(firstResult.tbWidth);
79
+    var height = parseInt(firstResult.tbHeight);
80
+    console.assert(
81
+        typeof imageUrl == 'string' && !isNaN(width) && !isNaN(height),
82
+        'Unexpected respose from the Google Image Search API!');
83
+    callback(imageUrl, width, height);
84
+  };
85
+  x.onerror = function() {
86
+    errorCallback('Network error.');
87
+  };
88
+  x.send();
89
+}
90
+
91
+function renderStatus(statusText) {
92
+  document.getElementById('status').textContent = statusText;
93
+}
94
+
95
+document.addEventListener('DOMContentLoaded', function() {
96
+  getCurrentTabUrl(function(url) {
97
+    // Put the image URL in Google search.
98
+    renderStatus('Performing Google Image search for ' + url);
99
+
100
+    getImageUrl(url, function(imageUrl, width, height) {
101
+
102
+      renderStatus('Search term: ' + url + '\n' +
103
+          'Google image search result: ' + imageUrl);
104
+      var imageResult = document.getElementById('image-result');
105
+      // Explicitly set the width/height to minimize the number of reflows. For
106
+      // a single image, this does not matter, but if you're going to embed
107
+      // multiple external images in your page, then the absence of width/height
108
+      // attributes causes the popup to resize multiple times.
109
+      imageResult.width = width;
110
+      imageResult.height = height;
111
+      imageResult.src = imageUrl;
112
+      imageResult.hidden = false;
113
+
114
+    }, function(errorMessage) {
115
+      renderStatus('Cannot display image. ' + errorMessage);
116
+    });
117
+  });
118
+});