Created
May 7, 2022 21:07
-
-
Save guest271314/abef8078d88b9b4163eeae5e49f7a78e to your computer and use it in GitHub Desktop.
Google Images Search with Ctrl+V
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
// https://twitter.com/ericlaw/status/1521939256574423044 | |
document.onpaste = async (e) => { | |
async function handlePaste(file) { | |
const url = 'https://www.google.com/searchbyimage/upload'; | |
const fd = new FormData(); | |
fd.append('hl', navigator.languages[navigator.languages.length - 1]); | |
fd.append('filename', file.name); | |
// fd.append('image_content', file, file.name); | |
fd.append('encoded_image', file, file.name); | |
const request = await fetch(url, { | |
method: 'post', | |
body: fd, | |
}); | |
const results = await request.text(); | |
iframe = document.createElement('iframe'); | |
iframe.width = screen.width / 2; | |
iframe.height = screen.height / 2; | |
iframe.srcdoc = results; | |
iframe.style.bottom = 0; | |
iframe.style.right = 0; | |
iframe.style.position = 'absolute'; | |
iframe.style.display = 'block'; | |
document.body.appendChild(iframe); | |
} | |
try { | |
const { files } = e.clipboardData; | |
// const ([items] = await navigator.clipboard.read()); | |
// console.log(items, item.types); | |
// const file = await items.getType('image/png'); | |
for (const file of files) { | |
if (/^image/.test(file.type)) { | |
handlePaste(file); | |
break; | |
} | |
} | |
} catch (err) { | |
console.log(err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment