Reverse Image Search

Search largest image on the page or current image on Google, TinEye, Yandex, SauceNAO and iqdb.org

当前为 2022-12-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reverse Image Search
  3. // @namespace github.com/cvzi/
  4. // @version 1.0.0
  5. // @description Search largest image on the page or current image on Google, TinEye, Yandex, SauceNAO and iqdb.org
  6. // @author cuzi
  7. // @copyright 2022, cuzi (https://github.com/cvzi/)
  8. // @license GPL-3.0-or-later
  9. // @match ://*/*
  10. // @icon https://raw.githubusercontent.com/hfg-gmuend/openmoji/master/color/72x72/1F5BC.png
  11. // @grant GM_openInTab
  12. // @grant GM_registerMenuCommand
  13. // ==/UserScript==
  14.  
  15. /*
  16. Reverse Image Search
  17. Search largest image on the page or current image on Google, TinEye, Yandex, SauceNAO and iqdb.org
  18. Copyright (C) 2022, cuzi (https://github.com/cvzi/)
  19.  
  20. This program is free software: you can redistribute it and/or modify
  21. it under the terms of the GNU General Public License as published by
  22. the Free Software Foundation, either version 3 of the License, or
  23. (at your option) any later version.
  24.  
  25. This program is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. GNU General Public License for more details.
  29.  
  30. You should have received a copy of the GNU General Public License
  31. along with this program. If not, see <https://www.gnu.org/licenses/>.
  32. */
  33.  
  34. /* globals GM_openInTab, GM_registerMenuCommand */
  35.  
  36. (function () {
  37. 'use strict'
  38.  
  39. function onKeyDown (keyCode, cb) {
  40. return function (ev) {
  41. if (document.activeElement === document.body) {
  42. if (ev.ctrlKey && ev.key === keyCode) {
  43. ev.preventDefault()
  44. cb(ev)
  45. }
  46. }
  47. }
  48. }
  49.  
  50. function getUrl () {
  51. if (document && document.body && document.body.firstChild.tagName === 'IMG') {
  52. return document.location.href
  53. } else {
  54. const biggestImage = Array.from(document.querySelectorAll('img[src^=http],img[srcset]')).filter(img => img.src.startsWith('http')).map(img => {
  55. const dim = img.getBoundingClientRect()
  56. return {
  57. img,
  58. size: dim.width + dim.height
  59. }
  60. }).sort(o => o.size).map(o => o.img.src).pop()
  61. if (biggestImage) {
  62. return biggestImage
  63. }
  64. }
  65. return document.location.href
  66. }
  67.  
  68. function google () {
  69. /* Google reverse image search */
  70. GM_openInTab('https://www.google.com/imghp?sbi=1#habibi=' + encodeURIComponent(getUrl()), { active: true })
  71. }
  72.  
  73. function tinEye () {
  74. /* TinEye reverse image search */
  75. GM_openInTab('https://tineye.com/search?url=' + encodeURIComponent(getUrl()))
  76. }
  77. function yandex () {
  78. /* Yandex reverse image search */
  79. GM_openInTab('https://yandex.com/images/search?rpt=imageview&url=' + encodeURIComponent(getUrl()))
  80. }
  81. function sauceNAO () {
  82. /* SauceNAO reverse image search */
  83. GM_openInTab('https://saucenao.com/search.php?url=' + encodeURIComponent(getUrl()))
  84. }
  85. function iqdb () {
  86. /* iqdb.org reverse image search */
  87. GM_openInTab('http://iqdb.org/?url=' + encodeURIComponent(getUrl()))
  88. }
  89.  
  90. GM_registerMenuCommand('Reverse Image search - Google', google, 'g')
  91. GM_registerMenuCommand('Reverse Image search - TinEye', tinEye, 'q')
  92. GM_registerMenuCommand('Reverse Image search - Yandex', yandex, 'y')
  93. GM_registerMenuCommand('Reverse Image search - SauceNAO', sauceNAO, 'x')
  94. GM_registerMenuCommand('Reverse Image search - iqdb.org', iqdb, ',')
  95.  
  96. if (document && document.body && document.body.firstChild.tagName === 'IMG') {
  97. document.addEventListener('keydown', onKeyDown('g', google))
  98. document.addEventListener('keydown', onKeyDown('q', tinEye))
  99. document.addEventListener('keydown', onKeyDown('y', yandex))
  100. document.addEventListener('keydown', onKeyDown('x', sauceNAO))
  101. document.addEventListener('keydown', onKeyDown(',', iqdb))
  102. } else if (document.location.href.startsWith('/imghp') !== -1 && document.location.hash.startsWith('#habibi=')) {
  103. // Enter url into Google search form
  104. window.setTimeout(() => {
  105. document.querySelector('[role="button"]>img[src]').click()
  106. window.setTimeout(() => {
  107. document.querySelector('input[text=text]').value = decodeURIComponent(document.location.hash.substring(8))
  108. document.querySelector('input[text=text]').nextElementSibling.click()
  109. }, 500)
  110. }, 500)
  111. }
  112. })()