-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain Auction Displayer.user.js
228 lines (214 loc) · 6.77 KB
/
Main Auction Displayer.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
// ==UserScript==
// @name Main Auction Displayer
// @version 0.17
// @namespace dithpri.RCES
// @description Displays puppets' main nation above puppet name in an auction
// @author dithpri
// @downloadURL https://github.com/dithpri/RCES/raw/master/userscripts/auction/Main%20Auction%20Displayer.user.js
// @noframes
// @match https://www.nationstates.net/*/page=deck/*
// @match https://www.nationstates.net/page=deck/*
// @grant GM.xmlHttpRequest
// @grant GM.setValue
// @grant GM.getValue
// @grant GM.registerMenuCommand
// @connect docs.google.com
// @connect googleusercontent.com
// ==/UserScript==
/*
* To have the script run on any NS page, you can add this match line:
// @match https://www.nationstates.net/*
*/
/*
* Copyright (c) 2020 dithpri (Racoda) <[email protected]>
* This file is part of RCES: https://github.com/dithpri/RCES and licensed under
* the MIT license. See LICENSE.md or
* https://github.com/dithpri/RCES/blob/master/LICENSE.md for more details.
*/
/* Permissions:
*
* GM.xmlHttpRequest, `connect docs.google.com`, `connect googleusercontent.com`:
* to automatically fetch and update the puppet list.
*
* GM.setValue, GM.getValue:
* to save and load the puppet list locally.
*
* GM.registerMenuCommand:
* to offer a force update sheets button in the UserScripts menu.
*/
const sheets = [
{
// 9003's spreadsheet
url: "https://docs.google.com/spreadsheets/d/1MZ-4GLWAZDgB1TDvwtssEcVKHKunOKi3l90Jof1pBB4/export?format=tsv&id=1MZ-4GLWAZDgB1TDvwtssEcVKHKunOKi3l90Jof1pBB4&gid=733627866",
puppetColumn: 0,
mainColumn: 1,
headerRows: 1,
},
{
// XKI card co-op
url: "https://docs.google.com/spreadsheets/d/e/2PACX-1vSem15AVLXgdjxWBZOnWRFnF6NwkY0gVKPYI8aWuHJzlbyILBL3o1F5GK1hSK3iiBlXLIZBI5jdpkVr/pub?gid=916202163&single=true&output=tsv",
puppetColumn: 0,
mainColumn: 1,
headerRows: 0,
},
];
function GM_addStyle(style) {
"use strict";
var node = document.createElement("style");
node.innerHTML = style;
document.getElementsByTagName("head")[0].appendChild(node);
}
function GM_promiseXmlHttpRequest(opts) {
return new Promise((resolve, reject) => {
let details = opts;
details.onload = (response) => {
if (response.status >= 200 && response.status < 300) {
resolve(response);
} else {
reject(response);
}
};
details.onerror = (response) => {
reject(response);
};
GM.xmlHttpRequest(details);
});
}
function canonicalize(name) {
return name.trim().toLowerCase().replace(/ /g, "_");
}
async function getFromSheet(sheetUrl, puppetColumn = 0, mainColumn = 1, headerRows = 1) /* -> object (map) */ {
const data = await GM_promiseXmlHttpRequest({
method: "GET",
url: sheetUrl,
});
return data.responseText
.split("\n")
.map((x) => {
let y = x.split("\t");
if (puppetColumn < mainColumn) {
y = y.slice(puppetColumn, mainColumn + 1);
} else {
// switch column order if the puppetmaster column is before the puppet
y.slice(mainColumn, puppetColumn + 1);
y = [y[1], y[0]];
}
return y;
})
.slice(headerRows)
.filter((x) => canonicalize(x[0]) != canonicalize(x[1]))
.reduce(function (map, obj) {
map[canonicalize(obj[0].trim())] = ((x) =>
// De-canonicalize owner names
// This could potentially be done when displaying, but we
// prefer doing this when downloading the sheet.
x.charAt(0).toUpperCase() + x.slice(1))(obj[1].trim().replaceAll("_", " "));
return map;
}, {});
}
async function updatePuppets(isAuctionPage) {
const puppets_map = JSON.parse(await GM.getValue("rces-main-nations", "{}"));
if (isAuctionPage) {
document
.querySelectorAll(
"#cardauctiontable > tbody > tr > td > p > a.nlink, .deckcard-title > a.nlink:not(.rces-was-parsed), .deckcard-name > a.nlink:not(.rces-was-parsed)"
)
.forEach(function (el, i) {
const canonical_nname = el.getAttribute("href").replace(/^nation=/, "");
el.classList.add("rces-was-parsed");
if (Object.prototype.hasOwnProperty.call(puppets_map, canonical_nname)) {
const puppetmaster = puppets_map[canonical_nname];
el.insertAdjacentHTML(
"beforebegin",
`<a href="nation=${canonicalize(
puppetmaster
)}" class="nlink rces-was-parsed rces-main-nation">(<span class="nnameblock">${puppetmaster}</span>)</a><br />`
);
}
});
}
document
.querySelectorAll('a.nlink:not(.rces-was-parsed), a[href^="/nation="]:not(.rces-was-parsed)')
.forEach(function (el, i) {
const nlink = el.getAttribute("href");
if (!/^\/?nation=[a-z0-9_-]+$/.test(nlink)) {
return;
}
const canonical_nname = el.getAttribute("href").replace(/^\/?nation=/, "");
if (Object.prototype.hasOwnProperty.call(puppets_map, canonical_nname)) {
const puppetmaster = puppets_map[canonical_nname];
el.classList.add("rces-was-parsed");
el.insertAdjacentHTML(
"afterend",
` <a href="nation=${canonicalize(
puppetmaster
)}" class="nlink rces-was-parsed">(<span class="nnameblock">${puppetmaster}</span>)</a>`
);
}
});
}
async function refreshAllSheets() {
await GM.setValue(
"rces-main-nations",
JSON.stringify(
Object.assign(
{},
...(await Promise.all(
sheets.map((sheet) =>
getFromSheet(sheet.url, sheet.puppetColumn, sheet.mainColumn, sheet.headerRows)
)
))
)
)
);
GM.setValue("rces-main-nations-lastupdate", new Date().getTime());
}
function setupUpdates() {
const isAuctionPage = document.getElementById("auctiontablebox") != null;
let observer = new MutationObserver(function (mutationList) {
updatePuppets(isAuctionPage);
});
let observerOptions = {
childList: true,
// for dynamically updating pages such as the world feed
subtree: !isAuctionPage,
};
updatePuppets(isAuctionPage);
observer.observe(document.getElementById("auctiontablebox") || document, observerOptions);
}
(async function () {
"use strict";
let lastUpdateMs = await GM.getValue("rces-main-nations-lastupdate", 0);
if (GM.registerMenuCommand != undefined) {
/*
* GreaseMonkey requires a polyfill.
* Not using it, because it clutters the user's page context menu instead of displaying the option in the extension's menu.
*/
await GM.registerMenuCommand(
`Force refresh sheets (last update: ${new Date(lastUpdateMs).toLocaleString()})`,
refreshAllSheets,
null
);
}
// Continue with the old script functionality.
// If we haven't updated in the last 24h
if (lastUpdateMs + 24 * 60 * 60 * 1000 < new Date().getTime()) {
refreshAllSheets();
}
GM_addStyle(
`
.s3-upper .rces-main-nation {
position: relative;
z-index: 3;
margin: .5em .5em .4em 1em;
color: white;
font-weight: 700;
font-size: 200%;
filter: drop-shadow(1px 1px 1px black) drop-shadow(1px 1px 1px black);
}
.s3-upper .rces-main-nation .nnameblock {
}
`
);
setupUpdates();
})();