sepen77 / Lost Ark Inven Global - Sailing Voyage Island Sort (LAIG-SVIS)

// ==UserScript==
// @name         Lost Ark Inven Global - Sailing Voyage Island Sort (LAIG-SVIS)
// @namespace    https://openuserjs.org/users/sepen77
// @version      0.1
// @description  Sorts the islands in the region-select dropdown when viewing Sailing Voyage. A utility for Lost Ark info site Inven Global.
// @author       sepen77
// @match        https://www.invenglobal.com/lostark/world*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=invenglobal.com
// @copyright 2022, sepen77 (https://openuserjs.org/users/sepen77)
// @license MIT
// ==/UserScript==

/* jshint esversion: 6 */

console.log("LAIG-SVIS: starting up...");

const WORLD_SAILING_VOYAGE_ID = 14;

function sortRegionSelectIfSailingVoyage() {
    console.log("LAIG-SVIS: Checking if Sailing Voyage...");
    const worldSelect = document.querySelector('select#world');
    // Check if world selected is Sailing Voyage
    if (worldSelect.value == WORLD_SAILING_VOYAGE_ID.toString()) {
        console.log("LAIG-SVIS: Sailing voyage detected!");
        sortRegionSelect();
    }
}

function sortRegionSelect() {
    const regionSelect = document.querySelector('select#region');
    const regionOptions = [].slice.call(regionSelect.children);
    console.log("LAIG-SVIS: Sorting islands...");
    regionOptions.sort((a, b) => {
        if (a.text === b.text) {
            return 0;
        }
        else if (a.text > b.text) {
            return 1;
        }
        else {
            return -1;
        }
    });
    regionSelect.replaceChildren(...regionOptions);
    console.log("LAIG-SVIS: Islands sorted.");
}

(function() {
    'use strict';
    // Attach listener to sort
    console.log("LAIG-SVIS: attaching listener...");
    const worldSelect = document.querySelector('select#world');
    worldSelect.addEventListener('change', () => {
        sortRegionSelectIfSailingVoyage();
    });
    // Run initial sort
    console.log("LAIG-SVIS: running initial sort...");
    sortRegionSelectIfSailingVoyage();
})();