Garmin Connect: sort calendar workout list

Sorts the workout list in the Garmin Connect calendar

目前為 2025-07-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Garmin Connect: sort calendar workout list
// @namespace    http://tampermonkey.net/
// @version      2025-07-13
// @description  Sorts the workout list in the Garmin Connect calendar
// @author       flowstate
// @match        https://connect.garmin.com/modern/calendar
// @icon         https://www.google.com/s2/favicons?sz=64&domain=garmin.com
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

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

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

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


    function onClose() {
        setTimeout(init, 0);
    }

    function onWorkoutList(elem) {
        document.querySelector(closeButton).addEventListener("click", onClose);
        sortList(document.querySelector(dropdown))
    }

    const dropdown = ".calendar-add-workout-list .sidebar-list";
    const item = ".calendar-add-workout-list .sidebar-item";
    const closeButton = ".calendar-add-workout button.close"
    function waitForElement(readySelector, callback) {
        const tryNow = function () {
            const elem = document.querySelector(readySelector);
            if (elem) {
                callback(elem);
            } else {
                setTimeout(tryNow, 300);
            }
        };
        tryNow();
    }

    function init() {
        waitForElement(item, onWorkoutList);
    }

    init();
})();