返回首页

Spotify 和 Yandex Music 的 Pomodoro 计时器

本文描述了在音乐服务中创建 Pomodoro 计时器 bookmarklet 的方法。代码通过域名检测平台,通过 cookie 管理计时器并暂停播放。适合需要专注自动化的开发者。

浏览器中音乐的自动 Pomodoro
Advertisement 728x90

音乐服务番茄钟书签小工具

远程开发者常常被各种通知干扰。要保持专注,一种好方法是使用番茄工作法,结合定时自动暂停音乐。这样就不需要额外安装应用,只需一个书签小工具(bookmarklet):一段保存为浏览器书签的 JavaScript 代码,能在设定时间后自动暂停音乐。

核心思路?25 分钟后暂停 Spotify。基础代码用 setTimeout 点击暂停按钮:

setTimeout(() => { document.querySelector("[data-testid='control-button-playpause']").click()}, 5000)

这里 setTimeout 接受回调函数和毫秒延迟。选择器 [data-testid='control-button-playpause'] 定位按钮,.click() 模拟点击。25 分钟就是 1000 60 25

Google AdInline article slot

简单方案的问题及解决方案

基础脚本有几个痛点:需要手动输入、在控制台运行、找不到按钮就崩溃,而且只支持 Spotify。

这时书签小工具就派上用场了。创建书签时用 javascript: 前缀加你的代码:

javascript:setTimeout(() => {
  document.querySelector("[data-testid='control-button-playpause']")
    .click()
}, 5000)

添加按钮检查和提示:

Google AdInline article slot
(() => {
    const stopButton = document.querySelector("[data-testid='control-button-playpause']");

    if (!stopButton) {
        alert("未找到暂停按钮 😭");
        return;
    }

    setTimeout(() => {stopButton.click()}, 5000);
    alert("番茄钟启动 🍅⏰")
})()

定时器控制:启动与停止

要取消计时,需要用 Cookie 存储定时器 ID。完整代码会检查是否有活跃定时器,用 clearTimeout 停止,或启动新定时器:

javascript:(() => {
    const time = 1000 * 60 * 25;
    
    const stopButton = document.querySelector("[data-testid='control-button-playpause']");

    const timerID = document.cookie.split('; ').find(row => row.startsWith('myPomodoroMusic='))?.split('=')[1];

    if (!stopButton) {
        alert("未找到暂停按钮 😭");
        return;
    }

    if (timerID) {
        clearTimeout(Number(timerID));
        alert(`定时器已停止`);
        document.cookie = `myPomodoroMusic=${timerID}; expires=${ new Date().toUTCString() }; path=/;`;
        return;
    }

    window.myPomodoroMusic = setTimeout(() => {stopButton.click()}, time);

    document.cookie = `myPomodoroMusic=${window.myPomodoroMusic}; expires=${ new Date(Date.now() + time).toUTCString() }; path=/;`;

    alert(`定时器启动,${time / 1000 / 60} 分钟后暂停`);
})()

Cookie 会保存 ID,直到定时器到期或手动停止。刷新页面后依然有效。

支持热门音乐平台

通过 getPauseButton 函数根据域名匹配对应选择器,支持多平台:

Google AdInline article slot
  • Spotify: [data-testid='control-button-playpause']
  • 网易云音乐: [aria-label='暂停']
  • QQ音乐: [id='theme-provider'] > div > div > div:nth-of-type(2) > div > div > div:nth-of-type(2) > div > button:nth-of-type(2)
  • 酷狗音乐: [data-testid='audio-player-controls-state-button']
  • YouTube: [aria-keyshortcuts='k']

完整最终代码:

javascript:(() => {
    const time = 1000 * 60 * 25;

    const getPauseButton = () => {
        const url = window.location.href;
        const domain = new URL(url).hostname;
        switch (domain) {
            case "open.spotify.com":
                return document.querySelector("[data-testid='control-button-playpause']");
            case "music.163.com":
                return document.querySelector("[aria-label='暂停']");
            case "y.qq.com":
                return document.querySelector("[id='theme-provider'] > div > div > div:nth-of-type(2) > div > div > div:nth-of-type(2) > div > button:nth-of-type(2)");
            case "www.kugou.com":
                return document.querySelector("[data-testid='audio-player-controls-state-button']");
            case "www.youtube.com":
                return document.querySelector("[aria-keyshortcuts='k']");
            default:
                return null;
        }
    }
    
    const stopButton = getPauseButton();

    const timerID = document.cookie.split('; ').find(row => row.startsWith('myPomodoroMusic='))?.split('=')[1];

    if (!stopButton) {
        alert("未找到暂停按钮 😭");
        return;
    }

    if (timerID) {
        clearTimeout(Number(timerID));
        alert(`定时器已停止`);
        document.cookie = `myPomodoroMusic=${timerID}; expires=${ new Date().toUTCString() }; path=/;`;
        return;
    }

    window.myPomodoroMusic = setTimeout(() => {stopButton.click()}, time);

    document.cookie = `myPomodoroMusic=${window.myPomodoroMusic}; expires=${ new Date(Date.now() + time).toUTCString() }; path=/;`;

    alert(`定时器启动,${time / 1000 / 60} 分钟后暂停`);
})()

关键要点

  • 书签小工具用 Cookie 存储定时器 ID,支持无刷新停止。
  • 根据域名智能选择 Spotify、网易云音乐、QQ音乐、酷狗音乐和 YouTube 的暂停按钮。
  • 按钮检查避免错误,提示显示状态更新。
  • 无需扩展,在任意浏览器运行——适合中高级开发者。
  • 选择器依赖 data-testidaria-labelaria-keyshortcuts,更可靠。

— Editorial Team

Advertisement 728x90

继续阅读