NATION

PASSWORD

[Tampermonkey Script] NationStates Card Management Queue

The place to wheel and deal, talk shop, and build up your dream deck!
User avatar
Anozia
Secretary
 
Posts: 35
Founded: Oct 10, 2010
Inoffensive Centrist Democracy

[Tampermonkey Script] NationStates Card Management Queue

Postby Anozia » Sun Oct 27, 2019 8:27 am

What I did before the script:
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.

User avatar
Destructive Government Economic System
Minister
 
Posts: 3243
Founded: Jun 15, 2017
Corporate Police State

Postby Destructive Government Economic System » Sun Dec 15, 2019 7:40 pm

This is actually a good tool.

However, the inconvenience of using this is the fact that the queue only applies to opening packs.

Therefore, after very slightly tweaking the code, the script now works on your nation's deck page as well:

Code: Select all
// ==UserScript==
// @name         NationStates Card Management Queue For Deck Page AND Opening Packs
// @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_deck_page = true;

        for(i = 0; i < p_elts.length && !is_on_deck_page; i++) {
            if(p_elts[i].textContent == "Tap cards to reveal...") {
                is_on_deck_page = false;
            }
        }

        if(is_on_deck_page) { //deck 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

    }

})();


If you click on any (already-existing) cards on your deck page, you should be able to add them to the queue, along with the cards you added from opening packs. This is particularly useful when you're trying to sell a bunch of cards in a short amount of time.

As an example, here is me adding 2 cards to the queue. The first added one came from a recently-opened pack:

Image


... while the second one comes from a card that's been laying around in my deck page:

Image


After adding them, I go to a random card and do whatever with it. The 2 queued cards should then display after pulling up the random card's page:

Image


Click the "next element in queue" button to go the cards you added in queue:

Image
Image


(The queue button is removed once you get to the very last card in said queue.)




As the OP mentioned, you will need to use the Tampermonkey extension to get this working. After installing Tampermonkey, click on the extension's icon, go to "create a new script", copy and paste the code provided, and save the file. The script should automatically work after saving, and you will be free to disable it at any time by clicking the extension icon, going to "dashboard", and turning off the script.

Hope this helps anyone.
"All I wish is to see the world burn."
-The Great Uniter and Beast of the DGES
(By the way, the DGES is a servant to DEAREST LEADER of Psychotic Dictatorships.)
Just your typical guy who wants to have fun. Don't take this nation seriously,
ever.
I DO NOT use NS stats!
Keshiland literally wrote:I would give it a no. A country that lies about how free, or how great, or how humanitarian it is can never be developed. Example, NK lies and says they are democratic and are not, the US lies and says we are free yet we incarcerate millions for a medical plant. See we are basically a larger more populated North Korea.

User avatar
9003
Chargé d'Affaires
 
Posts: 401
Founded: Oct 25, 2012
Inoffensive Centrist Democracy

Postby 9003 » Thu Dec 19, 2019 8:51 am

Love this! thank you super much

One idea/suggestion/how could I do it on my own :P

this works great for farming and listing every card and processing on the puppets. but I still have to re add them all to it when I am on my main. Is there a way for it to save a list temporarily or more permanently in a file. Even if it just prints each link in the terminal?
proud member of PETZ people for the Ethical Treatment of Zombies

Active member of The cards market place discord

User avatar
Ter Voland
Secretary
 
Posts: 35
Founded: Feb 14, 2018
Capitalist Paradise

Postby Ter Voland » Thu Dec 19, 2019 10:02 am

Finally tried this. Thank you Anozia and DGES. It's really useful. :)

User avatar
Recuecn
Diplomat
 
Posts: 781
Founded: Feb 02, 2015
Liberal Democratic Socialists

Postby Recuecn » Thu Dec 19, 2019 10:03 am

Thanks for this! I was just thinking about this before this thread got bumped and this is going to come in super useful.

I'd also be interested in the update 9003 requested, since why not, but I'm not sure it would be as useful for me in particular since I know for a fact I don't sell nearly as many cards to myself as he does lol. If you're just selling cards that happen to have bids on them or even gifting to yourself then you don't need a way to find those cards back as badly.
rəswɛsən

User avatar
Recuecn
Diplomat
 
Posts: 781
Founded: Feb 02, 2015
Liberal Democratic Socialists

Postby Recuecn » Fri Dec 20, 2019 1:39 am

One feature that would make this even better would be if it worked with the new deck filters. It doesn't for me I think that's because the url starts off as "nationstates.net/nation=cahds_20/page=deck" with the nation name in there, rather than just "nationstates.net/page=deck"

I assume (I hope, at least!) that this is a pretty easy fix and can be done with wildcards, but I don't know/remember enough about coding to fix it myself. Could this feature be added please?
rəswɛsən

User avatar
Anozia
Secretary
 
Posts: 35
Founded: Oct 10, 2010
Inoffensive Centrist Democracy

Postby Anozia » Sun Mar 29, 2020 1:17 am

I'm afraid I can't help much on requested features, as they often require a list rather than a queue, so it should not be build on this exact system to begin with.
For those interested, I will show my latest version of my script, that got side-tracked but may be informative:
Code: Select all
// ==UserScript==
// @name         NationStates Card Management Queue
// @namespace    NS_Cards_Queue_Script
// @version      1.1
// @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/*
// @run-at       document-idle
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM.deleteValue
// ==/UserScript==

(async function() {

    var button_elt;
    var div_elt;
    var i;
    var cards_button_top;

    try {
        if(!cards_button_top) {
            let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));
            cards_button_top = document.createElement("div");
            cards_button_top.setAttribute('class',"bel");
            if(queue_holder !== null && queue_holder.length > 0) {
                cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a><div class=\"notificationnumber refreshable\" style=\"background-color:green\">" + queue_holder.length + "</div></div>";
            }
            else {
                cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a></div>";
            }
        }

        (document.getElementsByClassName("belspacer belspacermain")[0]).before(cards_button_top);

    }
    catch(exc) {}

    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
    if(window.location.href.startsWith("https://www.nationstates.net/page=deck/card=")) { //single card page

        let 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) {}

            }

        }

    }

    let cards_flags = document.getElementsByClassName("deckcard-flag");

    for(i = 0; i < cards_flags.length; i++) {
        cards_flags[i].addEventListener("click",toggleCardTitle,false);
    }

    function toggleCardTitle(event) {
        let card_title_element = event.target.parentNode.getElementsByClassName("deckcard-title");
        if(card_title_element.length != 0) {
            if(card_title_element[0].style.visibility == "hidden") {
                card_title_element[0].style.visibility = "visible";
            }
            else {
                card_title_element[0].style.visibility = "hidden";
            }
        }
    }

    async function get_next_element_in_queue(click_event) {

        click_event.target.removeEventListener('click',get_next_element_in_queue,false);

        try {

            let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));

            let 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);

        let 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

    }

})();


Features:
-A new card button appearing at top of screen to quickly go to the main cards page from anywhere
-When there are cards in the queue, a green indicator appear on that button (I needed that to not leave cards in a queue before leaving a nation :p )
-A different approach (not "match" at top of script) to recognize which page I'm on (may be useful for some requests) in the code.
-A completely unrelated feature to be able to click the upper part of a flag of a season 2 card (where the cursor doesn't turn into a hand) to toggle the
visibility of the nation name (to fully enjoy the flag design :) )
Last edited by Anozia on Sun Mar 29, 2020 1:19 am, edited 1 time in total.

User avatar
Recuecn
Diplomat
 
Posts: 781
Founded: Feb 02, 2015
Liberal Democratic Socialists

Postby Recuecn » Wed Apr 08, 2020 3:53 am

First off, let me say that I love the new features Anozia. I'd been thinking just recently, before you posted the update, that it would be nice sometimes to be able to see the whole card and hide the info on top, and I'm already finding the new button at the top of the screen super handy.

However, I also found that I'd been spoiled by DGES' version where the script could run on the deck page too. Often I open packs all at once and go back to sort through the cards later, so it wasn't really worth giving up on the deck page functionality for the cool new bells and whistles. Also, I find that the best way for me to quickly identify which cards might be worth selling if by looking at the value page. So I asked DGES if it might be possible to combine the deck page functionality of his version with Anozia's latest upgrades, and maybe even add some new features. As always, DGES was incredibly gracious and looked into it for me, but in the end suggested running both scripts at once and, although he did find a way to make the script work on deck pages other than the first (which it didn't before), couldn't figure out all the new capabilities I wanted—which was fair, I was asking for quite a few things. So I turned to Racoda and "commissioned" a new version of the script to combine DGES' deck page feature with all of Anozia's features, plus the new features I wanted. Below is the result. All my thanks to Racoda, Anozia, and DGES for this. It's really nice of all of them to share their time and effort with the community in this way. I've done none of the actual coding here.

Code: Select all
// ==UserScript==
// @namespace    NS_Cards_Queue_Script
// @name         Anozia feat. RCSA & DGES - Reçu's Feature Requests
// @version      1.1
// @description  A queue to put the url of cards from packs and load them later from another card
// @author       Anozia
// @contributors Racoda and DGES
// @match        https://www.nationstates.net/*
// @run-at       document-idle
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM.deleteValue
// ==/UserScript==

(async function() {

    var button_elt;
    var div_elt;
    var i;
    var cards_button_top;

    try {
        if(!cards_button_top) {
            let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));
            cards_button_top = document.createElement("div");
            cards_button_top.setAttribute('class',"bel");
            if(queue_holder !== null && queue_holder.length > 0) {
                cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a><div class=\"notificationnumber refreshable\" style=\"background-color:green\">" + queue_holder.length + "</div></div>";
            }
            else {
                cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a></div>";
            }
        }

        (document.getElementsByClassName("belspacer belspacermain")[0]).before(cards_button_top);

    }
    catch(exc) {}

    if(window.location.href.startsWith("https://www.nationstates.net/page=deck/card=")) { //single card page

        let 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) {}

            }

        }

    }
    else
    if(window.location.href.startsWith("https://www.nationstates.net/page=deck")
     ||window.location.href.match(/https:\/\/www\.nationstates\.net\/(nation|container)=[A-Za-z0-9_-]+\/page=deck/)) { //deck page, may be lootbox page or another
        if (window.location.href.includes("/value_deck=1")) {
            var card_link_elts = document.getElementsByClassName("cardnameblock");
            for(i = 0; i < card_link_elts.length; i++) {
                button_elt = document.createElement("button");
                button_elt.textContent = "Add to cards queue";
                button_elt.addEventListener('click',add_in_queue_valdeck,false);
                button_elt.classList.add("deckcard-token");
                card_link_elts[i].parentNode.insertBefore(button_elt,card_link_elts[i]);
            }

        } else {
            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;
                }
            }

            // Unneeded condition
            // 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]);

                }

            // }
        }

    }

    let cards_flags = document.getElementsByClassName("deckcard-flag");

    for(i = 0; i < cards_flags.length; i++) {
        cards_flags[i].addEventListener("click",toggleCardTitle,false);
    }

    function toggleCardTitle(event) {
        let card_title_element = event.target.parentNode.getElementsByClassName("deckcard-title");
        if(card_title_element.length != 0) {
            if(card_title_element[0].style.visibility == "hidden") {
                card_title_element[0].style.visibility = "visible";
            }
            else {
                card_title_element[0].style.visibility = "hidden";
            }
        }
    }

    async function get_next_element_in_queue(click_event) {

        click_event.target.removeEventListener('click',get_next_element_in_queue,false);

        try {

            let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));

            let 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_valdeck(click_event) {

        click_event.target.removeEventListener('click',add_in_queue,false);

        let 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.nextElementSibling.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

    }


    async function add_in_queue(click_event) {

        click_event.target.removeEventListener('click',add_in_queue,false);

        let 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

    }

})();

Features:
  • Shortcut to deck page at top of screen
  • Card processing queue
  • Icon to show how many cards are in queue
  • Hide (and unhide) card info to view flag
  • Works when opening packs
  • Works on deck page
  • Works on value page
  • Works with deck filters
  • Works on all pages of deck
  • Works with firefox containers
  • Any combination of the above
rəswɛsən

User avatar
Destructive Government Economic System
Minister
 
Posts: 3243
Founded: Jun 15, 2017
Corporate Police State

Postby Destructive Government Economic System » Wed Apr 08, 2020 8:31 am

Beautiful. Absolutely beautiful.

Now we're getting somewhere! Definitely something many players will try using now o.0

Anozia, Resu, and Racoda, thanks for all of your help; y'all have really worked to bring out the potential of this tool 8)
"All I wish is to see the world burn."
-The Great Uniter and Beast of the DGES
(By the way, the DGES is a servant to DEAREST LEADER of Psychotic Dictatorships.)
Just your typical guy who wants to have fun. Don't take this nation seriously,
ever.
I DO NOT use NS stats!
Keshiland literally wrote:I would give it a no. A country that lies about how free, or how great, or how humanitarian it is can never be developed. Example, NK lies and says they are democratic and are not, the US lies and says we are free yet we incarcerate millions for a medical plant. See we are basically a larger more populated North Korea.

User avatar
Vylixan
Civil Servant
 
Posts: 10
Founded: Mar 19, 2006
Left-Leaning College State

Bugfix for domain matching

Postby Vylixan » Sun Jul 05, 2020 12:58 am

I added a feature to this script. You can now add cards from the "The North Pacific Card Queries" result page to the queue with a button. I also added a reset button on that page.

Code: Select all
        // ==UserScript==
    // @namespace    NS_Cards_Queue_Script
    // @name         Anozia feat. RCSA & DGES - Reçu's Feature Requests - and Sitethiefs QueryQueueFiller
    // @version      1.2
    // @description  A queue to put the url of cards from packs and load them later from another card, now also able to add cards from the query page
    // @author       Anozia
    // @contributors Racoda, DGES and Sitethief
    // @include      *azure.nsr3n.info/card_queries/*
    // @include      *www.nationstates.net/*
    // @require      https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.min.js?a4098
    // @run-at       document-idle
    // @grant        GM.getValue
    // @grant        GM.setValue
    // @grant        GM.deleteValue
    // ==/UserScript==

/*
 * Keybinds:
 * [n]ext queue item
 */


    (async function() {
        // next queue item
        Mousetrap.bind(['n', 'N'], function(event) {
            get_next_element_in_queue(event);

        });
        var button_elt;
        var div_elt;
        var i;
        var cards_button_top;
        var fill_button_top;
        var reset_button_top;

        let location = window.location.href
        let locationIsNS = location.includes('nationstates.net');
        let locationIsQueryServer = location.includes('azure.nsr3n.info/card_queries/get_daemon.sh')

        if (locationIsQueryServer) {
              try {
                if(!fill_button_top) {
                  fill_button_top = document.createElement('div');
                  fill_button_top.id="queue_fill_button";
                  fill_button_top.style.color = "red";
                  fill_button_top.style.border = "1px solid black";
                  fill_button_top.style.position = "absolute";
                  fill_button_top.style.padding = "10px";
                  fill_button_top.style.top = "20px";
                  fill_button_top.style.margin = "-10px 0 0 500px";
                  fill_button_top.style.cursor = "pointer";
                  document.body.appendChild(fill_button_top);
                  fill_button_top.innerHTML = "Add all links to the queue";
                  fill_button_top.addEventListener("click",addAllLinksToTheQueue,false);
                }
                if (!reset_button_top) {
                  reset_button_top = document.createElement('div');
                  reset_button_top.id="queue_fill_button";
                  reset_button_top.style.color = "red";
                  reset_button_top.style.border = "1px solid black";
                  reset_button_top.style.position = "absolute";
                  reset_button_top.style.padding = "10px";
                  reset_button_top.style.top = "20px";
                  reset_button_top.style.margin = "-10px 0 0 725px";
                  reset_button_top.style.cursor = "pointer";
                  document.body.appendChild(reset_button_top);
                  reset_button_top.innerHTML = "Reset the queue";
                  reset_button_top.addEventListener("click",resetTheQueue,false);
                }
            }
            catch(exc) {}

            async function addAllLinksToTheQueue(click_event) {
                var r = confirm("Are you sure that you want to add all the cards on this page to the current queue?");
                if (r == true) {
                    add_links_to_queue_from_query_page(document);
                }
            }

           async function resetTheQueue(click_event) {
                var r = confirm("Are you sure that you want to remove all the cards from current queue?");
                if (r == true) {
                    reset_queue(document);
                }
            }
        }
        if (locationIsNS) {
            try {
                if(!cards_button_top) {
                    let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));
                    cards_button_top = document.createElement("div");
                    cards_button_top.setAttribute('class',"bel");
                    if(queue_holder !== null && queue_holder.length > 0) {
                        cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a><div class=\"notificationnumber refreshable\" style=\"background-color:green\">" + queue_holder.length + "</div></div>";
                    }
                    else {
                        cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a></div>";
                    }
                }

                (document.getElementsByClassName("belspacer belspacermain")[0]).before(cards_button_top);

            }
            catch(exc) {}

            if(window.location.href.startsWith("https://www.nationstates.net/page=deck/card=")) { //single card page

                let 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) {}
                    }

                }

            }
            else
                if(window.location.href.startsWith("https://www.nationstates.net/page=deck")
                   ||window.location.href.match(/https:\/\/www\.nationstates\.net\/(nation|container)=[A-Za-z0-9_-]+\/page=deck/)) { //deck page, may be lootbox page or another
                    if (window.location.href.includes("/value_deck=1")) {
                        var card_link_elts = document.getElementsByClassName("cardnameblock");
                        for(i = 0; i < card_link_elts.length; i++) {
                            button_elt = document.createElement("button");
                            button_elt.textContent = "Add to cards queue";
                            button_elt.addEventListener('click',add_in_queue_valdeck,false);
                            button_elt.classList.add("deckcard-token");
                            card_link_elts[i].parentNode.insertBefore(button_elt,card_link_elts[i]);
                        }

                    } else {
                        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;
                            }
                        }

                        // Unneeded condition
                        // 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]);

                        }

                        // }
                    }

                }

            let cards_flags = document.getElementsByClassName("deckcard-flag");

            for(i = 0; i < cards_flags.length; i++) {
                cards_flags[i].addEventListener("click",toggleCardTitle,false);
            }
        }

        function toggleCardTitle(event) {
            let card_title_element = event.target.parentNode.getElementsByClassName("deckcard-title");
            if(card_title_element.length != 0) {
                if(card_title_element[0].style.visibility == "hidden") {
                    card_title_element[0].style.visibility = "visible";
                }
                else {
                    card_title_element[0].style.visibility = "hidden";
                }
            }
        }

        async function get_next_element_in_queue(click_event) {

            click_event.target.removeEventListener('click',get_next_element_in_queue,false);

            try {

                let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));

                let 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_valdeck(click_event) {

            click_event.target.removeEventListener('click',add_in_queue,false);

            let 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.nextElementSibling.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

        }

        async function add_links_to_queue_from_query_page(document) {

            let 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);
            }

             document
                 .querySelectorAll('a')
                 .forEach(function (anchor) {
                 if (anchor.getAttribute('href') !== 'submit.sh') {
                     try {
                         //add link to card info to queue
                         let url = anchor.getAttribute('href').replace('https://www.nationstates.net', '');
                         queue_holder.push(url);
                     }
                     catch(exc) {}
                 }
             });

            await GM.setValue("NS_Cards_Queue",JSON.stringify(queue_holder)); //store back updated queue

        }

         async function reset_queue(document) {

            let queue_holder = await GM.getValue("NS_Cards_Queue", null);

            await GM.setValue("NS_Cards_Queue",JSON.stringify([])); //store back empty queue

        }

        async function add_in_queue(click_event) {

            click_event.target.removeEventListener('click',add_in_queue,false);

            let 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
                console.log(click_event.target.parentNode.nextElementSibling.firstElementChild.getAttribute("href"));
                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

        }

    })();



Screenshot:
Image

The first button adds all cards on that page to the queue, the second button resets the queue, both buttons have a confirm dialog.
I also added a keypress detection based on dithpri/9003's keybind script. If you hit the n key it will activate the next queue item button.
Thus, if you combine this script with the keybind script you can quite easily move through a lot of cards to place bids.

I could use some testers for these two new features. And yes the two buttons are still very rudimentary, they will get changed to proper buttons when I feel like it :p . For now they are functional.

I might add a reset button next to the button on the NS page as well if people like it.

By the way, I would very much like to host this script on my NS scripts Github project, but there a multiple authors here, and I don't want to hijack this script and claim it as my own.
One of the benefits then would be that we could auto-update it with new features. It also allows multiple people to add features and solve bugs in an organized way.

The disadvantages of course are that contributors need to go through that GitHub project to make changes.

Basically I'm only adding it in to that Github project if all authors agree to it. That repro has a GNU General Public License v3.0 and the license of the previous versions are a bit murky, so it would only be possible if all authors agree I think.
Last edited by Vylixan on Sun Jul 05, 2020 2:10 am, edited 2 times in total.

User avatar
Racoda
Attaché
 
Posts: 67
Founded: Aug 12, 2014
Liberal Democratic Socialists

Postby Racoda » Sun Jul 05, 2020 7:20 pm

That's a great feature! (But the indentation is really weird!!)

For my part, I agree to release my contributions under the GNU GPL 3.0

User avatar
Evrigenis
Lobbyist
 
Posts: 16
Founded: Dec 12, 2019
Left-Leaning College State

Little edits: Advanced Queries and Viewing Mode

Postby Evrigenis » Fri Sep 18, 2020 3:12 pm

This version now also works on the advanced query pages and allows you to choose a mode, for example the trades history or the owners of a card. This can be useful if you're looking at several cards and want other info about them that are not shown on the main card page. It defaults to the main card page, but changing it is easy.

I also ran it through an auto-indenter, so the style is a little less funky.

Code: Select all
// ==UserScript==
// @namespace    NS_Cards_Queue_Script
// @name         Anozia feat. RCSA & DGES - Reçu's Feature Requests - and Sitethiefs QueryQueueFiller and y0's mode
// @version      1.2.1
// @description  A queue to put the url of cards from packs and load them later from another card, now also able to add cards from the query page and the advanced query page, and with the option to go to different parts of the page
// @author       Anozia
// @contributors Racoda, DGES, Sitethief, and borromeanWhyKnot (y0)
// @include      *azure.nsr3n.info/card_queries/*
// @include      *www.nationstates.net/*
// @require      https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.min.js?a4098
// @run-at       document-idle
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM.deleteValue
// ==/UserScript==

/*
* Keybinds:
* [n]ext queue item
*/

/*
* change line 30 to one of the following options to change the viewing mode
* var mode = ""
* mode = "/owners=1"
* mode = "/finds_history=1"
* mode = "/trades_history=1"
* mode = "/show_collections=1"
*/
var mode = "";

(async function() {
    // next queue item
    Mousetrap.bind(['n', 'N'], function(event) {
        get_next_element_in_queue(event);

    });
    var button_elt;
    var div_elt;
    var i;
    var cards_button_top;
    var fill_button_top;
    var reset_button_top;

    let location = window.location.href
    let locationIsNS = location.includes('nationstates.net');
    let locationIsQueryServer = location.includes('azure.nsr3n.info/card_queries/get_daemon.sh') || location.includes('azure.nsr3n.info/card_queries/get_daemon_advanced.sh')

    if (locationIsQueryServer) {
          try {
            if(!fill_button_top) {
              fill_button_top = document.createElement('div');
              fill_button_top.id="queue_fill_button";
              fill_button_top.style.color = "red";
              fill_button_top.style.border = "1px solid black";
              fill_button_top.style.position = "absolute";
              fill_button_top.style.padding = "10px";
              fill_button_top.style.top = "70px";
              fill_button_top.style.left = "-300px";
              fill_button_top.style.margin = "-10px 0 0 500px";
              fill_button_top.style.cursor = "pointer";
              document.body.appendChild(fill_button_top);
              fill_button_top.innerHTML = "Add all links to the queue";
              fill_button_top.addEventListener("click",addAllLinksToTheQueue,false);
            }
            if (!reset_button_top) {
              reset_button_top = document.createElement('div');
              reset_button_top.id="queue_fill_button";
              reset_button_top.style.color = "red";
              reset_button_top.style.border = "1px solid black";
              reset_button_top.style.position = "absolute";
              reset_button_top.style.padding = "10px";
              reset_button_top.style.top = "70px";
              reset_button_top.style.left = "-300px";
              reset_button_top.style.margin = "-10px 0 0 725px";
              reset_button_top.style.cursor = "pointer";
              document.body.appendChild(reset_button_top);
              reset_button_top.innerHTML = "Reset the queue";
              reset_button_top.addEventListener("click",resetTheQueue,false);
            }
        }
        catch(exc) {}

        async function addAllLinksToTheQueue(click_event) {
            var r = confirm("Are you sure that you want to add all the cards on this page to the current queue?");
            if (r == true) {
                add_links_to_queue_from_query_page(document);
            }
        }

       async function resetTheQueue(click_event) {
            var r = confirm("Are you sure that you want to remove all the cards from current queue?");
            if (r == true) {
                reset_queue(document);
            }
        }
    }
    if (locationIsNS) {
        try {
            if(!cards_button_top) {
                let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));
                cards_button_top = document.createElement("div");
                cards_button_top.setAttribute('class',"bel");
                if(queue_holder !== null && queue_holder.length > 0) {
                    cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a><div class=\"notificationnumber refreshable\" style=\"background-color:green\">" + queue_holder.length + "</div></div>";
                }
                else {
                    cards_button_top.innerHTML = "<div class=\"belcontent\"><a href=\"/page=deck\" class=\"bellink\"><i class=\"icon-cards\"></i>CARDS</a></div>";
                }
            }

            (document.getElementsByClassName("belspacer belspacermain")[0]).before(cards_button_top);

        }
        catch(exc) {}

        if(window.location.href.startsWith("https://www.nationstates.net/page=deck/card=")) { //single card page

            let 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) {}
                }

            }

        }
        else
            if(window.location.href.startsWith("https://www.nationstates.net/page=deck")
               ||window.location.href.match(/https:\/\/www\.nationstates\.net\/(nation|container)=[A-Za-z0-9_-]+\/page=deck/)) { //deck page, may be lootbox page or another
                if (window.location.href.includes("/value_deck=1")) {
                    var card_link_elts = document.getElementsByClassName("cardnameblock");
                    for(i = 0; i < card_link_elts.length; i++) {
                        button_elt = document.createElement("button");
                        button_elt.textContent = "Add to cards queue";
                        button_elt.addEventListener('click',add_in_queue_valdeck,false);
                        button_elt.classList.add("deckcard-token");
                        card_link_elts[i].parentNode.insertBefore(button_elt,card_link_elts[i]);
                    }

                } else {
                    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;
                        }
                    }

                    // Unneeded condition
                    // 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]);

                    }

                    // }
                }

            }

        let cards_flags = document.getElementsByClassName("deckcard-flag");

        for(i = 0; i < cards_flags.length; i++) {
            cards_flags[i].addEventListener("click",toggleCardTitle,false);
        }
    }

    function toggleCardTitle(event) {
        let card_title_element = event.target.parentNode.getElementsByClassName("deckcard-title");
        if(card_title_element.length != 0) {
            if(card_title_element[0].style.visibility == "hidden") {
                card_title_element[0].style.visibility = "visible";
            }
            else {
                card_title_element[0].style.visibility = "hidden";
            }
        }
    }

    async function get_next_element_in_queue(click_event) {

        click_event.target.removeEventListener('click',get_next_element_in_queue,false);

        try {

            let queue_holder = JSON.parse(await GM.getValue("NS_Cards_Queue", null));

            let 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_valdeck(click_event) {

        click_event.target.removeEventListener('click',add_in_queue,false);

        let 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.nextElementSibling.getAttribute("href") + mode); //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

    }

    async function add_links_to_queue_from_query_page(document) {

        let 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);
        }

         document
             .querySelectorAll('a')
             .forEach(function (anchor) {
             if (anchor.getAttribute('href') !== 'submit.sh') {
                 try {
                     //add link to card info to queue
                     let url = anchor.getAttribute('href').replace('https://www.nationstates.net', '') + mode;
                     queue_holder.push(url);
                 }
                 catch(exc) {}
             }
         });

        await GM.setValue("NS_Cards_Queue",JSON.stringify(queue_holder)); //store back updated queue

    }

     async function reset_queue(document) {

        let queue_holder = await GM.getValue("NS_Cards_Queue", null);

        await GM.setValue("NS_Cards_Queue",JSON.stringify([])); //store back empty queue

    }

    async function add_in_queue(click_event) {

        click_event.target.removeEventListener('click',add_in_queue,false);

        let 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
            //var mode = "";


            console.log(click_event.target.parentNode.nextElementSibling.firstElementChild.getAttribute("href") + mode);
            queue_holder.push(click_event.target.parentNode.nextElementSibling.firstElementChild.getAttribute("href") + mode); //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

    }

})();
Last edited by Evrigenis on Fri Sep 18, 2020 3:41 pm, edited 1 time in total.


Advertisement

Remove ads

Return to Trading Cards

Who is online

Users browsing this forum: No registered users

Advertisement

Remove ads