Skip to content

Instantly share code, notes, and snippets.

@lnrdgmz
Created April 13, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lnrdgmz/c33bf922d59e1ded548389f0eba7cb30 to your computer and use it in GitHub Desktop.
Save lnrdgmz/c33bf922d59e1ded548389f0eba7cb30 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Test Script 275457
// @version 1
// @include *example.com/*
// @grant GM.getResourceUrl
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @require https://raw.githubusercontent.com/lodash/lodash/4.17.5/dist/lodash.min.js
// @resource soundA https://raw.githubusercontent.com/KDE/oxygen/master/sounds/Oxygen-Im-Contact-In.ogg
// @resource soundB https://raw.githubusercontent.com/KDE/oxygen/master/sounds/Oxygen-Sys-Special.ogg
// ==/UserScript===
/**
* Settings
*/
const notificationFrequency = 45000;
/**
* Add a button for toggling sound notifications and create click handlers
*/
const enableSound = () => {
makeSound = _.throttle(makeSoundFunc, notificationFrequency, { leading: true, trailing: false })
const button = document.getElementById('toggle-btn');
button.onclick = () => {
disableSound();
}
button.innerHTML = "Disable audio notifications"
}
const disableSound = () => {
makeSound.cancel()
makeSound = _.throttle(makeSoundFunc, Infinity, { leading: false, trailing: true })
const button = document.getElementById('toggle-btn');
button.onclick = () => {
enableSound();
}
button.innerHTML = "Enable sound notification"
}
const buttonStyle = "position:absolute; top:0px; right:0px";
const toggleButton = document.createElement('button');
toggleButton.onclick = enableSound;
toggleButton.setAttribute('id', 'toggle-btn');
toggleButton.innerText = 'Enable sound notifications';
toggleButton.setAttribute('style', buttonStyle);
document.body.appendChild(toggleButton)
/**
* Create buffers with the audio files
*/
const getBuffer = (url) => {
const ctx = new AudioContext();
return fetch(url).then(res => res.arrayBuffer())
.then(res => {
const buff = ctx.decodeAudioData(res)
ctx.close();
return buff;
})
}
let aBuff, bBuff;
(async function () {
const sndAUrl = await GM.getResourceUrl('soundA');
const sndBUrl = await GM.getResourceUrl('soundB');
aBuff = await getBuffer(sndAUrl);
bBuff = await getBuffer(sndBUrl);
})()
/**
* makeSoundFunc takes a buffer of audio data and plays it through the previously created AudioContext
*/
const makeSoundFunc = (buffer) => {
console.log('duration: ' + buffer.duration);
const ctx = new AudioContext();
console.log('makeSoundFunc invoked')
const source = ctx.createBufferSource();
source.buffer = buffer;
source.connect(ctx.destination);
source.loop = false;
source.start(0);
setTimeout(() => {
ctx.close()
}, Math.floor(buffer.duration * 1000))
}
let makeSound = _.throttle(makeSoundFunc, Infinity, { leading: false, trailing: true });
function checkOpen() {
makeSound(aBuff);
}
setInterval(checkOpen, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment