Skip to content

Instantly share code, notes, and snippets.

@Sitethief
Last active December 10, 2022 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sitethief/4fcefb701dd976ac55ccff23e30f7ff5 to your computer and use it in GitHub Desktop.
Save Sitethief/4fcefb701dd976ac55ccff23e30f7ff5 to your computer and use it in GitHub Desktop.
Adds owner counts to the value page
// ==UserScript==
// @name NSValuePageOwnerCount Gist version
// @namespace sitethiefs-ns-scripts
// @version 0.2.1
// @description Adds owner counts to the value/bids/asks/auctions page
// @author Sitethief of Vylixan
// @copyright GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
// @match https://www.nationstates.net/page=deck/value_deck*
// @match https://www.nationstates.net/*/value_deck*
// @match https://www.nationstates.net/page=deck/show_trades=bids
// @match https://www.nationstates.net/*/show_trades=bids*
// @match https://www.nationstates.net/page=deck/show_trades=asks
// @match https://www.nationstates.net/*/show_trades=asks*
// @match https://www.nationstates.net/page=deck/show_trades=auctions
// @match https://www.nationstates.net/*/show_trades=auctions*
// @match https://www.nationstates.net/page=deck/show_market=auctions
// @match https://www.nationstates.net/*/show_market=auctions*
// @icon https://www.google.com/s2/favicons?domain=nationstates.net
// @grant GM.xmlHttpRequest
// ==/UserScript==
(function () {
'use strict';
/**
*
* Start of customizable options
*
**/
let options = {
mainNationName: '', // This should be your main nation name to identify you in all API requests
}
/**
*
* End of customizable options
*
**/
if (options.mainNationName) {
const sleepNow = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
async function getOwners(cardID, cardSeason, row) {
GM.xmlHttpRequest({
headers: {
"User-Agent": options.mainNationName,
"Accept": "text/xml",
},
method: "GET",
url: `https://www.nationstates.net/cgi-bin/api.cgi?q=card+info+owners;cardid=${cardID};season=${cardSeason}`,
onload: async function (response) {
let responseXML = new DOMParser()
.parseFromString(response.responseText, "text/xml");
let owners = responseXML.getElementsByTagName("OWNER");
console.log(owners);
let cardsCount = owners.length;
let ownersCount = await countOwners(owners, row);
const cardsCountTD = document.createElement("td");
const cardsTextnode = document.createTextNode(cardsCount);
cardsCountTD.appendChild(cardsTextnode);
row.appendChild(cardsCountTD);
}
});
}
function countOwners(owners, row) {
let ownersCount = 0;
let ownersList = [];
for (let i = 0; i < owners.length; i++) {
if (typeof ownersList[owners[i].innerHTML] === 'undefined') {
ownersList[owners[i].innerHTML] = true;
ownersCount++;
}
}
const ownerCountTD = document.createElement("td");
const ownerTextnode = document.createTextNode(ownersCount);
ownerCountTD.appendChild(ownerTextnode);
row.appendChild(ownerCountTD);
}
async function startLoop() {
let rows = document.querySelectorAll('.auctionslisttable tbody tr');
for (let i = 0; i < rows.length; i++) {
let row = rows[i]
let auctionCell = row.querySelector('td:nth-child(1) p');
if (!auctionCell || auctionCell.innerText !== 'Auction') {
let cardID = null;
let season = null;
let linkTD = row.querySelector('td:nth-child(2)');
let cardLink = linkTD.querySelector('a.cardnameblock');
const url = new URL(cardLink.href);
let splitpaths = url.pathname.split('/');
let foundID = splitpaths.filter(s => s.includes('card='));
if (foundID[0]) {
cardID = foundID[0].replace('card=', '');
}
let foundSeason = splitpaths.filter(s => s.includes('season='));
if (foundSeason[0]) {
season = foundSeason[0].replace('season=', '');
}
if (season && cardID) {
getOwners(cardID, season, row);
await sleepNow(700);
}
} else {
const ownerHeader = document.createElement("td");
const ownereTextnode = document.createTextNode("Owners");
ownerHeader.appendChild(ownereTextnode);
row.appendChild(ownerHeader);
const cardsHeader = document.createElement("td");
const cardsTextnode = document.createTextNode("Cards");
cardsHeader.appendChild(cardsTextnode);
row.appendChild(cardsHeader);
}
}
}
startLoop();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment