AudioStation support the MediaSession API

让AudioStation支持系统级别的媒体控制(上一首、下一首、暂停/播放)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AudioStation support the MediaSession API
// @name:en      AudioStation support the MediaSession API
// @name:zh      AudioStation的MediaSession API支持
// @namespace    https://greasyfork.org/en/users/1434718-ot0kaz4
// @version      1.0
// @description  让AudioStation支持系统级别的媒体控制(上一首、下一首、暂停/播放)
// @description:en  Make AudioStation support system-level media control (Previous, Next, Pause/Play)
// @description:zh  让AudioStation支持系统级别的媒体控制(上一首、下一首、暂停/播放)
// @homepageURL  https://otokaze.me
// @license MIT
// @author       otokaze.me
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';
    GM_registerMenuCommand("配置AudioStation域名", function(){
        let domains = prompt("配置AudioStation域名(多个用逗号隔开)")
        if (domains){
            GM_setValue("allowedDomains", domains.split(','))
        }
    })
    let allowedDomains = GM_getValue("allowedDomains", []);
    const currentDomain = window.location.hostname;
    const isAllowed = allowedDomains.some(domain => currentDomain.endsWith(domain));
    if (!isAllowed) {
        console.log("当前域名未在白名单内,脚本不会执行。");
        return;
    }
    console.log("✅ 脚本已启用!当前站点:" + currentDomain);

    setInterval(function() {
        if (!navigator.mediaSession) {
            return
        }
        var main = SYNO?.SDS?.AudioStation?.Window?.getPanelScope("SYNO.SDS.AudioStation.Main")
        if (!main) {
            return
        }
        navigator.mediaSession.metadata = new MediaMetadata({
            title: main.playerPanel.Ctrl.getCurrentTitle(),
            artist: main.playerPanel.Ctrl.getCurrentArtist(),
            album: main.playerPanel.Ctrl.getCurrentAlbum(),
            artwork: [{
                src: main.playerPanel.Ctrl.getCurrentCover(),
                // sizes: "480x480",
                // type: "image/jpeg",
            }],
        });
        navigator.mediaSession.setActionHandler('play', function() {
            main.audioPlayer.doPlay();
        });
        navigator.mediaSession.setActionHandler('pause', function() {
            main.audioPlayer.doPlay();
        });
        navigator.mediaSession.setActionHandler('previoustrack', function() {
            main.audioPlayer.doPrevious();
        });
        navigator.mediaSession.setActionHandler('nexttrack', function() {
            main.audioPlayer.doNext();
        });
    },1000)
})();