-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paste Image into Google Image Search.js
58 lines (51 loc) · 1.91 KB
/
Paste Image into Google Image Search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// ==UserScript==
// @name Paste Image into Google Image Search
// @version 1.1
// @description Performs a reverse Google Image Search with the image from the clipboard.
// @author Kartik Soneji
// @icon https://images.google.com/favicon.ico
// @license GPL-3.0-or-later
// @match https://www.google.com/search*tbm=isch*
// @match https://www.google.com/imghp*
// @match https://images.google.com*
// @grant none
// @run-at document-start
// @supportURL https://gitlab.com/KartikSoneji/userscripts/issues
// @updateURL https://openuserjs.org/meta/KartikSoneji/Paste_Image_into_Google_Image_Search.meta.js
// @downloadURL https://openuserjs.org/install/KartikSoneji/Paste_Image_into_Google_Image_Search.user.js
// ==/UserScript==
(() => {
window.addEventListener("load", e => {
//Show and hide the "Search by image" box
window.google.qb.tp();
window.google.qb.tp();
document.querySelector("input[title = Search]").addEventListener("paste", handlePasteEvent);
document.querySelector("[name = image_url]").addEventListener("paste", handlePasteEvent)
});
function handlePasteEvent(e){
getImageURLFromPasteEvent(e).then(url => searchByImageURL(url)).catch(e => e);
}
function searchByImageURL(url){
document.querySelector("[name = image_url]").value = url;
document.querySelector("[value = 'Search by image']").click();
}
function getImageURLFromPasteEvent(e){
for(let i of e.clipboardData.items)
if(i.type.indexOf("image") > -1)
return getBase64(i.getAsFile());
throw "No Image on clipboard";
}
async function getBase64(file){
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
let encoded = reader.result.replace(/^data:(.*;base64,)?/, '');
if(encoded.length % 4 > 0)
encoded += '='.repeat(4 - (encoded.length % 4));
resolve(reader.result);
};
reader.onerror = reject;
});
}
})();