-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwayfarer-prime-portal-links.user.js
125 lines (114 loc) · 5.3 KB
/
wayfarer-prime-portal-links.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
// ==UserScript==
// @name Wayfarer Prime Portal Links
// @version 0.1.0
// @description Add links to open Showcase and Nearby portals in Ingress Prime
// @namespace https://github.com/tehstone/wayfarer-addons
// @downloadURL https://github.com/tehstone/wayfarer-addons/raw/main/wayfarer-prime-portal-links.user.js
// @homepageURL https://github.com/tehstone/wayfarer-addons
// @match https://wayfarer.nianticlabs.com/*
// ==/UserScript==
// Copyright 2024 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() {
/**
* Overwrite the open method of the XMLHttpRequest.prototype to intercept the server calls
*/
(function (open) {
XMLHttpRequest.prototype.open = function (method, url) {
if (method == 'GET') {
let callback = null;
switch (url) {
case '/api/v1/vault/review':
callback = injectReview;
break;
}
if (callback) this.addEventListener('load', () => checkResponse(this.response, callback), 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 injectReview = result => awaitElement(() => (
document.querySelector('app-should-be-wayspot') ||
document.querySelector('app-review-edit') ||
document.querySelector('app-review-photo')
)).then(ref => {
switch (ref.tagName) {
case 'APP-SHOULD-BE-WAYSPOT':
awaitElement(() => document.querySelector('#location-accuracy-card nia-map'))
.then((ref) => {
addNearbyClickListeners();
});
break;
}
});
const addOpenButtons = (before, portal) => {
console.log(portal);
}
const addNearbyClickListeners = () => {
awaitElement(() => document.querySelector('div.w-full'))
.then(() => {
document.querySelectorAll('img.cursor-pointer').forEach((el) => {
el.addEventListener('click', (el) => {
const nearbyWaySpots = el.target.parentElement['__ngContext__'][23];
if (nearbyWaySpots !== null && nearbyWaySpots !== undefined) {
const output = nearbyWaySpots.filter(function(obj) {
return obj['infoWindowComponentData']['title'] === el.target.alt;
});
if (output.length === 1) {
const guid = output[0].id;
const lat = output[0].latitude;
const lng = output[0].longitude;
let nearbyBox = document.querySelector('.gm-style-iw-d > div:nth-child(1) > div:nth-child(1)');
if (nearbyBox !== null && nearbyBox !== undefined) {
const linkDiv = document.createElement('div');
linkDiv.classList.add('font-medium');
const link = document.createElement('a');
link.href = `https://link.ingress.com/?link=https%3a%2f%2fintel.ingress.com%2Fportal%2f${guid}&apn=com.nianticproject.ingress&isi=576505181&ibi=com.google.ingress&ifl=https%3a%2f%2fapps.apple.com%2fapp%2fingress%2fid576505181&ofl=https%3a%2f%2fintel.ingress.com%2fintel%3fpll%3d${lat}%2c${lng}`;
link.target = 'wayfareropenin';
link.textContent = el.target.alt;
linkDiv.appendChild(link);
nearbyBox.insertBefore(linkDiv, nearbyBox.children[0]);
nearbyBox.removeChild(nearbyBox.children[1]);
}
}
}
});
});
});
}
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();
});
})();