Better Player

QoL improvements for the Deezer player(bar)

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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();
})();