-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwayfarer-reverse-image-search.user.js
143 lines (128 loc) · 5.41 KB
/
wayfarer-reverse-image-search.user.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// ==UserScript==
// @name Wayfarer Reverse Image Search
// @version 0.4.1
// @description Add reverse image search links to Wayfarer
// @namespace https://github.com/tehstone/wayfarer-addons
// @downloadURL https://github.com/tehstone/wayfarer-addons/raw/main/wayfarer-reverse-image-search.user.js
// @homepageURL https://github.com/tehstone/wayfarer-addons
// @match https://wayfarer.nianticlabs.com/*
// ==/UserScript==
// Copyright 2024 tehstone, bilde
// This file is part of the Wayfarer Addons collection.
// This script is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This script is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You can find a copy of the GNU General Public License in the root
// directory of this script's GitHub repository:
// <https://github.com/tehstone/wayfarer-addons/blob/main/LICENSE>
// If not, see <https://www.gnu.org/licenses/>.
/* eslint-env es6 */
/* eslint no-var: "error" */
/* eslint indent: ['error', 2] */
(function() {
/**
* Overwrite the open method of the XMLHttpRequest.prototype to intercept the server calls
*/
(function (open) {
XMLHttpRequest.prototype.open = function (method, url) {
if (url == '/api/v1/vault/review') {
if (method == 'GET') {
this.addEventListener('load', () => checkResponse(this.response, checkReviewType), false);
}
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
const checkResponse = (response, callback) => {
try {
const json = JSON.parse(response);
if (!json) return;
if (json.captcha || !json.result) return;
callback(json.result);
} catch (e) {
console.log(e); // eslint-disable-line no-console
}
}
const awaitElement = get => new Promise((resolve, reject) => {
let triesLeft = 10;
const queryLoop = () => {
const ref = get();
if (ref) resolve(ref);
else if (!triesLeft) reject();
else setTimeout(queryLoop, 100);
triesLeft--;
}
queryLoop();
});
const checkReviewType = result => awaitElement(() => (
document.getElementById('check-duplicates-card') ||
document.querySelector('app-review-photo') ||
document.querySelector('app-review-edit')
)).then(ref => {
console.log("checkReviewType")
switch (ref.tagName) {
case 'WF-REVIEW-CARD':
awaitElement(() => document.querySelector('#check-duplicates-card nia-map'))
.then((ref) => {
addImageSearchLinks(ref, result);
});
break;
case 'APP-REVIEW-PHOTO':
awaitElement(() => ref.querySelector('.review-photo__info > div > div:nth-child(2)'))
.then((ref) => {
addPhotoReviewLinks(ref, result);
});
break;
default:
break;
}
});
const addImageSearchLinks = (before, data) => {
const mainUrl = encodeURIComponent(`${data['imageUrl']}`);
const mainSearchUrl = `https://lens.google.com/uploadbyurl?url=${mainUrl}`
addLink(mainSearchUrl, 'app-photo-b.nomination-info-review-card > wf-review-card-b:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2)');
const supportingUrl = encodeURIComponent(`${data['supportingImageUrl']}`);
const supportingSearchUrl = `https://lens.google.com/uploadbyurl?url=${supportingUrl}`
addLink(supportingSearchUrl, 'app-supporting-info-b.nomination-info-review-card > wf-review-card-b:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2)');
}
const addLink = (url, selector) => {
console.log("addLink ")
const insertBeforeEl = document.querySelector(selector);
if (insertBeforeEl !== null && insertBeforeEl !== undefined) {
const linkSpan = document.createElement('span');
const link = document.createElement('a');
link.href = url;
link.target = 'wayfareropenin';
link.textContent = 'Reverse Image Search';
linkSpan.appendChild(link);
insertAfter(linkSpan, insertBeforeEl);
}
}
const addPhotoReviewLinks = (before, data) => {
for (let i = 1; i < 27; i++) {
const photoEl = document.querySelector(`app-photo-card.ng-star-inserted:nth-child(${i}) > div:nth-child(1) > div:nth-child(2)`);
if (photoEl === null || photoEl === undefined) {
break;
}
const url = encodeURIComponent(`${data['newPhotos'][i-1]['value']}`);
console.log("here")
const searchUrl = `https://lens.google.com/uploadbyurl?url=${url}`
const linkSpan = document.createElement('span');
const link = document.createElement('a');
link.href = searchUrl;
link.target = 'wayfareropenin';
link.textContent = 'Reverse Image Search';
//link.preventDefault();
linkSpan.appendChild(link);
photoEl.insertBefore(linkSpan, photoEl.children[0]);
}
}
const insertAfter = (newNode, referenceNode) => {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
})();