Advertisement
Guest User

Untitled

a guest
Apr 8th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Image Download and Search
  3. // @description Adds a download icon and a search by image icon to images larger than 250x250 pixels
  4. // @namespace
  5. // @include *
  6. // @grant GM_openInTab
  7. // ==/UserScript==
  8.  
  9. (function() {
  10. 'use strict';
  11.  
  12. var minImageSize = 250;
  13.  
  14. // Add icons to images larger than minImageSize
  15. var images = document.getElementsByTagName('img');
  16. for (var i = 0; i < images.length; i++) {
  17. var img = images[i];
  18. if (img.width >= minImageSize && img.height >= minImageSize) {
  19. addDownloadIcon(img);
  20. addSearchIcon(img);
  21. }
  22. }
  23.  
  24. // Add download icon to the image
  25. function addDownloadIcon(img) {
  26. var downloadIcon = document.createElement('div');
  27. downloadIcon.classList.add('download-icon');
  28. img.parentNode.insertBefore(downloadIcon, img.nextSibling);
  29. downloadIcon.onclick = function() {
  30. downloadImage(img.src);
  31. };
  32. }
  33.  
  34. // Download the image
  35. function downloadImage(src) {
  36. var link = document.createElement('a');
  37. link.href = src;
  38. link.download = '';
  39. document.body.appendChild(link);
  40. link.click();
  41. document.body.removeChild(link);
  42. }
  43.  
  44. // Add search icon to the image
  45. function addSearchIcon(img) {
  46. var searchIcon = document.createElement('div');
  47. searchIcon.classList.add('search-icon');
  48. searchIcon.onclick = function(e) {
  49. e.preventDefault();
  50. GM_openInTab('https://yandex.com/images/search?rpt=imageview&url=' + encodeURIComponent(img.src), { active:
  51.  
  52. false, insert: true });
  53. };
  54. img.parentNode.insertBefore(searchIcon, img.nextSibling);
  55. }
  56.  
  57. // CSS styles for the icons
  58. var css = `
  59. /* Download icon */
  60. .download-icon {
  61. position: absolute;
  62. top: 10px;
  63. right: 10px;
  64. width: 25px;
  65. height: 25px;
  66. background-image: url('https://iili.io/Hk2uZla.png');
  67. background-size: cover;
  68. opacity: 0.5;
  69. cursor: pointer;
  70. }
  71.  
  72. /* Search icon */
  73. .search-icon {
  74. position: absolute;
  75. top: 40px;
  76. right: 10px;
  77. width: 25px;
  78. height: 25px;
  79. background-image: url('https://i.imgur.com/LiueXkw.png');
  80. background-size: cover;
  81. opacity: 0.5;
  82. cursor: pointer;
  83. }
  84. `;
  85.  
  86. // Add the CSS styles to the page
  87. var style = document.createElement('style');
  88. style.type = 'text/css';
  89. style.appendChild(document.createTextNode(css));
  90. document.head.appendChild(style);
  91.  
  92. })();
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement