Search by Image Context Menu

Add menu in browser context menu when you right click on a standalone image page to search that image on search engines.

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

  1. /*
  2. Search by Image Context Menu
  3. Add menu in browser context menu when you right click on a standalone
  4. image page to search that image on search engines.
  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 Search by Image Context Menu
  23. // @namespace http://userscripts.org/users/12
  24. // @description Add menu in browser context menu when you right click on a standalone image page to search that image on search engines.
  25. // @version 2.0
  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. // @grant GM.openInTab
  33. // ==/UserScript==
  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 services = [
  42. {
  43. "name": "Google",
  44. "host": "https://www.google.com/",
  45. "query": "searchbyimage?image_url="
  46. }, {
  47. "name": "Bing",
  48. "host": "https://www.bing.com/",
  49. "query": "images/search?view=detailv2&iss=sbi&q=imgurl:"
  50. }, {
  51. "name": "Yandex",
  52. "host": "https://yandex.com/",
  53. "query": "images/search?rpt=imageview&url="
  54. }, {
  55. "name": "SauceNAO",
  56. "host": "https://saucenao.com/",
  57. "query": "search.php?url="
  58. }, {
  59. "name": "IQDB",
  60. "host": "https://iqdb.org/",
  61. "query": "?url="
  62. }, {
  63. "name": "TinEye",
  64. "host": "https://tineye.com/",
  65. "query": "search?sort=size&order=desc&url="
  66. }
  67. ];
  68.  
  69. var menu = body.appendChild(document.createElement("menu"));
  70. menu.setAttribute("id", "userscript-search-by-image");
  71. menu.setAttribute("type", "context");
  72.  
  73. for (let i in services) {
  74. let menuitem = menu.appendChild(document.createElement("menuitem"));
  75. menuitem.setAttribute("label", "Search " + services[i].name);
  76. menuitem.setAttribute("icon", services[i].host + "favicon.ico");
  77. menuitem.setAttribute("url", services[i].host + services[i].query);
  78. }
  79.  
  80. document.querySelector("#userscript-search-by-image")
  81. .addEventListener("click", searchImage, false);
  82.  
  83. // Executed when user right click on web page body
  84. // aEvent.target is the element you right click on
  85. function initMenu(aEvent) {
  86. let node = aEvent.target;
  87. let item = document.querySelector("#userscript-search-by-image");
  88. if (node.localName == "img") {
  89. body.setAttribute("contextmenu", "userscript-search-by-image");
  90. item.setAttribute("imageURL", node.src);
  91. } else {
  92. body.removeAttribute("contextmenu");
  93. item.removeAttribute("imageURL");
  94. }
  95. }
  96.  
  97. function addParamsToForm(aForm, aKey, aValue) {
  98. let hiddenField = document.createElement("input");
  99. hiddenField.setAttribute("type", "hidden");
  100. hiddenField.setAttribute("name", aKey);
  101. hiddenField.setAttribute("value", aValue);
  102. aForm.appendChild(hiddenField);
  103. }
  104.  
  105. // Executed when user click on menuitem
  106. // aEvent.target is the <menu> element
  107. function searchImage(aEvent) {
  108. let imageURL = aEvent.target.parentNode.getAttribute("imageURL");
  109. console.log(aEvent.target);
  110. if (imageURL.indexOf("data:") == 0) {
  111. let base64Offset = imageURL.indexOf(",");
  112. if (base64Offset != -1) {
  113. let inlineImage = imageURL.substring(base64Offset + 1)
  114. .replace(/\+/g, "-")
  115. .replace(/\//g, "_")
  116. .replace(/\./g, "=");
  117.  
  118. let form = document.createElement("form");
  119. form.setAttribute("method", "POST");
  120. form.setAttribute("action", "//www.google.com/searchbyimage/upload");
  121. form.setAttribute("enctype", "multipart/form-data");
  122. form.setAttribute("target", "_blank");
  123. addParamsToForm(form, "image_content", inlineImage);
  124. addParamsToForm(form, "filename", "");
  125. addParamsToForm(form, "image_url", "");
  126. body.appendChild(form);
  127. form.submit();
  128. }
  129. } else {
  130. let url = aEvent.target.getAttribute("url") +
  131. encodeURIComponent(imageURL);
  132. if (typeof GM_openInTab == "function") {
  133. GM_openInTab(url);
  134. } else if (typeof GM.openInTab == "function") {
  135. GM.openInTab(url, false);
  136. } else {
  137. open(url);
  138. }
  139. }
  140. }