Skip to content

Instantly share code, notes, and snippets.

@clupasq
Last active August 29, 2015 14:10
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 clupasq/88c7716f1b4babf95d58 to your computer and use it in GitHub Desktop.
Save clupasq/88c7716f1b4babf95d58 to your computer and use it in GitHub Desktop.
StackOverflow New Question Sound Notification
(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();
}());
@clupasq
Copy link
Author

clupasq commented Aug 17, 2015

Adapted to also work with codegolf.stackexchange.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment