AudioStation support the MediaSession API

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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