diff --git a/README.md b/README.md index 18a87a8..46a2098 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ jquery.soundcloud ================= -jQuery plugin to inject the SoundCloud player +jQuery plugin to inject the SoundCloud player. + +Check `example.html` for more information. diff --git a/example.html b/example.html index 3334698..ec3e4ab 100644 --- a/example.html +++ b/example.html @@ -12,6 +12,21 @@ -
+

jQuery SoundCloud Plugin

+ +

Maximum Config

+
+ +

Minimal Config

+
diff --git a/jquery.soundcloud.js b/jquery.soundcloud.js index fc49328..9c084d8 100644 --- a/jquery.soundcloud.js +++ b/jquery.soundcloud.js @@ -4,55 +4,63 @@ { // Default settings var defaults = { - client_id: false, - url: false, - callback: false, - maxwidth: '100%', - maxheight: false, - color: false, auto_play: false, + //callback: false, + client_id: false, + color: false, + iframe: true, + maxheight: false, + maxwidth: false, show_comments: true, - iframe: false + url: false }; // Overrides the defaults with passed in options - options = $.extend(defaults, options); + options = $.extend({}, defaults, options); + var elements = this; var protocol = document.location.protocol == 'https:' ? 'https://' : 'http://'; if (typeof SC === 'undefined') { - // Adds the SoundCloud SDK - var script = document.createElement('script'); - script.src = protocol + 'connect.soundcloud.com/sdk.js'; - $('body').append(script); + $.getScript( + protocol + 'connect.soundcloud.com/sdk.js', + function() + { + elements.each(function(index, element) + { + try + { + // Overrides the options with the data attributes + parameters = $.extend({}, options, $(this).data()); + + // Checks that we have a client ID + if (!parameters.client_id) + { + throw 'Missing client ID.'; + } + + SC.initialize({ client_id: parameters.client_id }); + + // Request the embed HTML + SC.oEmbed( + parameters.url, + parameters, + function(oEmbed) + { + $(element).replaceWith(oEmbed.html.replace("https://", protocol)); + } + ); + } + catch (error) + { + console.log('[jQuery.SoundCloud] [error] ' + error); + } + }); + } + ); } - return this.each(function() - { - try - { - // Overrides the options with the data attributes - options = $.extend(options, $(this).data()); - - // Checks that we have a client ID - if (!options.client_id) - { - throw 'Missing client ID.'; - } - - // SC.initialize({ client_id: options.client_id }); - // var track_url = protocol + options.url; - // SC.oEmbed(track_url, { auto_play: true, maxheight: 166 }, function(oEmbed) - // { - // //'$(".soundcloud").eq(' + index + ').replaceWith(oEmbed.html.replace("https:", "http:"));', - // console.log(oEmbed); - // }); - } - catch (error) - { - console.log('[jQuery.SoundCloud] [error] ' + error); - } - }); + return this; } })(jQuery);