User:Blue-Haired Lawyer/ogg.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Blue-Haired Lawyer/ogg. |
/* play ogg sounds inline */
var soundElement;
var nowPlaying = 0;
var soundStopped;
function playPauseSound(evnt) {
var e = window.event ? window.event : evnt;
var obj = window.event ? window.event.srcElement : evnt.target;
// Search the tree to see id there's a link
var depth = 0;
while(obj && obj.parentNode && depth < 5) {
if(obj.tagName == "A") {
// Is this link a ogg file
var m = obj.href.match(/\.([^\.\/]*)$/);
if(!m) return true;
var extension = m[1];
if(extension != "ogg") return true;
// just make sure this isn't a file descripted page
if(obj.pathname.substr(0, 6) == "/wiki/") return true;
// only act upon left clicks
if(e.which != 1) return true;
// if user clicks on a sound during the playback
// of another sound, pause the first
if(soundElement.src != obj.href && nowPlaying == 1) {
soundElement.pause();
nowPlaying = 0;
}
// set up a new AUDIO tag when appropriate
if(!soundElement.src || soundElement.src != obj.href) {
soundElement = document.createElement("AUDIO");
soundElement.src = obj.href;
soundElement.addEventListener("pause", soundStopped);
soundElement.addEventListener("ended", soundStopped);
}
// play or pause as appropriate
if(nowPlaying == 1) {
soundElement.pause();
} else {
soundElement.play();
nowPlaying = 1;
}
if (typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else {
e.returnValue = false;
}
return false;
}
obj = obj.parentNode;
depth++;
}
}
function soundStopped() {
nowPlaying = 0;
}
// Does this browser support the audio element and play ogg sounds?
var soundElement = document.createElement("AUDIO");
if(soundElement.play && soundElement.canPlayType) {
var canPlay = soundElement.canPlayType('application/ogg');
if(canPlay != "no" && canPlay != "") {
document.addEventListener("click", playPauseSound);
}
}