Google Image Search on Ptt

Add Google image search buttons on Ptt.

  1. // ==UserScript==
  2. // @name Google Image Search on Ptt
  3. // @namespace https://wiki.gslin.org/wiki/GoogleImageSearchOnPtt
  4. // @version 0.20231226.0
  5. // @description Add Google image search buttons on Ptt.
  6. // @author Gea-Suan Lin <darkkiller@gmail.com>
  7. // @match https://www.ptt.cc/bbs/*/*.html
  8. // @grant GM_openInTab
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function ev() {
  16. let img_re = new RegExp('\.(gif|jpeg|jpg|png|webp)$');
  17. let timeout = 0;
  18.  
  19. document.querySelectorAll('#main-content img').forEach(function(el) {
  20. const img_url = el.getAttribute('href') || el.getAttribute('src');
  21. if (!img_url.match(img_re)) {
  22. return;
  23. }
  24.  
  25. let url = 'https://lens.google.com/uploadbyurl?url=' + encodeURIComponent(img_url);
  26. setTimeout(function() {
  27. GM_openInTab(url, true);
  28. }, timeout);
  29. timeout += 1500;
  30. });
  31. };
  32.  
  33. let btn = document.createElement('button');
  34. btn.addEventListener('click', ev);
  35. btn.innerHTML = 'Google Image Search';
  36. btn.setAttribute('style', 'height: 1.5em; margin-bottom: 3px; vertical-align: text-bottom;');
  37. document.querySelectorAll('.article-metaline')[2].appendChild(btn);
  38. })();