Created
April 13, 2018 23:41
-
-
Save lnrdgmz/c33bf922d59e1ded548389f0eba7cb30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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