当特定单词出现在页面上时,如何让 Tampermonkey 用户脚本播放声音?

How can I have a Tampermonkey userscript play a sound when a specific word appears on a page?

在聊天室中,当有人将某个词粘贴到聊天中时,我希望浏览器发出声音来提醒我。

看来 Tampermonkey 应该可以做到这一点,但我不知道如何开始 searching turns up few, and inapplicable results
(这个 other question 看起来很接近,但无论如何答案只是每 5 秒播放一次声音!)

当通过 AJAX 出现目标词时,用户脚本如何提醒?

设置一个MutationObserver来监视目标词。然后使用HTML5 Audio播放想要的声音。

这个 完整的工作脚本 监视文本 "just now" 并在该短语出现时播放悦耳的曲调:

// ==UserScript==
// @name     _Play sound when word appears on a page
// @include  https://stackexchange.com/questions?tab=realtime
// @grant    none
// ==/UserScript==
/* eslint-disable no-multi-spaces */

//-- Set the watched terms using the awesome power of regex.
var watchRegx = /\bjust now\b/i;
var alrtSound = new Audio ("data:audio/mp3;base64,SUQzAwAAAAAAIVRYWFgAAAAXAAAARW5jb2RlZCBieQBMYXZmNTIuMTYuMP/7kGQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEluZm8AAAAPAAAACAAADrAAICAgICAgICAgICAgQEBAQEBAQEBAQEBAYGBgYGBgYGBgYGBgYICAgICAgICAgICAgKCgoKCgoKCgoKCgoKDAwMDAwMDAwMDAwMDg4ODg4ODg4ODg4ODg////////////////AAAAOUxBTUUzLjk5cgGqAAAAAAAAAAAUgCQElk4AAIAAAA6wvc1zzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVU=");

//-- Customize this next for your page or get lazy and use "body".
var cntnrNode = document.querySelector ("#mainArea");
if (typeof cntnrNode === "undefined")   console.error ("TM: Container node not found.");
else {
    var wordObsrv = new MutationObserver (alertOnWord);
    wordObsrv.observe (document.body, {childList: true, subtree: true} );
}
function alertOnWord (mutationRecords) {
    mutationRecords.forEach (muttn => {
        if (muttn.type === "childList"  &&  typeof muttn.addedNodes === "object") {
            muttn.addedNodes.forEach (newNode => {
                /*-- Restrict the kinds of nodes watched for better performance
                    and/or to avoid false positives.
                */
                if (newNode.nodeName === "DIV"  ||  newNode.nodeName === "SPAN"  ||  newNode.nodeName === "P") {
                    if (watchRegx.test (newNode.textContent) ) {
                        console.log ("Found new instance of target term!");
                        alrtSound.play ();
                    }
                }
            } );
        }
    } );
}

使用 Tampermonkey、Violentmonkey 或类似工具安装该脚本,然后访问 the Stack Exchange Realtime page

每次出现新的 post 时您都会听到声音 -- 通常每分钟出现几次。