Skip to content

Instantly share code, notes, and snippets.

@arctic-hen7
Created April 11, 2022 07:40
Show Gist options
  • Select an option

  • Save arctic-hen7/f0110791162c765032ad66c3c95c621c to your computer and use it in GitHub Desktop.

Select an option

Save arctic-hen7/f0110791162c765032ad66c3c95c621c to your computer and use it in GitHub Desktop.

Revisions

  1. arctic-hen7 created this gist Apr 11, 2022.
    34 changes: 34 additions & 0 deletions pvpn-server-loads.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // RUN THIS AT <https://protonvpn.com/vpn-servers/>

    SELECTOR = "#collapseNLfree > div:nth-child(1) > ul:nth-child(1) > .list-group-item"; // Change this to change the servers listed, this defaults to the Netherlands free servers

    // As Proton change their website design, this will likely need to be changed
    freeServers = document.querySelectorAll(SELECTOR);

    loads = Array.from(freeServers).map(elem => {
    const [name, load] = elem.innerText.split("Load ");
    return name.trim() + ": " + load.trim();
    });
    // This just sorts the servers numerically
    ordered = loads.sort((a, b) => {
    // We extract the server number from between the `#` and the `:` (as in `NL-FREE#33: 71%`)
    const numA = a.substring(a.indexOf("#") + 1, a.indexOf(":"));
    const numB = b.substring(b.indexOf("#") + 1, b.indexOf(":"));
    return numA - numB;
    });
    list = ordered.join("\n");

    // Get the fastest server by load
    // We set an initial value of 200% to make sure we get something
    // If all servers have the same load, this will just return the first server in the list
    fastest = loads.reduce(([lowestLoadServer, lowestLoadVal], curr) => {
    const currLoadVal = parseInt(curr.substring(curr.indexOf(": ") + 2, curr.length - 1));
    const currLoadServer = curr.substring(0, curr.indexOf(":"));
    return currLoadVal < lowestLoadVal ? [currLoadServer, currLoadVal] : [lowestLoadServer, lowestLoadVal];
    }, [null, 200]);
    fastestMsg = `Fastest server is '${fastest[0]}' with a load of ${fastest[1]}%.`;

    // --- OUTPUTS ---
    // `fastest`: array with two elements (fastest server name, fastest server load as a percentage)
    // `fastestMsg`: information about the fastest server, formatted for human reading
    // `list`: a human-readable list of all servers and their loads