From 5fc1ac4db9e7b9ed1efaf87100c1d68ec5c556ff Mon Sep 17 00:00:00 2001 From: Marco Biedermann Date: Sun, 31 Jan 2016 21:37:36 +0100 Subject: [PATCH 01/11] improve performance - hoist variables to top - cache array.length - cache document object - reduce filesize --- loudlinks-1.0.js | 24 ++++++++++++++++-------- loudlinks-1.0.min.js | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/loudlinks-1.0.js b/loudlinks-1.0.js index 55c5e79..faeb6b4 100644 --- a/loudlinks-1.0.js +++ b/loudlinks-1.0.js @@ -1,13 +1,14 @@ -var loudlinks = (function(){ - +var loudlinks = (function(document) { + // Create audio element and make it awesome var generateAudioElement = (function() { var audioPlayer = document.createElement('audio'); // create the audio element + var source1 = document.createElement('source'); // creating a source element + var source2 = document.createElement('source'); + audioPlayer.id = 'loudLinksAudioPlayer'; // give the audio element the proper id audioPlayer.preload = true; // audio element preload attribute - var source1 = document.createElement('source'); // creating a source element - var source2 = document.createElement('source'); source1.id = 'mp3Source'; source2.id = 'oggSource'; @@ -42,6 +43,7 @@ var loudlinks = (function(){ var audioSrc = element.getAttribute('data-src'); // getting the sound name from the data-src Attribute var soundMp3Link = mp3Location + audioSrc + '.mp3'; // settnig the mp3 sound in a variable var soundOggLink = oggLocation + audioSrc + '.ogg'; // settnig the ogg sound in a variable + mp3Source.setAttribute('src', soundMp3Link); // putting the mp3 sound link in the src Attribute of oggSource.setAttribute('src', soundOggLink); // putting the mp3 sound link in the src Attribute of @@ -56,6 +58,7 @@ var loudlinks = (function(){ // checking if the data-src Attribute isn't empty checkAttribute = function(element) { var audioSrc = element.getAttribute('data-src'); // getting the sound name from the data-src Attribute + if (audioSrc) { return true; } else { @@ -106,20 +109,25 @@ var loudlinks = (function(){ // Go crazy! Scan all the links and see if they have the 'data-src' Attribute and do the events runTrackers = function() { - for (var i = 0; i < LoudLinksHover.length; i++) { // Hover + var i; + var loudLinksHoverLength = LoudLinksHover.length; + var loudLinksClickLength = LoudLinksClick.length; + + for (i = 0; i < loudLinksHoverLength; i++) { // Hover trackHover(LoudLinksHover[i]); } - for (var i = 0; i < LoudLinksClick.length; i++) { // Click + + for (i = 0; i < loudLinksClickLength; i++) { // Click trackClick(LoudLinksClick[i]); } } // Check if the browser supports audio then get crazy! - if (checkAudioSupport()){ + if (checkAudioSupport()) { console.log('Audio works like a charm 👍'); runTrackers(); } else { console.log('Oh man 😩! \nI\'m sorry but your browsers doesn\'t support awesomeness.') } -})(); +})(document); diff --git a/loudlinks-1.0.min.js b/loudlinks-1.0.min.js index 5708cef..cd3d83b 100644 --- a/loudlinks-1.0.min.js +++ b/loudlinks-1.0.min.js @@ -1 +1 @@ -var loudlinks=function(){var e=(function(){var e=document.createElement("audio");e.id="loudLinksAudioPlayer",e.preload=!0;var o=document.createElement("source"),t=document.createElement("source");o.id="mp3Source",t.id="oggSource",e.appendChild(o),e.appendChild(t),document.body.appendChild(e)}(),document.getElementsByClassName("loud-link-hover")),o=document.getElementsByClassName("loud-link-click"),t=document.getElementById("loudLinksAudioPlayer"),n="sounds/mp3/",r="sounds/ogg/",u=document.getElementById("mp3Source"),c=document.getElementById("oggSource");checkAudioSupport=function(){return!!document.createElement("audio").canPlayType},getAudioSource=function(e){var o=e.getAttribute("data-src"),t=n+o+".mp3",i=r+o+".ogg";u.setAttribute("src",t),c.setAttribute("src",i),c.addEventListener("error",function(){console.log("😶 D'oh! The ♪ ogg file URL is wrong!")},!1),u.addEventListener("error",function(){console.log("😶 D'oh! The ♪ mp3 file URL is wrong!")},!1)},checkAttribute=function(e){var o=e.getAttribute("data-src");return o?!0:!1},playAudio=function(e){checkAttribute(e)&&(getAudioSource(e),t.load(),t.play())},stopAudio=function(){t.pause(),t.currentTime=0},trackHover=function(e){e.addEventListener("mouseover",function(){playAudio(e)},!1),e.addEventListener("mouseout",function(){stopAudio()},!1),e.addEventListener("touchmove",function(){stopAudio()},!1),e.addEventListener("click",function(){stopAudio()},!1),e.addEventListener("dblclick",function(){stopAudio()},!1)},trackClick=function(e){e.addEventListener("click",function(){playAudio(e)},!1)},runTrackers=function(){for(var t=0;te;e++)trackHover(o[e]);for(e=0;r>e;e++)trackClick(t[e])},checkAudioSupport()?(console.log("Audio works like a charm 👍"),runTrackers()):console.log("Oh man 😩! \nI'm sorry but your browsers doesn't support awesomeness.")}(document); \ No newline at end of file From 9cce45d9dc1abcf3c3dff84243fffe426a978a54 Mon Sep 17 00:00:00 2001 From: Shaw Date: Thu, 4 Feb 2016 12:01:14 -0600 Subject: [PATCH 02/11] Grab audioPlayer element on creation No need for grabbing the element by id later, get it from the creation function! --- loudlinks-1.0.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/loudlinks-1.0.js b/loudlinks-1.0.js index faeb6b4..bf7eeb2 100644 --- a/loudlinks-1.0.js +++ b/loudlinks-1.0.js @@ -1,7 +1,7 @@ var loudlinks = (function(document) { // Create audio element and make it awesome - var generateAudioElement = (function() { + var audioPlayer = (function() { var audioPlayer = document.createElement('audio'); // create the audio element var source1 = document.createElement('source'); // creating a source element var source2 = document.createElement('source'); @@ -16,6 +16,8 @@ var loudlinks = (function(document) { audioPlayer.appendChild(source2); document.body.appendChild(audioPlayer); // appending the player to the body + + return audioPlayer; })(); // declaring stuff --------------------------------------------- @@ -26,7 +28,6 @@ var loudlinks = (function(document) { var LoudLinksHover = document.getElementsByClassName('loud-link-hover'); var LoudLinksClick = document.getElementsByClassName('loud-link-click'); - var audioPlayer = document.getElementById('loudLinksAudioPlayer'); var mp3Location = 'sounds/mp3/'; // mp3 sounds location var oggLocation = 'sounds/ogg/'; // ogg sounds location var mp3Source = document.getElementById('mp3Source'); From 2f48f00256cd573cb63b187c29602964f87d43a1 Mon Sep 17 00:00:00 2001 From: Shaw Date: Thu, 4 Feb 2016 12:11:41 -0600 Subject: [PATCH 03/11] Exit if no audio support No need for all the setup if audio is not supported! --- loudlinks-1.0.js | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/loudlinks-1.0.js b/loudlinks-1.0.js index bf7eeb2..67b6286 100644 --- a/loudlinks-1.0.js +++ b/loudlinks-1.0.js @@ -1,5 +1,13 @@ var loudlinks = (function(document) { - + + // Check if the browser supports audio then get crazy, or exit out if no support. + if ( !document.createElement('audio').canPlayType ) { + console.log('Oh man 😩! \nI\'m sorry but your browsers doesn\'t support awesomeness.'); + return; + } else { + console.log('Audio works like a charm 👍'); + } + // Create audio element and make it awesome var audioPlayer = (function() { var audioPlayer = document.createElement('audio'); // create the audio element @@ -34,10 +42,6 @@ var loudlinks = (function(document) { var oggSource = document.getElementById('oggSource'); // ------------------------------------------------------------ - // check if the browser supports Audio ¯\_(ツ)_/¯ - checkAudioSupport = function() { - return !!document.createElement('audio').canPlayType; - } // get the audio source and appending it to