Skip to content

Instantly share code, notes, and snippets.

@Tnifey
Created November 18, 2020 13:33
Show Gist options
  • Save Tnifey/c7279322eacb3fbb7d7e4774f10b35b8 to your computer and use it in GitHub Desktop.
Save Tnifey/c7279322eacb3fbb7d7e4774f10b35b8 to your computer and use it in GitHub Desktop.
[tempermonkey][userscript] paste to preview image on google images
// ==UserScript==
// @name Search Image from clipboard
// @namespace https://github.com/tnifey
// @version 0.1
// @description Google Images - Search By Paste Image from clipboard
// @author Tnifey
// @match https://images.google.com/*
// @match https://google.com/imghp
// @match https://google.com/imghp*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener("paste", (ev) => {
searchFromClipboard(ev);
});
let preview = null;
async function searchFromClipboard(ev) {
console.log('file');
const preview = getPreview();
const dT = ev.clipboardData || window.clipboardData;
const file = dT.files[0];
console.log("search by paste", file);
if(file) {
const url = URL.createObjectURL(file);
console.log("file");
preview.addEventListener('load', (ev) => {
URL.revokeObjectURL(url);
}, { once: true });
preview.src = url;
}
else {
if(preview) {
preview.remove();
preview = null;
}
}
}
function getPreview() {
if(!preview) {
preview = document.createElement("img");
preview.classList.add("preview");
preview.style.width = '200px';
preview.style.height = '200px';
preview.style.border = "none";
preview.style.position = 'fixed';
preview.style.objectFit = 'contain';
preview.style.backgroundColor = 'tomato';
preview.style.bottom = '2rem';
preview.style.right = '2rem';
preview.style.zIndex = '9001';
document.body.append(preview);
}
return preview;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment