Skip to content

Instantly share code, notes, and snippets.

@Jack-Works

Jack-Works/index.js

Created Dec 29, 2019
Embed
What would you like to do?
Dave Conservatoire helper
// ==UserScript==
// @name Dave Conservatoire helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.daveconservatoire.org/*
// @grant none
// ==/UserScript==
// Including https://github.com/kapetan/browser-beep/blob/master/index.js
/**
* How to use:
* Press 1~9 to selectAnswer;
* Press Space or Enter to submitAnswer;
* Press "r" to replayAudio;
*/
/**
* @param {string} x
* @return {HTMLElement[]}
*/
function $(x) {
return Array.from(document.querySelectorAll(x))
}
setInterval(() => {
const btn = $('a').filter(x => ['Start Exercise', 'Start again'].includes(x.innerText))[0]
if (btn) {
beep({ interval: 225, frequency: 700 })(2)
btn.click()
}
}, 200)
document.addEventListener('keydown', async e => {
if ([' ', 'Enter'].includes(e.key)) return (e.preventDefault(), submitAnswer())
if (['r'].includes(e.key)) return replayAudio()
const num = parseInt(e.key, 10)
if (num) return selectAnswer(num)
})
function submitAnswer() {
const lastLife = $('.icon-heart').length
$('input').filter(x => x instanceof HTMLInputElement && x.value === "Check Answer")[0]?.click()
setTimeout(() => {
const nextLife = $('.icon-heart').length
if (lastLife !== nextLife) beep({interval: 225, frequency: 600})(1)
}, 50)
}
/**
* @param {number} index
*/
function selectAnswer(index) {
$('#solutionarea li')[index - 1]?.querySelector('button')?.click()
}
function replayAudio() {
$('a').filter(x => x.innerText === "Play Again")[0]?.click()
}
var FREQUENCY = 440
var INTERVAL = 250
var RAMP_VALUE = 0.00001
var RAMP_DURATION = 1
/**
* @param {{ context?: AudioContext; frequency?: number; interval?: number; }} options
*/
function beep(options) {
if (!options) options = {}
var context = options.context || new window.AudioContext()
var frequency = options.frequency || FREQUENCY
var interval = options.interval || INTERVAL
var play = function () {
var currentTime = context.currentTime
var osc = context.createOscillator()
var gain = context.createGain()
osc.connect(gain)
gain.connect(context.destination)
gain.gain.setValueAtTime(gain.gain.value, currentTime)
gain.gain.exponentialRampToValueAtTime(RAMP_VALUE, currentTime + RAMP_DURATION)
osc.onended = function () {
gain.disconnect(context.destination)
osc.disconnect(gain)
}
osc.type = 'sine'
osc.frequency.value = frequency
osc.start(currentTime)
osc.stop(currentTime + RAMP_DURATION)
}
var beep = /**
* @param {number} times
*/
function (times) {
if (!times) times = 1
;(function loop (i) {
play()
if (++i < times) setTimeout(loop, interval, i)
})(0)
}
beep.destroy = function () {
if (!options.context) context.close()
}
return beep
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment