playlist.js

  1(function(){
  2    var hasFrame = window.parent!=window,
  3        scripts = document.getElementsByTagName('script'),
  4        current = scripts[scripts.length-1],
  5        config = current.getAttribute('data-config'),
  6        head = document.getElementsByTagName("head")[0],
  7        dest = location.href.replace(/scmplayer\=true/g, 'scmplayer=false'),
  8        destHost = dest.substr(0,dest.indexOf('/',10)),
  9        scm = current.getAttribute('src').replace(/script\.js.*/g,'scm.html?16102012')+'#'+dest,
 10        scmHost = scm.substr(0,scm.indexOf('/',10)),
 11        isOutside = !hasFrame || location.href.indexOf("scmplayer=true")>0,
 12        postMessage = function(msg){
 13            return window.top.document.getElementById('scmframe')
 14                .contentWindow.postMessage(msg,scmHost);
 15        },
 16        postFactory = function(obj,keys){
 17            var keys = keys.split(','),
 18                post = function(key){
 19                    return function(arg){
 20                        var argStr = '';
 21                        if(typeof(arg)!='undefined')
 22                            argStr = (key.match(/(play|queue)/) ? 'new Song(':'(') +
 23                                JSON.stringify(arg)+')';
 24                        postMessage('SCM.'+key+'('+argStr+')');
 25                    }
 26                };
 27            for(var i=0;i<keys.length;i++){
 28                var key = keys[i];
 29                obj[key] = post(key);
 30            }
 31        },
 32        postConfig = function(config){
 33            if(!isOutside)
 34                postMessage('SCM.config('+config+')');
 35        },
 36
 37        addEvent = function(elm, evType, fn) {
 38            if(elm.addEventListener) 
 39                elm.addEventListener(evType, fn);
 40            else if (elm.attachEvent) 
 41                elm.attachEvent('on' + evType, fn);
 42            else
 43                elm['on' + evType] = fn;
 44        },
 45        isIE = (function(){
 46            var undef,v = 3,div = document.createElement('div'),
 47                all = div.getElementsByTagName('i');
 48            while (
 49                div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
 50                all[0] );
 51            return v > 4 ? v : undef;
 52        })(),
 53        isMobile = navigator.userAgent.match(/iPad|iPhone|Android|Blackberry/i),
 54
 55        init = function(){
 56            if(!document.body){ 
 57                setTimeout(init,10); 
 58                return;
 59            }
 60            if(isOutside) outside(); else inside();
 61        },
 62
 63        outside = function(){
 64            var css = 'html,body{overflow:hidden;} body{margin:0;padding:0;border:0;} img,a,embed,object,div,address,table,iframe,p,span,form,header,section,footer{ display:none;border:0;margin:0;padding:0; } #scmframe{display:block; background-color:transparent; position:fixed; top:0px; left:0px; width:100%; height:100%; z-index:1667;} ';
 65            var style = document.createElement('style');
 66            style.type = 'text/css';
 67            style.id = 'scmcss';
 68
 69            if(style.styleSheet) style.styleSheet.cssText = css;
 70            else style.appendChild(document.createTextNode(css));
 71
 72            head.appendChild(style);
 73            /*
 74      while(head.firstChild.id!="scmcss")
 75        head.removeChild(head.firstChild);
 76        */
 77
 78            var scmframe = document.createElement('iframe');
 79            scmframe.frameBorder = 0;
 80            scmframe.id = "scmframe";
 81            scmframe.allowTransparency = true;
 82            scmframe.src = scm;
 83
 84            document.body.insertBefore(scmframe,document.body.firstChild);
 85
 86            addEvent(window,'load',function() {
 87                setTimeout(function(){
 88                    while(document.body.firstChild!=scmframe)
 89                        document.body.removeChild(document.body.firstChild);
 90                    while(document.body.lastChild!=scmframe)
 91                        document.body.removeChild(document.body.lastChild);
 92                },0);
 93            });
 94
 95            //fix frame height in IE
 96            addEvent(window,'resize',function(){
 97                scmframe.style.height = (function(){
 98                    if( typeof( window.innerHeight ) == 'number' )
 99                        return window.innerHeight; 
100                    else if( document.documentElement && document.documentElement.clientHeight ) 
101                        return document.documentElement.clientHeight; 
102                    else if( document.body && document.body.clientHeight ) 
103                        return document.body.clientHeight; 
104                })();
105            });
106            //pushState and hash change detection
107            var getPath = function(){
108                return location.href.replace(/#.*/,'');
109            },
110                path = getPath(),
111                hash = location.hash;
112            setInterval(function(){
113                if(getPath()!=path){
114                    path = getPath();
115                    window.scminside.location.replace(path);
116                }
117                if(location.hash != hash){
118                    hash = location.hash;
119                    window.scminside.location.hash = hash;
120                }
121            },100);
122        },
123        inside = function(){
124            //change title
125            window.top.document.title = document.title;
126            //fix links
127            var filter = function(host){
128                host = host.replace(/blogspot.[a-z.]*/i,'blogspot.com');
129                host = host.replace(/^(http(s)?:\/\/)?(www.)?/i,'');
130                return host;
131            };
132            addEvent(document.body,'click',function(e){
133                var tar = e.target;
134                while(!tar.tagName.match(/^(a|area)$/i) && tar!=document.body) 
135                    tar = tar.parentNode;
136                if(tar.tagName.match(/^(a|area)$/i) && 
137                    !tar.href.match(/.(jpg|png)$/i) && //ignore picture link
138                    !tar.href.match(/^javascript:/) //ignore javascript link
139                ){ 
140                    if(tar.href.indexOf('#')==0){
141                        //hash
142                        if(tar.href != "#"){
143                            window.top.scminside = window;
144                            window.top.location.hash = location.hash;
145                            e.preventDefault();
146                        }
147                    }else if(tar.title.match(/^(SCM:|\[SCM\])/i)){
148                        //SCM Play link
149                        var title = tar.title.replace(/^(SCM:|\[SCM\])( )?/i,'');
150                        var url = tar.href;
151                        SCM.play({title:title,url:url});
152                        e.preventDefault();
153                    }else if(tar.href.match(/\.css$/)){
154                        //auto add skin
155                        window.open('http://playlist.me/w/#skin='+tar.href,'_blank');
156                        window.focus();
157                        e.preventDefault();
158                    }else if(filter(tar.href).indexOf(filter(location.host))==-1 ){
159                        if(tar.href.match(/^http(s)?/)){
160                            //external links
161                            window.open(tar.href,'_blank');
162                            window.focus();
163                            e.preventDefault();
164                        }
165                    }else if(history.pushState){
166                        //internal link & has pushState
167                        //change address bar href
168                        var url = filter(tar.href).replace(filter(destHost),'');
169                        window.top.scminside = window;
170                        window.top.history.pushState(null,null,url);
171                        e.preventDefault();
172                    }
173                }
174            });
175
176            addEvent(window,'load',function() {
177            });
178
179        };
180
181    //SCM interface
182    var SCM = {};
183
184    postFactory(SCM,
185        'queue,play,remove,pause,next,previous,volume,skin,placement,'+
186        'loadPlaylist,repeatMode,isShuffle,showPlaylist,'+
187        'togglePlaylist,toggleShuffle,changeRepeatMode');
188
189    if(window.SCM && window.SCMMusicPlayer) return;
190
191    if(!isMobile) init();
192
193    //send config
194    if(config) postConfig(config);
195    SCM.init = postConfig;
196
197    window.SCMMusicPlayer = window.SCMMusicPlayer || SCM;
198    window.SCM = window.SCM || SCM;
199})();