Guest User

Untitled

a guest
Sep 16th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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, updated for HTTP by Dildoer the Cocknight
  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 class="image-search" label="Search SauceNao for image" icon="https://i.imgur.com/3Fn1aTv.png"></menuitem>\
  42. <menuitem class="image-search-http" label="Search SauceNao for HTTP version image" icon="https://i.imgur.com/3Fn1aTv.png"></menuitem> </menu>';
  43.  
  44. document.querySelector("#userscript-search-by-image menuitem.image-search")
  45.         .addEventListener("click", searchImage, false);
  46.  
  47. document.querySelector("#userscript-search-by-image menuitem.image-search-http")
  48.         .addEventListener("click", searchImageHTTP, false);
  49.  
  50. function initMenu(aEvent) {
  51.   // Executed when user right click on web page body
  52.   // aEvent.target is the element you right click on
  53.   var node = aEvent.target;
  54.   var item = document.querySelector("#userscript-search-by-image menuitem.image-search");
  55.   var item2 = document.querySelector("#userscript-search-by-image menuitem.image-search-http");
  56.   if (node.localName == "img") {
  57.     body.setAttribute("contextmenu", "userscript-search-by-image");
  58.     item.setAttribute("imageURL", node.src);
  59.     item2.setAttribute("imageURL", node.src);
  60.   } else {
  61.     body.removeAttribute("contextmenu");
  62.     item.removeAttribute("imageURL");
  63.   }
  64. }
  65.  
  66. function addParamsToForm(aForm, aKey, aValue) {
  67.   var hiddenField = document.createElement("input");
  68.   hiddenField.setAttribute("type", "hidden");
  69.   hiddenField.setAttribute("name", aKey);
  70.   hiddenField.setAttribute("value", aValue);
  71.   aForm.appendChild(hiddenField);
  72. }
  73.  
  74. function searchImage(aEvent) {
  75.   // Executed when user click on menuitem
  76.   // aEvent.target is the <menuitem> element
  77.   var imageURL = aEvent.target.getAttribute("imageURL");
  78.   window.open(`https://saucenao.com/search.php?url=${imageURL}`);
  79.  
  80.    
  81. }
  82.  
  83. function searchImageHTTP(aEvent) {
  84.   // Executed when user click on menuitem
  85.   // aEvent.target is the <menuitem> element
  86.   var imageURL = aEvent.target.getAttribute("imageURL");
  87.   let url = new URL(imageURL);
  88.   window.open(`https://saucenao.com/search.php?url=http://${url.host}${url.pathname}`);
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment