When opening a loot box, I would junk cards I'm not interested in, and middle-click the Infos button on cards I want to load on their own page to decide what to do.
What was wrong with that:
After selling or gifting or anything on the 1st card page opened in the previous way, other card pages had to be reloaded before use, else they would reload with a security error message, and the attempted action would not have worked.
What I do with the script:
When opening a loot box, I junk cards I'm not interested in, middle-click the Info button for the 1st remaining card after that, then use the "Add to cards queue"
button from the script for the remaining cards.
Then, after doing what I had to do with the card page loaded through a middle-click, I click the "Next element in queue[X]" (where X is the number of elements in queue), that load the page of a card added to queue, removing that card from the queue.
Advantages:
For player, no more unnecessary reload to do, or security error to face.
For server, only send the card page once.
Script code:
- Code: Select all
// ==UserScript==
// @name NationStates Card Management Queue
// @namespace NS_Cards_Queue_Script
// @version 1.0
// @description A queue to put the url of cards from packs and load them later from another card
// @author Anozia
// @match https://www.nationstates.net/page=deck
// @match https://www.nationstates.net/page=deck/card=*
// @run-at document-idle
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// ==/UserScript==
(async function() {
var button_elt;
var div_elt;
var i;
if(window.location.href == "https://www.nationstates.net/page=deck") { //deck page, may be lootbox page or another
var p_elts = document.getElementsByTagName("p");
var is_on_lootbox_page = false;
for(i = 0; i < p_elts.length && !is_on_lootbox_page; i++) {
if(p_elts[i].textContent == "Tap cards to reveal...") {
is_on_lootbox_page = true;
}
}
if(is_on_lootbox_page) { //lootbox page confirmed
var card_elts = document.getElementsByClassName("deckcard-info-cardlink");
for(i = 0; i < card_elts.length; i++) {
button_elt = document.createElement("button");
button_elt.textContent = "Add to cards queue";
button_elt.addEventListener('click',add_in_queue,false);
div_elt = document.createElement("div");
div_elt.appendChild(button_elt);
card_elts[i].parentNode.insertBefore(div_elt,card_elts[i]);
}
}
}
else { //single card page
var queue_holder = await GM.getValue("NS_Cards_Queue", null);
if(queue_holder != null) { //get the existing queue
queue_holder = JSON.parse(queue_holder);
//only create the button if the queue is not empty
if(queue_holder.length != 0) {
try {
var existing_elt = document.getElementById("deck-single-card");
button_elt = document.createElement("button");
button_elt.textContent = "Next element in queue[" + queue_holder.length + "]";
button_elt.addEventListener('click',get_next_element_in_queue,false);
div_elt = document.createElement("div");
div_elt.style["justify-content"] = "center";
div_elt.style["display"] = "flex";
div_elt.appendChild(button_elt);
existing_elt.parentNode.insertBefore(div_elt,existing_elt);
}
catch(exc) {}
}
}
}
async function get_next_element_in_queue(click_event) {
click_event.target.removeEventListener('click',get_next_element_in_queue,false);
try {
var queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));
var queue_element = queue_holder.shift(); //get and remove first element in queue
if(queue_holder.length != 0) {
await GM.setValue("NS_Cards_Queue",JSON.stringify(queue_holder)); //store back updated queue
}
else {
await GM.deleteValue("NS_Cards_Queue"); //delete queue storage if empty
}
click_event.target.parentNode.parentNode.removeChild(click_event.target.parentNode); //remove the div containing the button
window.location = queue_element; //go to the page of next element in queue
}
catch(exc) { //could happen if queue is emptied from another tab, just remove the button in this case
click_event.target.parentNode.parentNode.removeChild(click_event.target.parentNode); //remove the div containing the button
}
}
async function add_in_queue(click_event) {
click_event.target.removeEventListener('click',add_in_queue,false);
var queue_holder = await GM.getValue("NS_Cards_Queue", null);
if(queue_holder == null) { //create the new empty queue
queue_holder = [];
}
else { //get the existing queue
queue_holder = JSON.parse(queue_holder);
}
try {
//add link to card info to queue
queue_holder.push(click_event.target.parentNode.nextElementSibling.firstElementChild.getAttribute("href")); //button.div.infobutton.link.getAttribute("href")
click_event.target.textContent = "Card added to queue";
click_event.target.disabled = true;
}
catch(exc) {
click_event.target.textContent = "Error";
click_event.target.disabled = true;
}
await GM.setValue("NS_Cards_Queue",JSON.stringify(queue_holder)); //store back updated queue
}
})();
Technicalities:
-The GM methods are used, because other methods of storage would not work with Firefox 69 in permanent privacy mode.
-The queue only store urls that aren't tied to a nation.
-When the last card is taken out of the queue, the queue is removed from storage.The idea would be to prevent the data to be written to disk.No idea if that's what really happen.
I don't really like web programming or javascript, but I hope I still managed to make something that can be useful to some.