-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinventory-anomalyhub-updater.user.js
More file actions
207 lines (187 loc) · 8.53 KB
/
inventory-anomalyhub-updater.user.js
File metadata and controls
207 lines (187 loc) · 8.53 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ==UserScript==
// @name Anomaly Update Ingress Inventory
// @id guschtel@inventory-update
// @description Update inventory script
// @author Guschtel
// @namespace http://tampermonkey.net/
// @category Utilities
// @version 0.9
// @match https://*.willbe.blue/inventory/update
// @icon https://www.google.com/s2/favicons?sz=64&domain=willbe.blue
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
console.log('Running Anomaly Update Ingress Inventory script! -------------------------------------');
function to_number(val) {
if (val === null || val === undefined) {
return 0;
}
return +(val.toString().replace(/\r/g, ''));
}
function getOtherBeacons(inventory) {
let sum = 0;
for (const [key, value] of Object.entries(inventory)) {
if (key.startsWith("Powerup") && !key.startsWith("Powerup BB_BATTLE_RARE")) {
sum += +value;
}
}
return sum;
}
function setFormValue(formKey, formValue) {
const elements = document.getElementsByName(formKey);
if (elements !== undefined && elements.length > 0) {
elements[0].value = formValue;
}
}
function isiOS() {
return (/iPad|iPhone|iPod|Macintosh|Mac OS X/.test(navigator.userAgent) && !window.MSStream);
}
function isFirefox() {
return (/Firefox/i.test(navigator.userAgent));
}
function getClipboardContentFromDialog() {
return new Promise((resolve, reject) => {
const dialog = document.createElement('dialog');
const description = document.createElement('p');
const textarea = document.createElement('textarea');
const okButton = document.createElement('button');
const cancelButton = document.createElement('button');
dialog.style.padding = '1em';
description.textContent = 'The clipboard could not be accessed in your browser. Please paste your inventory into the textarea below.';
textarea.style.width = '400px';
textarea.style.height = '200px';
textarea.style.marginBottom = '1em';
okButton.textContent = 'OK';
cancelButton.textContent = 'Cancel';
okButton.style.marginRight = '1em';
dialog.appendChild(description);
dialog.appendChild(textarea);
dialog.appendChild(document.createElement('br'));
dialog.appendChild(okButton);
dialog.appendChild(cancelButton);
okButton.onclick = () => {
dialog.close();
resolve(textarea.value);
};
cancelButton.onclick = () => {
dialog.close();
reject(new Error('Dialog for inserting clipboard content cancelled'));
};
document.body.appendChild(dialog);
dialog.showModal();
});
}
function getClipboardContentForIOs() {
console.log("iOs: Read clipboard start");
if (!navigator.clipboard?.readText) {
console.log("iOs: Read clipboard content from dialog bc navigator.clipboard.readText is not available");
return getClipboardContentFromDialog();
}
console.log("iOs: Read clipboard normally");
return navigator.clipboard.readText();
}
function getClipboardContentForNonIOs() {
try {
if (isFirefox()) {
return navigator.clipboard.readText();
}
// Chrome
return navigator.permissions.query({name: "clipboard-read"}).then(
async (result) => {
if (result.state === "granted" || result.state === "prompt") {
return await navigator.clipboard.readText();
} else {
return getClipboardContentFromDialog();
}
},
(error) => {
if (error.message.includes("clipboard-read")) {
return getClipboardContentFromDialog();
}
throw error;
}
);
} catch (e) {
console.error('Failed to read clipboard contents in catch: ', e);
return getClipboardContentFromDialog();
}
}
const updateBtn = document.getElementsByClassName("btn btn-primary")[0];
let copyCPbtn = document.createElement("button");
copyCPbtn.innerHTML = "Copy from Clipboard";
copyCPbtn.className = "btn btn-primary";
copyCPbtn.style = "margin-left: 1em;";
function parseClipboardContent(text) {
console.log('Pasted content: ', text);
if (text.includes('Type Rarity Count')) {
const lines = text.split("\n");
const inventory = {};
for (var line = 1; line < lines.length; line++) {
// By tabs
var tabs = lines[line].split("\t");
if (tabs[0] === "Shield" || tabs[0] === "HS" || tabs[0] === "Multi-Hack") {
inventory[tabs[0] + " " + tabs[1]] = tabs[2];
} else {
inventory[tabs[0]] = tabs[2];
}
}
console.log('Inventory: ', inventory);
setFormValue("inventory[X8]", to_number(inventory["XMP 8"]));
setFormValue("inventory[U8]", to_number(inventory["US 8"]));
setFormValue("inventory[JARVIS]", to_number(inventory["Virus JARVIS"]));
setFormValue("inventory[ADA]", to_number(inventory["Virus ADA"]));
setFormValue("inventory[R8]", to_number(inventory["Resonator 8"]));
setFormValue("inventory[R7]", to_number(inventory["Resonator 7"]));
setFormValue("inventory[R6]", to_number(inventory["Resonator 6"]));
setFormValue("inventory[R5]", to_number(inventory["Resonator 5"]));
setFormValue("inventory[R4]", to_number(inventory["Resonator 4"]));
setFormValue("inventory[Aegis]", to_number(inventory["Aegis Shield"]));
setFormValue("inventory[VR Shields]", to_number(inventory["Shield VERY_RARE"]));
setFormValue("inventory[C8]", to_number(inventory["PC 8"]));
setFormValue("inventory[Hypercubes]", to_number(inventory["Hypercube"]));
setFormValue("inventory[LPC]", to_number(inventory["Hypercube"]));
setFormValue("inventory[VR Battle Beacons]", to_number(inventory["Powerup BB_BATTLE"]));
setFormValue("inventory[Other Beacons]", to_number(getOtherBeacons(inventory)));
setFormValue("inventory[VRHS]", to_number(inventory["HS VERY_RARE"]));
setFormValue("inventory[VRMH]", to_number(inventory["Multi-Hack VERY_RARE"]));
setFormValue("inventory[SBUL]", to_number(inventory["Ultra-Link"]));
setFormValue("inventory[FRK]", to_number(inventory["Powerup FRACK"]));
setFormValue("inventory[Caps]", to_number(inventory["Capsule"]));
const utc = new Date().toJSON();
const commentElement = document.getElementsByName("inventory[_notes]")[0];
if (!commentElement.value.includes("Inventory automatically filled with Guschtels Inventory userscripts")) {
commentElement.value += "\nInventory automatically filled with Guschtels Inventory userscripts (https://github.com/Guschtel/public-iitc-scripts/tree/main) on " + utc;
} else {
const regex = /on [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.*Z/i;
commentElement.value = commentElement.value.replace(regex, 'on ' + utc);
}
} else {
console.error('Invalid clipboard content');
}
}
if (isiOS()) {
console.log('iOs detected');
copyCPbtn.onclick = async (event)=> {
event.preventDefault();
getClipboardContentFromDialog().then(text => {
parseClipboardContent(text);
})
}
} else {
console.log('Proceeding with non-iOs');
copyCPbtn.onclick = async (event)=> {
event.preventDefault();
getClipboardContentForNonIOs()
.then(text => {
parseClipboardContent(text);
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
return false;
}
}
updateBtn.parentNode.insertBefore(copyCPbtn, updateBtn.nextSibling);
})();