Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Image Download and Search
- // @description Adds a download icon and a search by image icon to images larger than 250x250 pixels
- // @namespace
- // @include *
- // @grant GM_openInTab
- // ==/UserScript==
- (function() {
- 'use strict';
- var minImageSize = 250;
- // Add icons to images larger than minImageSize
- var images = document.getElementsByTagName('img');
- for (var i = 0; i < images.length; i++) {
- var img = images[i];
- if (img.width >= minImageSize && img.height >= minImageSize) {
- addDownloadIcon(img);
- addSearchIcon(img);
- }
- }
- // Add download icon to the image
- function addDownloadIcon(img) {
- var downloadIcon = document.createElement('div');
- downloadIcon.classList.add('download-icon');
- img.parentNode.insertBefore(downloadIcon, img.nextSibling);
- downloadIcon.onclick = function() {
- downloadImage(img.src);
- };
- }
- // Download the image
- function downloadImage(src) {
- var link = document.createElement('a');
- link.href = src;
- link.download = '';
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
- // Add search icon to the image
- function addSearchIcon(img) {
- var searchIcon = document.createElement('div');
- searchIcon.classList.add('search-icon');
- searchIcon.onclick = function(e) {
- e.preventDefault();
- GM_openInTab('https://yandex.com/images/search?rpt=imageview&url=' + encodeURIComponent(img.src), { active:
- false, insert: true });
- };
- img.parentNode.insertBefore(searchIcon, img.nextSibling);
- }
- // CSS styles for the icons
- var css = `
- /* Download icon */
- .download-icon {
- position: absolute;
- top: 10px;
- right: 10px;
- width: 25px;
- height: 25px;
- background-image: url('https://iili.io/Hk2uZla.png');
- background-size: cover;
- opacity: 0.5;
- cursor: pointer;
- }
- /* Search icon */
- .search-icon {
- position: absolute;
- top: 40px;
- right: 10px;
- width: 25px;
- height: 25px;
- background-image: url('https://i.imgur.com/LiueXkw.png');
- background-size: cover;
- opacity: 0.5;
- cursor: pointer;
- }
- `;
- // Add the CSS styles to the page
- var style = document.createElement('style');
- style.type = 'text/css';
- style.appendChild(document.createTextNode(css));
- document.head.appendChild(style);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement