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