Better Player

QoL improvements for the Deezer player(bar)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Better Player
// @description QoL improvements for the Deezer player(bar)
// @author      bertigert
// @version     1.0.3
// @icon        https://www.google.com/s2/favicons?sz=64&domain=deezer.com
// @namespace   Violentmonkey Scripts
// @match       https://www.deezer.com/*
// @grant       none
// ==/UserScript==


(function() {
    "use strict";

    class Logger {
        constructor(prefix, debug=false) {
            this.prefix = prefix;
            this.should_debug = debug;
        }

        debug(...args) {if (this.should_debug) console.debug(this.prefix, ...args);}
        log(...args) {console.log(this.prefix, ...args);}
        warn(...args) {console.warn(this.prefix, ...args);}
        error(...args) {console.error(this.prefix, ...args);}
    }

    class WebpackPatch {
        PATCHES = [
            {
                find: ["getStorageKey:e=>`ALERT_DISMISSED_${e}"],
                replacements: [
                    // remaining time
                    {
                        match: /(_renderCurrentValue.*?)this._formatValue\(([a-z])\)(.*?"data-testid":"elapsed_time")/, //(.*?"data-testid":"remaining_time"},[a-z])/,
                        replace: (_, $1, $2, $3, $4) => `${$1}(${WebpackPatcher.ph.data}.altMode?"-":"")+this._formatValue(${WebpackPatcher.ph.data}.altMode?this.props.max-${$2}:${$2})${$3},onMouseUp:()=>${WebpackPatcher.ph.functions}.toggle()` //${$4}`+(${WebpackPatcher.placeholders.data}.altMode?" / "+this._formatValue(dzPlayer.getTrackListDuration()):"")`
                    },
                    // better play previous
                    {
                        match: /(isDisabled:)![a-zA-Z]\|\|(!n.prev,onClick:\(\)=>{\$[a-zA-Z]\.[a-zA-Z]+\.control\.prevSong\(\)})/,
                        replace: (_, $1, $2) => `${$1}${$2}`
                    }
                ]
            }
        ]
        data = {
            altMode: false,
        }
        functions = {
            toggle: () => {
                this.data.altMode = !this.data.altMode;
            }
        }

        constructor() {
            this.wait_for_webpack_patcher_and_patch = this.wait_for_webpack_patcher_and_patch.bind(this);
            this.wait_for_webpack_patcher_and_patch();
        }

        wait_for_webpack_patcher_and_patch() {
            if (window.WebpackPatcher) {
                logger.log("Registering webpack patches");
                window.WebpackPatcher.register({
                    name: "BetterPlayer",
                    data: this.data,
                    functions: this.functions
                }, this.PATCHES);
            } else if (!window.webpackJsonpDeezer) {
                setTimeout(this.wait_for_webpack_patcher_and_patch, 0);
            } else {
                logger.warn("Webpack array found, but not patcher, stopping");
            }
        }
    }

    class DzPlayerPatch {
        static REPEAT_MODES = {
            NO_REPEAT: 0,
            REPEAT_ALL: 1,
            REPEAT_ONE: 2,
        }

        constructor() {
            this.patch_play_previous();
        }

        patch_play_previous() {
            const patch_play_previous = () => {
                logger.log("Hooking dzPlayer.control.prevSong");
                const orig_play_previous = window.dzPlayer.control.prevSong;
                window.dzPlayer.control.prevSong = async function() {
                    if (
                        window.dzPlayer.getPosition() > 5 ||
                        window.dzPlayer.getRepeat() !== DzPlayerPatch.REPEAT_MODES.REPEAT_ALL && window.dzPlayer.getTrackListIndex() === 0
                    ) {
                        window.dzPlayer.control.seek(0);
                    } else {
                        orig_play_previous.call(window);
                    }
                }
            }

            const wait_for_dzplayer = setInterval(() => {
                if (window?.dzPlayer?.control?.prevSong) {
                    clearInterval(wait_for_dzplayer);
                    patch_play_previous();
                }
            }, 100);
        }
    }


    const logger = new Logger("[Better Player]", false);

    new WebpackPatch();
    new DzPlayerPatch();
})();