Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Pakirava-Datsuma/e1bedebc3dbfa5c04442089e5fff74ee to your computer and use it in GitHub Desktop.
Save Pakirava-Datsuma/e1bedebc3dbfa5c04442089e5fff74ee to your computer and use it in GitHub Desktop.
beep browser when a new DOM element appears
[
["li pre.log-line", "Error", 440],
["li pre.log-line", "Warning", 523]
].forEach(([selector, targetText, frequency]) => {
new MutationObserver(mutations =>
mutations.forEach(({ addedNodes }) =>
addedNodes.forEach(node => {
if (node.nodeType === 1 &&
(node.matches?.(selector) || node.querySelector?.(selector)) &&
node.textContent.includes(targetText)) {
console.log(`Matched: "${node.textContent.trim()}"`);
const ctx = new (AudioContext || webkitAudioContext)();
const osc = ctx.createOscillator();
osc.type = "sine";
osc.frequency.setValueAtTime(frequency, ctx.currentTime);
osc.connect(ctx.destination);
osc.start();
setTimeout(() => osc.stop(), 200);
}
})
)
).observe(document.body, { childList: true, subtree: true });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment