Letterboxd Formated Runtime

Replaces the original runtime with a formated one

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Letterboxd Formated Runtime
// @namespace   https://github.com/Tetrax-10
// @description Replaces the original runtime with a formated one
// @icon        https://tetrax-10.github.io/letterboxd-custom-images/assets/icon.png
// @license     MIT
// @version     1.1
// @match       *://*.letterboxd.com/film/*
// @run-at      document-start
// ==/UserScript==

;(async () => {
    async function waitForElement(selector, timeout = null, nthElement = 1) {
        // wait till document body loads
        while (!document.body) {
            await new Promise((resolve) => setTimeout(resolve, 10))
        }

        nthElement -= 1

        return new Promise((resolve) => {
            if (document.querySelectorAll(selector)?.[nthElement]) {
                return resolve(document.querySelectorAll(selector)?.[nthElement])
            }

            const observer = new MutationObserver(async () => {
                if (document.querySelectorAll(selector)?.[nthElement]) {
                    resolve(document.querySelectorAll(selector)?.[nthElement])
                    observer.disconnect()
                } else {
                    if (timeout) {
                        async function timeOver() {
                            return new Promise((resolve) => {
                                setTimeout(() => {
                                    observer.disconnect()
                                    resolve(false)
                                }, timeout)
                            })
                        }
                        resolve(await timeOver())
                    }
                }
            })

            observer.observe(document.body, {
                childList: true,
                subtree: true,
            })
        })
    }

    function formatTimeString(timeStr) {
        // Extract hours and minutes using regex
        const match = timeStr.match(/(\d+)h\s(\d+)m/)

        if (!match) return "" // If the input string does not match the pattern

        const hours = parseInt(match[1], 10)
        const minutes = parseInt(match[2], 10)

        // If hours is 0, return an empty string
        if (hours === 0) {
            return ""
        }

        // Build the result string
        let result = ""

        // Format hours
        result += hours === 1 ? "1 hr" : `${hours} hrs`

        // Format minutes if more than 0
        if (minutes > 0) {
            result += ` ${minutes} min${minutes > 1 ? "s" : ""}`
        }

        return result
    }

    const runtimeSelector = ".text-footer-extra.duration-extra[data-original-title]"
    const runtime = await waitForElement(runtimeSelector)
    const formatedRuntime = runtime.dataset.originalTitle || ""

    const reFormatedRuntime = formatTimeString(formatedRuntime)

    if (reFormatedRuntime) {
        runtime.dataset.originalTitle = runtime.innerText
        runtime.innerText = reFormatedRuntime
    }
})()