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.

目前为 2014-02-19 提交的版本。查看 最新版本

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