|
@@ -47,72 +47,10 @@ function getCurrentTabUrl(callback) {
|
47
|
47
|
// alert(url); // Shows "undefined", because chrome.tabs.query is async.
|
48
|
48
|
}
|
49
|
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
|
50
|
function renderStatus(statusText) {
|
92
|
51
|
document.getElementById('status').textContent = statusText;
|
93
|
52
|
}
|
94
|
53
|
|
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
|
|
-});
|
|
54
|
+getCurrentTabUrl(function(url) {
|
|
55
|
+ renderStatus(url.match(/:\/\/(.+)\//)[1])
|
|
56
|
+})
|