-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAlternate Auction Layout.user.js
241 lines (221 loc) · 8.18 KB
/
Alternate Auction Layout.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
// ==UserScript==
// @name Alternate Auction Layout
// @version 0.5.2
// @namespace dithpri.RCES
// @description An alternate auction layout better suited for wide screens
// @author dithpri
// @downloadURL https://github.com/dithpri/RCES/raw/master/userscripts/auction/Alternate%20Auction%20Layout.user.js
// @noframes
// @match https://www.nationstates.net/*page=deck*/card=*
// @match https://www.nationstates.net/*card=*/page=deck*
// ==/UserScript==
/*
* Copyright (c) 2022 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.
*/
function addStyle(style) {
"use strict";
var node = document.createElement("style");
node.innerHTML = style;
document.getElementsByTagName("head")[0].appendChild(node);
}
function newElementWithAttribs(elem, attribs) {
let ret = document.createElement(elem);
for (const attribName in attribs) {
ret.setAttribute(attribName, attribs[attribName]);
}
return ret;
}
function canRowsCollapse(previousRow, currentRow) {
if (previousRow == undefined || currentRow == undefined) {
return false;
}
// Rows are identical
if (previousRow.textContent == currentRow.textContent) {
return true;
}
// Rows are unmatched "watch" bids or "no-junk" asks
if (
previousRow.classList.contains("cardauctionunmatchedrow") &&
currentRow.classList.contains("cardauctionunmatchedrow")
) {
// "no-junk" ask
if (
previousRow.children[1].textContent == currentRow.children[1].textContent &&
previousRow.children[1].textContent == "10000.00"
) {
return true;
}
// "watch" bid
if (
previousRow.children[3].textContent == currentRow.children[3].textContent &&
previousRow.children[3].textContent == "0.01"
) {
return true;
}
}
// Fallback
return false;
}
function update_auctiontable() {
// collapse identical bids/asks/matches into a single row
let previousRow = undefined;
[
...document
.getElementById("cardauctiontable")
.querySelectorAll("tr.cardauctionunmatchedrow, tr.cardauctionmatchedrow"),
undefined, // undefined is a hack to force an update/collapse of the last row if needed
].forEach((row) => {
if (previousRow == undefined) {
// We're on the first row, there's nothing to collapse
previousRow = row;
return;
}
if (row != undefined && canRowsCollapse(previousRow, row)) {
// If one of the rows is already visible, make sure the "collapsed" row will be visible as well
if (!("cardauctionhiddenrow" in previousRow.classList) || !("cardauctionhiddenrow" in row.classList)) {
previousRow.classList.remove("cardauctionhiddenrow");
row.classList.remove("cardauctionhiddenrow");
}
// Merge data from previous row if the buyers/sellers are not identical
// When merging, check if the nation is already somewhere in the list and deduplicate
if (previousRow.children[0].textContent != row.children[0].textContent) {
for (const element of [...previousRow.children[0].children]) {
let shouldPrepend = true;
for (const child of [...row.children[0].children]) {
if (child.textContent == element.textContent) {
shouldPrepend = false;
break;
}
}
if (shouldPrepend) {
row.children[0].prepend(element);
}
}
}
if (previousRow.children[4].textContent != row.children[4].textContent) {
for (const element of [...previousRow.children[4].children]) {
let shouldPrepend = true;
for (const child of [...row.children[4].children]) {
if (child.textContent == element.textContent) {
shouldPrepend = false;
break;
}
}
if (shouldPrepend) {
row.children[4].prepend(element);
}
}
}
// Current and previous rows can be collapsed, update the number of collapsed rows and remove previous row
row.dataset.rcesCollapsedRows = Number(previousRow.dataset.rcesCollapsedRows || 1) + 1;
if (previousRow.parentNode) {
previousRow.parentNode.removeChild(previousRow);
}
} else if (previousRow.dataset.rcesCollapsedRows > 1) {
// Current and previous rows are different, only now do we actually need to add an indicator of the number collapsed rows
const countIndicatorEl = document.createElement("div");
countIndicatorEl.textContent = `×${previousRow.dataset.rcesCollapsedRows}`;
if (previousRow.children[2].textContent != "") {
previousRow.children[2].append(document.createElement("hr"));
}
previousRow.children[2].append(countIndicatorEl);
}
previousRow = row;
});
// Hide bid/ask if they're identical to the match column
document
.getElementById("auctiontablebox")
.querySelectorAll("#cardauctiontable > tbody > tr.cardauctionmatchedrow")
.forEach(function (el) {
//td > p > span.cardprice
const [s1, s2, s3] = [...el.querySelectorAll("td > p > span.cardprice")];
const [p1, p2, p3] = [s1, s2, s3].map((x) => Number(x.textContent));
if (p1 == p2 && p2 == p3) {
s1.style.visibility = "hidden";
s3.style.visibility = "hidden";
}
});
}
(function () {
"use strict";
// Check if we're on the auction page or owners/trades history/finds/collections page
const isAuctionPage = !!document.getElementById("auctiontablebox");
if (isAuctionPage) {
// Because NS, for some reason, has really fucked up templating that results in nested forms and weird templates when there's no bids/asks placed.
// Move elements that do not normally belong in #auctiontablebox to after its parent form.
document
.querySelectorAll("#auctiontablebox > :not(#cardauctiontable)")
.forEach((node) => document.getElementById("auctiontablebox").parentElement.after(node));
}
// Add id to table for easier use
document.querySelector("table.shiny.wide.deckcard-card-stats").id = "rces-infotable";
// Container for side-by-side display of table and auction
document.getElementById("deck-single-card").before(newElementWithAttribs("div", {id: "rces-container"}));
// Move table wrapper, auction wrapper, card to container
document
.getElementById("rces-container")
.append(
newElementWithAttribs("div", {id: "rces-card-wrapper"}),
newElementWithAttribs("div", {id: "rces-auction-wrapper"}),
newElementWithAttribs("div", {id: "rces-infotable-wrapper"})
);
// Move the card to its wrapper
document.getElementById("rces-card-wrapper").append(document.getElementById("deck-single-card"));
// Move the card information table to its wrapper
document.getElementById("rces-infotable-wrapper").append(document.getElementById("rces-infotable"));
if (isAuctionPage) {
// Move the auction log below the card
document.getElementById("rces-card-wrapper").append(document.getElementById("auctionlog-area"));
// Move the auction table and bid/ask buttons to the correct wrapper
document
.getElementById("rces-auction-wrapper")
.append(
document.getElementById("cardauctionoffertable").parentElement,
document.getElementById("auctiontablebox").parentElement
);
}
// Wrap the auction countdown to make it float with scrolling
document.getElementById("rces-container").before(newElementWithAttribs("div", {id: "rces-countdown-wrapper"}));
document.getElementById("rces-countdown-wrapper").append(document.getElementById("countdown-cardauction") || "");
// Move the "You own x copies" info to the top.
document.getElementById("ttq_1a").after(document.querySelector(".minorinfo") || "");
if (isAuctionPage) {
update_auctiontable();
let observer = new MutationObserver(function (mutationList) {
update_auctiontable();
});
const observerOptions = {
childList: true,
};
observer.observe(document.getElementById("auctiontablebox"), observerOptions);
}
addStyle(`
#countdown-cardauction {
display: inline-block;
background-color: ${window.getComputedStyle(document.getElementById("main")).backgroundColor};
margin: 0;
padding: 0.5em;
border-radius: 0.5em;
}
#rces-countdown-wrapper{
position: sticky;
z-index: 99;
top: ${document.getElementById("banner")?.clientHeight || 0}px;
text-align: center;
pointer-events: none;
}
#rces-container {
display: flex;
justify-content: space-evenly;
flex-direction: row;
}
.minorinfo{
font-weight: 1000;
color: darkgreen;
text-align: center;
}
`);
})();