Garmin Connect: sort activity course dropdown

Sort garmin connect activity course dropdown alphabetically

当前为 2025-07-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Garmin Connect: sort activity course dropdown
// @namespace    http://tampermonkey.net/
// @description  Sort garmin connect activity course dropdown alphabetically
// @author       You
// @match        https://connect.garmin.com/modern/activity/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=garmin.com
// @grant        none
// @license      MIT
// @version      0.1-2025-06-02
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    function sortList(ul) {
        let new_ul = ul.cloneNode(false);
        let lis = [];
        for (let i = ul.childNodes.length; i--;) {
            if (ul.childNodes[i].nodeName === 'LI') {
                lis.push(ul.childNodes[i]);
            }
        }

        lis.sort(function (a, b) {
            let aText = a.childNodes[0].textContent;
            let bText = b.childNodes[0].textContent;

            if (a.getAttribute("data-value") == -1) {
                return -1;
            }
            if (b.getAttribute("data-value") == -1) {
                return 1;
            }

            // sorts case-insensitively and handles numbers correctly (e.g. "7" < "10")
            return aText.localeCompare(bText, undefined, {
                numeric: true,
                sensitivity: 'base'
            });
        });

        for (let i = 0; i < lis.length; i++) {
            new_ul.appendChild(lis[i]);
        }
        ul.parentNode.replaceChild(new_ul, ul);
    }

    var dropdownParent = "#course-dropdown + div.dropdown + div.dropdown";
    // don't know of way to be sure that the drop-down was fully populated,
    // so we sort on every click
    function installHandler(elem) {
        var dropdown = dropdownParent + " ul[role=menu]";
        elem.addEventListener('click', function(e) {
            sortList(elem.querySelector("ul[role=menu]"));
        });
    }
    function runWhenReady(readySelector, callback) {
        var numAttempts = 0;
        var tryNow = function() {
            var elem = document.querySelector(readySelector);
            if (elem) {
                callback(elem);
            } else {
                numAttempts++;
                if (numAttempts >= 34) {
                    console.warn('Giving up after 34 attempts. Could not find: ' + readySelector);
                } else {
                    setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
                }
            }
        };
        tryNow();
    }
    runWhenReady(dropdownParent, installHandler);
})();