-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.user.js
318 lines (277 loc) · 7.9 KB
/
request.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// ==UserScript==
// @name EliRequest
// @namespace https://politicsandwar.com/nation/id=97917
// @version 0.1
// @description adds a form to auto parse requests
// @author Eligos
// @match https://politicsandwar.com/alliance/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=politicsandwar.com
// @grant none
// ==/UserScript==
function setValue(name, value) {
let inputElement;
if (name === "withtype") {
inputElement = document.querySelector(`select[name="${name}"]`);
} else {
inputElement = document.querySelector(`input[name="${name}"]`);
}
if (inputElement) {
inputElement.value = value;
}
}
let debounceTimeout;
function debounce(func, delay) {
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(func, delay);
}
const apiUrl = 'https://api.politicsandwar.com/graphql?api_key=bce6428a6a9a385eb9c3';
async function getName(id) {
const query = `{
nations(first: 1, id: ${id}) {
data {
nation_name
}
}
}`;
return fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query }),
})
.then(response => response.json())
.then(data => {
if (!data.data.nations.data[0]) {
return;
}
const name = data.data.nations.data[0].nation_name;
return name;
})
.catch(error => {
console.error('Error:', error);
return;
});
}
async function getIdByName(name) {
const query = `{
nations(first: 1, nation_name: "${name}") {
data {
id
}
}
}`;
return fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query }),
})
.then(response => response.json())
.then(data => {
if (!data.data.nations.data[0]) {
return;
}
const id = data.data.nations.data[0].nationid;
return id;
})
.catch(error => {
console.error('Error:', error);
return;
});
}
async function getIdByLeader(name) {
const query = `{
nations(first: 1, leader_name: "${name}") {
data {
id
}
}
}`;
return fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query }),
})
.then(response => response.json())
.then(data => {
if (!data.data.nations.data[0]) {
return;
}
const id = data.data.nations.data[0].nationid;
return id;
})
.catch(error => {
console.error('Error:', error);
return;
});
}
(async function () {
"use strict";
const container = document.createElement('container');
container.style.display = 'flex';
container.style.alignItems = 'center';
const textarea = document.createElement('textarea');
textarea.placeholder = 'Paste request here...';
textarea.rows = '5';
textarea.cols = '40';
textarea.name = 'withrequest';
textarea.style.display = 'block';
textarea.style["margin-left"] = '15px';
textarea.style.width = '80%';
textarea.style.resize = 'vertical';
textarea.style["margin-bottom"] = '20px';
const separator = document.createElement('input');
separator.type = 'text';
separator.placeholder = 'Separator';
separator.style.margin = '0 auto';
separator.style["margin-bottom"] = '20px';
separator.style["margin-right"] = '20px';
separator.style["margin-left"] = '10px';
separator.style.flex = '1';
container.appendChild(textarea);
container.appendChild(separator);
async function updateArray() {
if (!textarea.value || textarea.value === '') {
return;
}
const array = separator.value ? textarea.value.split(separator.value) : textarea.value.split('\n');
const aliases = {
'alu': 'aluminum',
'cash': 'money',
'gas': 'gasoline',
'muni': 'munitions',
'munis': 'munitions',
'baux': 'bauxite',
'ura': 'uranium',
'from': 'note',
'sk': 'safekeeping',
'cash': 'money',
'alum': 'aluminum',
};
const resources = [
'uranium',
'iron',
'bauxite',
'coal',
'oil',
'steel',
'munitions',
'aluminum',
'gasoline',
'food',
'money'
];
const modified = [
'uranium',
'iron',
'bauxite',
'coal',
'oil',
'steel',
'munitions',
'aluminum',
'gasoline',
'money',
'food',
'note',
'recipient',
];
const keywords = ['note', 'leader', 'nation', 'alliance'];
const expandedArray = await Promise.all(array.map(async item => {
item = item.toLocaleLowerCase();
item = item.replace(/(\d+)\s*([kmbt])/ig, (match, number, multiplier) => {
const expandedNumber =
multiplier === 'k' ? parseFloat(number) * 1000 :
multiplier === 'm' ? parseFloat(number) * 1000000 :
multiplier === 'b' ? parseFloat(number) * 1000000000 :
multiplier === 't' ? parseFloat(number) * 1000000000000 :
parseFloat(number);
return expandedNumber.toString();
});
item = item.replaceAll(/\$(\d+)/g, "$1 money");
for (const alias in aliases) {
if (aliases.hasOwnProperty(alias)) {
const regex = new RegExp(`\\b${alias}\\b`, 'ig');
item = item.replace(regex, aliases[alias]);
}
}
const idRegex = /id=(\d+)/;
const idMatch = item.match(idRegex);
if (idMatch) {
const idNumber = idMatch[1];
const name = await getName(idNumber);
item = `nation ${name}`;
}
if (item.includes('safekeeping') && !item.includes('note')) {
item = 'note ' + item;
}
const resource = resources.find(resource => item.includes(resource));
if (resource) {
let amount = item.match(/(\d+)/);
if (amount) {
setValue(`with${resource}`, amount[1]);
modified.splice(modified.indexOf(resource), 1);
}
}
const keywordMatch = keywords.find(keyword => item.toLowerCase().includes(keyword));
if (keywordMatch) {
const keywordIndex = item.indexOf(keywordMatch);
const keywordText = item.slice(keywordIndex + keywordMatch.length + 1); // Remove keyword and space from the string
if (keywordMatch === 'note') {
setValue('withnote', keywordText);
modified.splice(modified.indexOf(keywordMatch), 1);
} else {
setValue(`withrecipient`, keywordText);
setValue(`withtype`, keywordMatch.charAt(0).toUpperCase() + keywordMatch.slice(1));
modified.splice(modified.indexOf('recipient'), 1);
}
}
return item;
}));
modified.forEach(resource => {
if (resource === 'note' || resource === 'recipient') {
setValue(`with${resource}`, '');
} else {
setValue(`with${resource}`, 0);
}
});
console.log(expandedArray);
}
textarea.addEventListener('input', () => {
debounce(updateArray, 1000);
});
separator.addEventListener('input', () => {
debounce(updateArray, 1000);
});
const parentElement = document.querySelector('hr+ .row');
if (parentElement) {
parentElement.insertBefore(container, parentElement.firstChild);
}
var vars = {};
var parts = window.location.href.replace(
/[?&]+([^=&]+)=([^&]*)/gi,
function (m, key, value) {
vars[key] = value;
}
);
if (vars.type === 'ID') {
let name = await getName(vars.recipient);
if (name) {
setValue('withrecipient', name);
setValue('withtype', 'Nation');
}
} else if(vars.type) {
setValue('withrecipient', vars.recipient)
setValue('withtype', vars.type)
}
['food', 'uranium', 'iron', 'bauxite', 'coal', 'oil', 'steel', 'munitions', 'aluminum', 'gasoline', 'money', 'note']
.forEach(param => {
if (vars[param]) {
setValue(`with${param}`, decodeURIComponent(vars[param]));
}
});
})();