Last active
August 29, 2016 22:23
-
-
Save priestc/715bd684af5bb841ee99aead248cf641 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
function get_title() { | |
return document.getElementById("itemTitle").innerText.trim(); | |
} | |
function voice_announce(msg) { | |
var utter = new SpeechSynthesisUtterance(msg); | |
window.speechSynthesis.speak(utter); | |
} | |
function total_seconds_remaining() { | |
var remaining_str = document.getElementById("vi-cdown_timeLeft").innerText.trim(); | |
var result = remaining_str.match(/^(\d+)s$/); | |
if(result) { | |
return parseInt(result[1]); | |
} | |
var result = remaining_str.match(/^(\d+)m (\d+)s$/); | |
if(result) { | |
var minutes = parseInt(result[1]); | |
var seconds = parseInt(result[2]); | |
return seconds + 60 * minutes | |
} | |
var result = remaining_str.match(/^(\d+)h (\d+)m (\d+)s$/); | |
if(result) { | |
var hours = parseInt(result[1]); | |
var minutes = parseInt(result[2]); | |
var seconds = parseInt(result[3]); | |
return seconds + 60 * minutes + (60 * 60 * hours) | |
} | |
// todo: return total seconds when there is more than 1 day remaining. | |
// for now this case will be ignored because no alarm will sound when more | |
// than one day remains | |
return 9999999 | |
} | |
var did_15min_beep = false; | |
var did_double_beep = false; | |
var did_1m_announce = false; | |
var did_10s_beep = false; | |
function watch_auction() { | |
var seconds_remaining = total_seconds_remaining(); | |
console.log(seconds_remaining, "seconds to go"); | |
if(!seconds_remaining) { | |
return; //auction has ended | |
} | |
if(seconds_remaining <= 10 && !did_10s_beep) { | |
triple_beep(); | |
did_10s_beep = true; | |
} | |
if(seconds_remaining <= 60 && !did_1m_announce) { | |
notifyMe(60); | |
voice_announce("one minute! " + get_title()); | |
did_1m_announce = true; | |
} | |
if(seconds_remaining < 5 * 60 && !did_double_beep) { | |
double_beep(); | |
did_double_beep = true; | |
} | |
if(seconds_remaining < 15 * 60 && !did_15min_beep) { | |
beep(500, 440, 0.2); | |
did_15min_beep = true; | |
} | |
var wait = 10000; // iterate every 10 seconds over 5 minutes | |
if(seconds_remaining <= 5 * 60) { | |
wait = 1000; //iterate every second when under 5 minutes | |
} | |
setTimeout(watch_auction, wait); | |
} | |
function do_beep(seconds) { | |
// do the actual beeping depending on how many seconds are left in the auction | |
// made into a seperate function to aide in testing. | |
if(seconds <= 60 * 5) { | |
if(seconds <= 60 * 2) { | |
if(seconds <= 30) { | |
if(seconds <= 10) { | |
if(seconds <= 2) { | |
triple_beep(); | |
return false; | |
} | |
} | |
} else if(seconds % 10 == 0) { | |
beep(300, 460, 0.4); // beep once every 10 seconds under 2 minutes | |
} | |
} else if(seconds % 60 == 0) { | |
beep(500, 440, 0.2); // beep once every minute below 5 minutes | |
} | |
} | |
return true; | |
} | |
function double_beep() { | |
beep(50, 480, 0.7, 'sine', function() { | |
beep(50, 510, 0.8); | |
}); | |
} | |
function triple_beep() { | |
beep(50, 480, 0.7, 'sine', function() { | |
beep(50, 510, 0.8, 'sine', function() { | |
beep(50, 560, 0.9); | |
}); | |
}); | |
} | |
//if you have another AudioContext class use that one, as some browsers have a limit | |
var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext); | |
//All arguments are optional: | |
//duration of the tone in milliseconds. Default is 500 | |
//frequency of the tone in hertz. default is 440 | |
//volume of the tone. Default is 1, off is 0. | |
//type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine. | |
//callback to use on end of tone | |
function beep(duration, frequency, volume, type, callback) { | |
var oscillator = audioCtx.createOscillator(); | |
var gainNode = audioCtx.createGain(); | |
oscillator.connect(gainNode); | |
gainNode.connect(audioCtx.destination); | |
if (volume){gainNode.gain.value = volume;}; | |
if (frequency){oscillator.frequency.value = frequency;} | |
if (type){oscillator.type = type;} | |
if (callback){oscillator.onended = callback;} | |
oscillator.start(); | |
setTimeout(function(){oscillator.stop()}, (duration ? duration : 500)); | |
}; | |
document.addEventListener('DOMContentLoaded', function () { | |
if (!Notification) { | |
return; | |
} | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
}); | |
function notifyMe(seconds) { | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
else { | |
var notification = new Notification('Auction Ending', { | |
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png', | |
body: get_title() + " ending in " + seconds + " seconds", | |
}); | |
notification.onclick = function () { | |
window.open("http://stackoverflow.com/a/13328397/1269037"); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment