-
Notifications
You must be signed in to change notification settings - Fork 20
/
wayfarer-upgrade-percent.user.js
148 lines (128 loc) · 4.69 KB
/
wayfarer-upgrade-percent.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
// ==UserScript==
// @name Wayfarer Upgrade Percentage
// @version 0.1.1
// @description Add local review history storage to Wayfarer
// @namespace https://github.com/tehstone/wayfarer-addons
// @downloadURL https://github.com/tehstone/wayfarer-addons/raw/main/wayfarer-upgrade-percentage.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 init() {
let tryNumber = 10;
let properties;
let percentTextDiv = null;
let rewardProgress = 0;
/**
* 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/properties' && method == 'GET') {
this.addEventListener('load', parseProps, false);
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
(function (open) {
XMLHttpRequest.prototype.open = function (method, url) {
if (url == '/api/v1/vault/review') {
if (method == 'GET') {
this.addEventListener('load', displayPercentage, false);
}
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
function parseProps(e) {
tryNumber = 10;
try {
const response = this.response;
const json = JSON.parse(response);
if (!json) {
console.log('Wayfarer Upgrade Percentage: Failed to parse response from Wayfarer');
return;
}
// ignore if it's related to captchas
if (json.captcha)
return;
properties = json.result;
if (!properties) {
console.log('Wayfarer Upgrade Percentage: Wayfarer\'s response didn\'t include a properties.');
return;
}
checkPageStatus();
} catch (e) {
console.log(e); // eslint-disable-line no-console
}
}
function checkPageStatus() {
const ref = document.querySelector('wf-upgrade-visualization');
if (!ref) {
if (tryNumber === 0) {
console.log('Wayfarer Upgrade Percentage:WF Percent Display initialization failed, please refresh the page');
return;
}
setTimeout(checkPageStatus, 250);
tryNumber--;
return;
}
displayPercentage(ref);
}
function displayPercentage() {
const ref = document.querySelector('wf-upgrade-visualization');
const circleImage = ref.parentNode;
const topRow = circleImage.parentNode;
let container = document.createElement("div");
container.classList.add("flex");
container.classList.add("flex-row");
if (percentTextDiv === null) {
percentTextDiv = document.createElement("div");
}
percentTextDiv.style.margin = "auto";
percentTextDiv.innerText = `${properties['rewardProgress']}%`;
container.appendChild(circleImage);
container.appendChild(percentTextDiv);
topRow.appendChild(container);
percentTextDiv.onclick = function() {
var http = new XMLHttpRequest();
var url = "https://wayfarer.nianticlabs.com/api/v1/vault/properties";
http.open('GET', url, true);
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
try {
const json = JSON.parse(http.responseText);
if (!json) {
console.log('Wayfarer Upgrade Percentage:Failed to parse response from Wayfarer');
return;
}
properties = json.result;
if (properties) {
percentTextDiv.innerText = `${properties['rewardProgress']}%`;
}
} catch (e) {}
}
}
http.send();
}
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
}
init();