Google Image Search Context Menu

Add 'Search by Image' in browser context menu when you right click on image to search Google with that image.

当前为 2020-07-17 提交的版本,查看 最新版本

  1. /*
  2. Google Image Search Context Menu
  3. Add 'Search by Image' in browser context menu when you
  4. right click on image to search Google with that image.
  5. Copyright (C) 2012 LouCypher
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>
  19. */
  20.  
  21. // ==UserScript==
  22. // @name Google Image Search Context Menu
  23. // @namespace http://userscripts.org/users/12
  24. // @description Add 'Search by Image' in browser context menu when you right click on image to search Google with that image.
  25. // @version 1.3
  26. // @author LouCypher
  27. // @license GPL
  28. // @resource license https://raw.github.com/LouCypher/userscripts/master/licenses/GPL/LICENSE.txt
  29. // @include *
  30. // @exclude file://*
  31. // @grant GM_openInTab
  32. // ==/UserScript==
  33.  
  34. if (!("contextMenu" in document.documentElement &&
  35. "HTMLMenuItemElement" in window)) return;
  36.  
  37. var body = document.body;
  38. body.addEventListener("contextmenu", initMenu, false);
  39.  
  40. var menu = body.appendChild(document.createElement("menu"));
  41. menu.outerHTML = '<menu id="userscript-search-by-image" type="context">\
  42. <menuitem label="Search Google with this image"\
  43. icon="data:image/png;base64,\
  44. iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\
  45. AAAK6wAACusBgosNWgAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNXG14zYAAAEl\
  46. SURBVDiNY/z//z8DJYCRkIKsthv/kRX9Z2BgmFalARdiIcaGKZXqcH5O+01U+ay2G3MYGBiSiXUm\
  47. mofnsBDSjEUTMkiBe2Eq1JnZ7TcZBHhZGNythBl0lLkZODmYGX7++sdw/sZnhl3H3zF8+voHwwsY\
  48. FkR5ijNICLMzTF31hOHnr38MHGxMDJlhMgwv3vxkWL7jJYpaJmzu0lTigWtmYGBg+PHrH8P0VU8Y\
  49. tJV5MNRiNYCfmxmuGQZ+/PrHwMmOqRyrAX///WfgYEOV4mBjwjAUpwHHL31iyA6XgRvCwcbEkBUm\
  50. w3DuxmcMtVgDkYONicHLVoTBSJOXgYONieHHz38Ml+98Ydh88DXDtx//CBtACmBiYGCYS4H+OYyU\
  51. 5kasgUgKAADN8WLFzlj9rgAAAABJRU5ErkJggg=="></menuitem>\
  52. </menu>';
  53.  
  54. document.querySelector("#userscript-search-by-image menuitem")
  55. .addEventListener("click", searchImage, false);
  56.  
  57. // Executed when user right click on web page body
  58. // aEvent.target is the element you right click on
  59. function initMenu(aEvent) {
  60. var node = aEvent.target;
  61. var item = document.querySelector("#userscript-search-by-image menuitem");
  62. if (node.localName == "img") {
  63. body.setAttribute("contextmenu", "userscript-search-by-image");
  64. item.setAttribute("imageURL", node.src);
  65. } else {
  66. body.removeAttribute("contextmenu");
  67. item.removeAttribute("imageURL");
  68. }
  69. }
  70.  
  71. function addParamsToForm(aForm, aKey, aValue) {
  72. var hiddenField = document.createElement("input");
  73. hiddenField.setAttribute("type", "hidden");
  74. hiddenField.setAttribute("name", aKey);
  75. hiddenField.setAttribute("value", aValue);
  76. aForm.appendChild(hiddenField);
  77. }
  78.  
  79. // Executed when user click on menuitem
  80. // aEvent.target is the <menuitem> element
  81. function searchImage(aEvent) {
  82. var imageURL = aEvent.target.getAttribute("imageURL");
  83. if (imageURL.indexOf("data:") == 0) {
  84. var base64Offset = imageURL.indexOf(",");
  85. if (base64Offset != -1) {
  86. var inlineImage = imageURL.substring(base64Offset + 1)
  87. .replace(/\+/g, "-")
  88. .replace(/\//g, "_")
  89. .replace(/\./g, "=");
  90.  
  91. var form = document.createElement("form");
  92. form.setAttribute("method", "POST");
  93. form.setAttribute("action", "//www.google.com/searchbyimage/upload");
  94. form.setAttribute("enctype", "multipart/form-data");
  95. form.setAttribute("target", "_blank");
  96. addParamsToForm(form, "image_content", inlineImage);
  97. addParamsToForm(form, "filename", "");
  98. addParamsToForm(form, "image_url", "");
  99. body.appendChild(form);
  100. form.submit();
  101. }
  102. } else {
  103. var url = "https://www.google.com/searchbyimage?image_url=" +
  104. encodeURIComponent(imageURL);
  105. //console.log("GM_openInTab: " + typeof GM_openInTab == "function")
  106. if (typeof GM_openInTab == "function")
  107. GM_openInTab(url);
  108. else
  109. open(url);
  110. }
  111. }