Created
November 18, 2020 13:33
-
-
Save Tnifey/c7279322eacb3fbb7d7e4774f10b35b8 to your computer and use it in GitHub Desktop.
[tempermonkey][userscript] paste to preview image on google images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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