Find access to blocked websites https://rsf.org/collateral-freedom

popup.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * Get the current URL.
  6. *
  7. * @param {function(string)} callback - called when the URL of the current tab
  8. * is found.
  9. */
  10. function getCurrentTabUrl(callback) {
  11. // Query filter to be passed to chrome.tabs.query - see
  12. // https://developer.chrome.com/extensions/tabs#method-query
  13. var queryInfo = {
  14. active: true,
  15. currentWindow: true
  16. };
  17. chrome.tabs.query(queryInfo, function(tabs) {
  18. // chrome.tabs.query invokes the callback with a list of tabs that match the
  19. // query. When the popup is opened, there is certainly a window and at least
  20. // one tab, so we can safely assume that |tabs| is a non-empty array.
  21. // A window can only have one active tab at a time, so the array consists of
  22. // exactly one tab.
  23. var tab = tabs[0];
  24. // A tab is a plain object that provides information about the tab.
  25. // See https://developer.chrome.com/extensions/tabs#type-Tab
  26. var url = tab.url;
  27. // tab.url is only available if the "activeTab" permission is declared.
  28. // If you want to see the URL of other tabs (e.g. after removing active:true
  29. // from |queryInfo|), then the "tabs" permission is required to see their
  30. // "url" properties.
  31. console.assert(typeof url == 'string', 'tab.url should be a string');
  32. callback(url);
  33. });
  34. // Most methods of the Chrome extension APIs are asynchronous. This means that
  35. // you CANNOT do something like this:
  36. //
  37. // var url;
  38. // chrome.tabs.query(queryInfo, function(tabs) {
  39. // url = tabs[0].url;
  40. // });
  41. // alert(url); // Shows "undefined", because chrome.tabs.query is async.
  42. }
  43. /**
  44. * @param {string} searchTerm - Search term for Google Image search.
  45. * @param {function(string,number,number)} callback - Called when an image has
  46. * been found. The callback gets the URL, width and height of the image.
  47. * @param {function(string)} errorCallback - Called when the image is not found.
  48. * The callback gets a string that describes the failure reason.
  49. */
  50. function getImageUrl(searchTerm, callback, errorCallback) {
  51. // Google image search - 100 searches per day.
  52. // https://developers.google.com/image-search/
  53. var searchUrl = 'https://ajax.googleapis.com/ajax/services/search/images' +
  54. '?v=1.0&q=' + encodeURIComponent(searchTerm);
  55. var x = new XMLHttpRequest();
  56. x.open('GET', searchUrl);
  57. // The Google image search API responds with JSON, so let Chrome parse it.
  58. x.responseType = 'json';
  59. x.onload = function() {
  60. // Parse and process the response from Google Image Search.
  61. var response = x.response;
  62. if (!response || !response.responseData || !response.responseData.results ||
  63. response.responseData.results.length === 0) {
  64. errorCallback('No response from Google Image search!');
  65. return;
  66. }
  67. var firstResult = response.responseData.results[0];
  68. // Take the thumbnail instead of the full image to get an approximately
  69. // consistent image size.
  70. var imageUrl = firstResult.tbUrl;
  71. var width = parseInt(firstResult.tbWidth);
  72. var height = parseInt(firstResult.tbHeight);
  73. console.assert(
  74. typeof imageUrl == 'string' && !isNaN(width) && !isNaN(height),
  75. 'Unexpected respose from the Google Image Search API!');
  76. callback(imageUrl, width, height);
  77. };
  78. x.onerror = function() {
  79. errorCallback('Network error.');
  80. };
  81. x.send();
  82. }
  83. function renderStatus(statusText) {
  84. document.getElementById('status').textContent = statusText;
  85. }
  86. document.addEventListener('DOMContentLoaded', function() {
  87. getCurrentTabUrl(function(url) {
  88. // Put the image URL in Google search.
  89. renderStatus('Performing Google Image search for ' + url);
  90. getImageUrl(url, function(imageUrl, width, height) {
  91. renderStatus('Search term: ' + url + '\n' +
  92. 'Google image search result: ' + imageUrl);
  93. var imageResult = document.getElementById('image-result');
  94. // Explicitly set the width/height to minimize the number of reflows. For
  95. // a single image, this does not matter, but if you're going to embed
  96. // multiple external images in your page, then the absence of width/height
  97. // attributes causes the popup to resize multiple times.
  98. imageResult.width = width;
  99. imageResult.height = height;
  100. imageResult.src = imageUrl;
  101. imageResult.hidden = false;
  102. }, function(errorMessage) {
  103. renderStatus('Cannot display image. ' + errorMessage);
  104. });
  105. });
  106. });