SauceNao Reverse Image Search

Adds a "Search on SauceNao" option to the context menu

  1. /*
  2. SauceNao Reverse Image Search
  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 SauceNao Reverse Image Search
  23. // @description Adds a "Search on SauceNao" option to the context menu
  24. // @version 1.0
  25. // @author BasedPeacock
  26. // @license GPL
  27. // @include *
  28. // @exclude file://*
  29. // @grant GM_openInTab
  30. // @namespace https://greasyfork.org/users/148462
  31. // ==/UserScript==
  32.  
  33. if (!("contextMenu" in document.documentElement &&
  34. "HTMLMenuItemElement" in window)) return;
  35.  
  36. var body = document.body;
  37. body.addEventListener("contextmenu", initMenu, false);
  38.  
  39. var menu = body.appendChild(document.createElement("menu"));
  40. menu.outerHTML = '<menu id="userscript-search-by-image" type="context">\
  41. <menuitem label="Search SauceNao for image"\
  42. icon="data:image/x-icon;base64,Qk02AwAAAAAAADYAAAAoAAAAEAAAABAAAAABABgAAAAAAAADAADEDgAAxA4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////////////////////////////////////////////////AAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAA////AAAA////////AAAAAAAAAAAAAAAAAAAAAAAA////////AAAA////AAAAAAAAAAAAAAAA////////////AAAAAAAAAAAAAAAA////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////AAAAAAAA////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////AAAAAAAA////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////AAAAAAAAAAAAAAAA////////////AAAAAAAAAAAAAAAA////AAAA////////AAAAAAAAAAAAAAAAAAAAAAAA////////AAAA////AAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////AAAAAAAA////////////////////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"></menuitem>\
  43. </menu>';
  44.  
  45. document.querySelector("#userscript-search-by-image menuitem")
  46. .addEventListener("click", searchImage, false);
  47.  
  48. function initMenu(aEvent) {
  49. // Executed when user right click on web page body
  50. // aEvent.target is the element you right click on
  51. var node = aEvent.target;
  52. var item = document.querySelector("#userscript-search-by-image menuitem");
  53. if (node.localName == "img") {
  54. body.setAttribute("contextmenu", "userscript-search-by-image");
  55. item.setAttribute("imageURL", node.src);
  56. } else {
  57. body.removeAttribute("contextmenu");
  58. item.removeAttribute("imageURL");
  59. }
  60. }
  61.  
  62. function addParamsToForm(aForm, aKey, aValue) {
  63. var hiddenField = document.createElement("input");
  64. hiddenField.setAttribute("type", "hidden");
  65. hiddenField.setAttribute("name", aKey);
  66. hiddenField.setAttribute("value", aValue);
  67. aForm.appendChild(hiddenField);
  68. }
  69.  
  70. function searchImage(aEvent) {
  71. // Executed when user click on menuitem
  72. // aEvent.target is the <menuitem> element
  73. var imageURL = aEvent.target.getAttribute("imageURL");
  74. GM_openInTab("https://saucenao.com/search.php?url=" +
  75. encodeURIComponent(imageURL));
  76. }