forked from gaodeng/fuzhuo.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
241 lines (223 loc) · 7.99 KB
/
application.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* zfu github.com/fuzhuo
* https://fuzhuo.me
*/
//The global player for lava and netease music
var player = new Player();
player.addEventListener('stateDidChange', function(listener, extraInfo) {
if (listener.state == 'end' || listener.state == 'paused') {
console.log("Enable auto sleep");
JSB.enableAutoSleep();
} else {
console.log("disable auto sleep");
JSB.disableAutoSleep();
}
},{});
var baseURL;
var currentVersion;
App.onLaunch = function(options) {
var l = options.location;
baseURL = l.substr(0,l.lastIndexOf('/')+1);
appStart(baseURL);
}
function appStart(baseURL) {
console.log("Base URL is " + baseURL);
const scripts = [
"home",
"youku4tvos/search",
"youku4tvos/history",
"youku4tvos/mainmenu",
"youku4tvos/series",
"youku4tvos/player",
"TumblrBrowser/post.xml",
"TumblrBrowser/tumblr.xml",
"lava/home.xml",
"lava/channel.xml",
"netease_music_tvos/main_page.xml",
"netease_music_tvos/music_list.xml",
"ximalaya/ximalaya.xml",
"ximalaya/category.xml",
"ximalaya/category_albums.xml",
"ximalaya/subject.xml",
"ximalaya/speciallist.xml",
"ximalaya/ranklist.xml",
"ximalaya/authors.xml",
"ximalaya/album.xml",
"tv/tv.xml",
"pptv/pptvSearch.xml",
"pptv/pptvHome.xml",
"ero/home.xml"
].map(
moduleName => `${baseURL}${moduleName}.js`
);
getHTTP('version.json', function(data){
let j = JSON.parse(data);
currentVersion = j['version'];
console.log("current version: " + currentVersion);
});
const loadingDocument = createLoadingDocument("LazyCat加载中..");
navigationDocument.pushDocument(loadingDocument);
for (let a of scripts) {
console.log("scripts[]:"+a);
}
evaluateScripts(scripts, function(scriptsAreLoaded) {
if (scriptsAreLoaded) {
console.log("scripts are loaded");
showHomePage();
} else {
const alertDocument = createEvalErrorAlertDocument();
navigationDocument.replaceDocument(alertDocument, loadingDocument);
throw new EvalError("TVML application.js unable to evaluate scripts");
}
});
}
App.onWillEnterForeground = function() {
console.log("app will enter foreground");
getHTTP('version.json', function(data){
const j = JSON.parse(data);
const version = j['version'];
if (version != currentVersion) {
const msg = j['message'];
console.log("find new version " + version + " current version: " + currentVersion);
console.log("What's new: " + msg);
const docText = `<?xml version="1.0" encoding="UTF-8" ?>
<document>
<alertTemplate>
<title>有可用更新</title>
<description>从${currentVersion}更新至${version},更新日志:${msg}</description>
<button onselect="updateVersion()">
<text>点击更新并重载</text>
</button>
</alertTemplate>
</document>`;
var doc = (new DOMParser).parseFromString(docText, "application/xml");
navigationDocument.pushDocument(doc);
} else {
console.log("version on server is: " + version);
}
});
}
function updateVersion() {
console.log("update version");
navigationDocument.clear();
appStart(`${baseURL}`);
}
function getHTTP(url_, callback) {
var url = url_;
if (url.match(/^http/) == null) {
url = `${baseURL}${url_}`;
}
console.log("getHTTP:"+url);
var templateXHR = new XMLHttpRequest();
templateXHR.responseType = "document";
templateXHR.timeout = 10000;
templateXHR.addEventListener("load", function() {
callback(templateXHR.responseText);
}, false);
templateXHR.addEventListener("timeout", function() {
const alertDocument = createAlertDocument("请求超时", `<![CDATA[未能成功拉取:${url}]]>`);
navigationDocument.pushDocument(alertDocument);
}, false);
templateXHR.addEventListener("error", function(e) {
const alertDocument = createAlertDocument("请求错误", `<![CDATA[未能成功拉取:${url}]]>`);
navigationDocument.pushDocument(alertDocument);
}, false);
templateXHR.open("GET", url, true);
templateXHR.send();
}
function postHTTP(url, postData, callback) {
var templateXHR = new XMLHttpRequest();
console.log("getHTTP:"+url);
templateXHR.responseType = "document";
templateXHR.timeout = 10000;
templateXHR.addEventListener("load", function() {
callback(templateXHR.responseText);
}, false);
templateXHR.addEventListener("timeout", function() {
const alertDocument = createAlertDocument("请求超时", `<![CDATA[未能成功拉取:${url}]]>`);
navigationDocument.pushDocument(alertDocument);
}, false);
templateXHR.addEventListener("error", function(e) {
const alertDocument = createAlertDocument("请求错误", `<![CDATA[未能成功拉取:${url}]]>`);
navigationDocument.pushDocument(alertDocument);
}, false);
templateXHR.open("POST", url, true);
templateXHR.send(postData);
}
/**
* Convenience function to create a TVML loading document with a specified title.
*/
function createLoadingDocument(title) {
// If no title has been specified, fall back to "Loading...".
title = title || "LazyCat加载中...";
const template = `<?xml version="1.0" encoding="UTF-8" ?>
<document>
<loadingTemplate>
<activityIndicator>
<title>${title}</title>
</activityIndicator>
</loadingTemplate>
</document>
`;
return new DOMParser().parseFromString(template, "application/xml");
}
/**
* Convenience function to create a TVML alert document with a title and description.
*/
function createAlertDocument(title, description) {
const template = `<?xml version="1.0" encoding="UTF-8" ?>
<document>
<alertTemplate>
<title>${title}</title>
<description>${description}</description>
</alertTemplate>
</document>
`;
return new DOMParser().parseFromString(template, "application/xml");
}
/**
* Convenience function to create a TVML alert document with a title and description.
*/
function createDescriptiveAlertDocument(title, description) {
const template = `<?xml version="1.0" encoding="UTF-8" ?>
<document>
<descriptiveAlertTemplate>
<title>${title}</title>
<description>${description}</description>
</descriptiveAlertTemplate>
</document>
`;
return new DOMParser().parseFromString(template, "application/xml");
}
/**
* Convenience function to create a TVML alert for failed evaluateScripts.
*/
function createEvalErrorAlertDocument() {
const title = "Evaluate Scripts Error";
const description = [
"There was an error attempting to evaluate the external JavaScript files.",
"Please check your network connection and try again later."
].join("\n\n");
return createAlertDocument(title, description);
}
/**
* Convenience function to create a TVML alert for a failed XMLHttpRequest.
*/
function createLoadErrorAlertDocument(url, xhr) {
const title = (xhr.status) ? `Fetch Error ${xhr.status}` : "Fetch Error";
const description = `Could not load document:\n${url}\n(${xhr.statusText})`;
return createAlertDocument(title, description);
}
function padLeft(nr, n, str){
return Array(n-String(nr).length+1).join(str||'0')+nr;
}
function time2str(time) {
var a = parseInt(time);
var h = parseInt(a/60/60);
a-=h*60*60;
var m = parseInt(a/60);
a-=m*60;
var s = a;
if (h>0) return ''+padLeft(h,2)+":"+padLeft(m,2)+":"+padLeft(s,2);
else return ''+padLeft(m,2)+":"+padLeft(s,2);
}