Last active
August 29, 2015 14:10
-
-
Save clupasq/88c7716f1b4babf95d58 to your computer and use it in GitHub Desktop.
StackOverflow New Question Sound Notification
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 () { | |
'use strict'; | |
var model = { | |
alarmActive: true | |
}; | |
var controller = { | |
init: function() { | |
if (this.alreadyRunning()) { | |
console.warn('Already notifying!'); | |
return; | |
} | |
view.init(); | |
view.update(); | |
}, | |
isAlarmActive: function() { | |
return model.alarmActive; | |
}, | |
notify: function() { | |
if (model.alarmActive) { | |
model.alarmActive = false; | |
view.notify(); | |
view.update(); | |
} | |
}, | |
reEnable: function() { | |
model.alarmActive = true; | |
view.update(); | |
}, | |
alreadyRunning: function() { | |
if(window.w0lfNotifierRunning) { | |
return true; | |
} | |
window.w0lfNotifierRunning = true; | |
} | |
}; | |
var view = { | |
init: function() { | |
this.domElement = document.getElementById('questions') || document.getElementById('qlist-wrapper'); | |
this.sound = new Audio("http://www.soundjay.com/button/sounds/button-14.mp3"); | |
this.chkSoundEnabled = document.createElement('input'); | |
this.chkSoundEnabled.type = 'checkbox'; | |
this.chkSoundEnabled.checked = true; | |
var title = document.querySelector('h1'); | |
title.insertBefore(this.chkSoundEnabled, title.children[0]); | |
this.domElement.addEventListener('DOMSubtreeModified', function(){ | |
view.handleDOMChange(); | |
}); | |
}, | |
handleDOMChange: function() { | |
var newPostsPresent = this.domElement.querySelector('.new-post-activity'); | |
if (newPostsPresent) { | |
controller.notify(); | |
} else { | |
controller.reEnable(); | |
} | |
}, | |
notify: function() { | |
if(this.chkSoundEnabled.checked) { | |
this.sound.play(); | |
} | |
}, | |
update: function() { | |
var active = controller.isAlarmActive(); | |
this.domElement.style.border = active ? '3px dotted green' : '3px dotted red'; | |
} | |
}; | |
controller.init(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adapted to also work with codegolf.stackexchange.com