forked from tehstone/wayfarer-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwayfarer-nomination-streetview.user.js
193 lines (172 loc) · 7.14 KB
/
wayfarer-nomination-streetview.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// ==UserScript==
// @name Wayfarer Nomination Streetview
// @version 0.3.9
// @description Add Streetview to selected nomination
// @namespace https://github.com/tehstone/wayfarer-addons/
// @downloadURL https://github.com/tehstone/wayfarer-addons/raw/main/wayfarer-nomination-streetview.user.js
// @homepageURL https://github.com/tehstone/wayfarer-addons/
// @match https://wayfarer.nianticlabs.com/*
// ==/UserScript==
// Copyright 2022 tehstone
// 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" */
(function() {
let tryNumber = 10;
const nomCache = {};
let intelLink = null;
/**
* 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/manage' && method == 'GET') {
this.addEventListener('load', parseNominations, false);
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
function parseNominations() {
tryNumber = 10;
const response = this.response;
const json = JSON.parse(response);
if (!json) {
console.log('Failed to parse response from Wayfarer');
return;
}
// ignore if it's related to captchas
if (json.captcha) return;
if (!json.result) {
console.log('Wayfarer\'s response didn\'t include candidates.');
return;
}
json.result.nominations.forEach(nomination => { nomCache[nomination.imageUrl] = nomination; })
const list = document.getElementsByTagName('app-nominations-list')[0];
list.addEventListener('click', handleNominationClick);
}
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();
});
function handleNominationClick(e) {
awaitElement(() => e.target.closest('app-nominations-list-item'))
.then((ref) => {
const nom = nomCache[ref.querySelector('img').src];
addStreetView(nom);
addCoordinates(nom);
const matCnt = document.querySelector('mat-sidenav-content');
const evtFunc= () => {
document.querySelector('.wf-page-header__title > div:nth-child(1)').scrollIntoView();
matCnt.removeEventListener('scroll',evtFunc);
};
matCnt.addEventListener('scroll', evtFunc);
});
}
function addCoordinates(selected) {
const { lat, lng, city, state } = selected;
awaitElement(() => document.querySelector("app-nominations app-details-pane p"))
.then((locationP) => {
const coordinates = `${lat},${lng}`;
const newText = `${city} ${state} (${coordinates})`;
locationP.innerText = newText;
locationP.style.cursor = 'pointer';
locationP.title = 'Copy coordinates to clipboard';
locationP.onclick = function() {
navigator.clipboard.writeText(coordinates);
}
});
awaitElement(() => document.querySelector("app-nominations app-details-pane h4"))
.then((titleP) => {
if (intelLink === null) {
intelLink = document.createElement('a');
intelLink.id = 'intelLink';
intelLink.className = 'anchor-link';
intelLink.target = "_blank";
intelLink.title = 'Open in Intel';
intelLink.style['font-size'] = "1.25rem";
}
intelLink.href = `https://intel.ingress.com/?ll=${lat},${lng}&z=16`;
intelLink.innerText = titleP.innerText;
insertAfter(intelLink, titleP);
titleP.style.display = "none";
});
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function addStreetView(selected) {
if (typeof(google) === 'undefined') {
setTimeout(addStreetView, 100, selected);
return;
}
const ref = document.querySelector('wf-page-header');
if (!ref) {
if (tryNumber === 0) {
alert('Nomination Street View initialization failed, please refresh the page');
return;
}
setTimeout(addStreetView, 300, selected);
tryNumber--;
return;
}
if (document.getElementById("pano") === null){
let lastPane = document.getElementsByClassName("details-pane__map")[0];
if (lastPane === undefined){
console.err("[WF-NSV] Failed to find attach elem");
return;
}
let SVMapElement = document.createElement("div");
SVMapElement.id = "pano";
SVMapElement.style.height = "480px";
SVMapElement.style.marginTop = "10px";
lastPane.parentElement.insertBefore(SVMapElement, lastPane.nextSibling);
}
const { lat, lng, title } = selected;
const SVMap = new google.maps.Map(document.getElementById("pano"), {
center: { lat, lng },
mapTypeId: "hybrid",
zoom: 17,
scaleControl: true,
scrollwheel: true,
gestureHandling: 'greedy',
mapTypeControl: false
});
const marker = new google.maps.Marker({
map: SVMap,
position: { lat, lng },
title
});
const client = new google.maps.StreetViewService;
client.getPanoramaByLocation({ lat, lng }, 50, function(result, status) {
if (status === "OK") {
const nomLocation = new google.maps.LatLng(lat, lng);
const svLocation = result.location.latLng;
const heading = google.maps.geometry.spherical.computeHeading(svLocation, nomLocation);
const panorama = SVMap.getStreetView();
panorama.setPosition(svLocation);
panorama.setPov({ heading, pitch: 0, zoom: 1 });
panorama.setMotionTracking(false);
panorama.setVisible(true);
}
});
}
})();