-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
440 lines (397 loc) · 13 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
var DB = [];
var DB_result = [];
var baseUrls = {};
var PER_PAGE = 30;
var isInitial = true;
var previousSearch = '';
var plistServerUrl = ''; // will append ?d=<data>
NodeList.prototype.forEach = Array.prototype.forEach; // fix for < iOS 9.3
/*
* Init
*/
function setMessage(msg) {
document.getElementById('content').innerHTML = msg;
}
function loadFile(url, onErrFn, fn) {
try {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'text';
xhr.onload = function (e) { fn(e.target.response); };
xhr.onerror = function (e) { onErrFn('Server or network error.'); };
xhr.send();
} catch (error) {
onErrFn(error);
}
}
function loadDB() {
var config = null;
try {
config = loadConfig(true);
} catch (error) {
alert(error);
}
setMessage('Loading base-urls ...');
loadFile('data/urls.json', setMessage, function (data) {
baseUrls = JSON.parse(data);
setMessage('Loading database ...');
loadFile('data/ipa.json', setMessage, function (data) {
DB = JSON.parse(data);
setMessage('ready. Links in database: ' + DB.length);
if (config && (config.page > 0 || config.search || config.bundleid)) {
searchIPA(true);
}
});
});
}
function loadConfig(chkServer) {
if (!location.hash) {
return; // keep default values
}
const params = location.hash.substring(1).split('&');
const data = {};
params.forEach(function (param) {
const pair = param.split('=', 2);
data[pair[0]] = decodeURIComponent(pair[1]);
});
document.querySelectorAll('input,select').forEach(function (input) {
if (input.type === 'checkbox') {
input.checked = data[input.id] || null;
} else {
input.value = data[input.id] || '';
}
});
if (chkServer && data['plistServer']) {
setPlistGen();
}
return data;
}
function saveConfig() {
const data = [];
document.querySelectorAll('input,select').forEach(function (e) {
const value = e.type === 'checkbox' ? e.checked : e.value;
if (value) {
data.push(e.id + '=' + encodeURIComponent(value));
}
});
const prev = location.hash;
location.hash = '#' + data.join('&');
return prev !== location.hash;
}
/*
* Search
*/
function applySearch() {
const term = document.getElementById('search').value.toLowerCase();
const bundle = document.getElementById('bundleid').value.trim().toLowerCase();
const unique = document.getElementById('unique').checked;
const minos = document.getElementById('minos').value;
const maxos = document.getElementById('maxos').value;
const platform = document.getElementById('device').value;
const minid = document.getElementById('minid').value;
const minV = minos ? strToVersion(minos) : 0;
const maxV = maxos ? strToVersion(maxos) : 9999999;
const device = platform ? 1 << platform : 255; // all flags
const minPK = minid ? parseInt(minid) : 0;
// [7, 2,20200,"180","com.headcasegames.180","1.0",1,"180.ipa", 189930],
// [pk, platform, minOS, title, bundleId, version, baseUrl, pathName, size]
DB_result = [];
isInitial = false;
const uniqueBundleIds = {};
DB.forEach(function (ipa, i) {
if (ipa[2] < minV || ipa[2] > maxV || !(ipa[1] & device) || ipa[0] < minPK) {
return;
}
if (bundle && ipa[4].toLowerCase().indexOf(bundle) === -1) {
return;
}
if (!term
|| ipa[3].toLowerCase().indexOf(term) > -1
|| ipa[4].toLowerCase().indexOf(term) > -1
|| ipa[7].toLowerCase().indexOf(term) > -1
) {
if (unique) {
const bId = ipa[4];
if (uniqueBundleIds[bId]) {
return;
}
uniqueBundleIds[bId] = true;
}
DB_result.push(i);
}
});
delete uniqueBundleIds; // free up memory
}
function restoreSearch() {
location.hash = previousSearch;
const conf = loadConfig(false);
previousSearch = '';
if (conf.random) {
randomIPA(conf.random);
} else {
searchIPA(true);
}
}
function searchBundle(idx, additional) {
previousSearch = location.hash + (additional || '');
document.getElementById('bundleid').value = DB[idx][4];
document.getElementById('search').value = '';
document.getElementById('page').value = null;
document.getElementById('unique').checked = false;
searchIPA();
}
function searchIPA(restorePage) {
var page = 0;
if (restorePage) {
page = document.getElementById('page').value;
} else {
document.getElementById('page').value = null;
}
applySearch();
printIPA((page || 0) * PER_PAGE);
saveConfig();
}
/*
* Random IPA
*/
function urlsToImgs(redirectUrl, list) {
const template = getTemplate('.screenshot');
var rv = '<div class="carousel">';
for (var i = 0; i < list.length; i++) {
rv += renderTemplate(template, { $REF: list[i], $URL: redirectUrl + list[i] });
}
return rv + '</div>';
}
function randomIPA(specificId) {
document.getElementById('search').value = '';
document.getElementById('bundleid').value = '';
if (saveConfig() || isInitial || specificId) {
applySearch();
}
var idx = specificId;
if (!specificId) {
if (DB_result.length > 0) {
idx = DB_result[Math.floor(Math.random() * DB_result.length)];
} else {
idx = Math.floor(Math.random() * DB.length);
}
}
const entry = entryToDict(DB[idx]);
const output = document.getElementById('content');
output.innerHTML = '<h3>Random:</h3>' + entriesToStr('.full', [idx]);
output.lastElementChild.className += ' single';
output.innerHTML += renderTemplate(getTemplate('.randomAction'), { $IDX: idx });
if (!plistServerUrl) {
output.innerHTML += getTemplate('.no-itunes');
return;
}
// Append iTunes info to result
const redirectUrl = plistServerUrl + '?r='
const iTunesUrl = 'https://itunes.apple.com/lookup?bundleId=' + entry.bundleId;
loadFile(redirectUrl + iTunesUrl, console.error, function (data) {
const obj = JSON.parse(data);
if (!obj || obj.resultCount < 1) {
output.innerHTML += '<p class="no-itunes">No iTunes results.</p>';
return;
}
const info = obj.results[0];
const imgs1 = info.screenshotUrls;
const imgs2 = info.ipadScreenshotUrls;
const device = document.getElementById('device').value || 255;
var imgStr = '';
if (imgs1 && imgs1.length > 0 && device & 1) {
imgStr += '<p>iPhone Screenshots:</p>' + urlsToImgs(redirectUrl, imgs1);
}
if (imgs2 && imgs2.length > 0 && device & 2) {
imgStr += '<p>iPad Screenshots:</p>' + urlsToImgs(redirectUrl, imgs2);
}
output.innerHTML += renderTemplate(getTemplate('.itunes'), {
$VERSION: info.version,
$PRICE: info.formattedPrice,
$RATING: info.averageUserRating.toFixed(1),
$ADVISORY: info.contentAdvisoryRating,
$DATE: info.currentVersionReleaseDate,
$GENRES: (info.genres || []).join(', '),
$URL: info.trackViewUrl,
$IMG: imgStr,
$DESCRIPTION: info.description,
});
});
}
/*
* Output
*/
function platformToStr(num) {
if (!num) { return '?'; }
return [
num & (1 << 1) ? 'iPhone' : null,
num & (1 << 2) ? 'iPad' : null,
num & (1 << 3) ? 'TV' : null,
num & (1 << 4) ? 'Watch' : null,
].filter(Boolean).join(', ');
}
function versionToStr(num) {
if (!num) { return '?'; }
const major = Math.floor(num / 10000);
const minor = Math.floor(num / 100) % 100;
const patch = num % 100;
return major + '.' + minor + (patch ? '.' + patch : '');
}
function strToVersion(versionStr) {
const x = ((versionStr || '0') + '.0.0.0').split('.');
return parseInt(x[0]) * 10000 + parseInt(x[1]) * 100 + parseInt(x[2]);
}
function humanSize(size) {
var sizeIndex = 0;
while (size > 1024) {
size /= 1024;
sizeIndex += 1;
}
return size.toFixed(1) + ['kB', 'MB', 'GB'][sizeIndex];
}
function getTemplate(name) {
return document.getElementById('templates').querySelector(name).outerHTML;
}
function renderTemplate(template, values) {
return template.replace(/\$[A-Z]+/g, function (x) { return values[x]; });
}
function validUrl(url) {
return encodeURI(url).replace('#', '%23').replace('?', '%3F');
}
function entryToDict(entry) {
const pk = entry[0];
return {
pk: pk,
platform: entry[1],
minOS: entry[2],
title: entry[3],
bundleId: entry[4],
version: entry[5],
baseUrl: entry[6],
pathName: entry[7],
size: entry[8],
ipa_url: baseUrls[entry[6]] + '/' + entry[7],
img_url: 'data/' + Math.floor(pk / 1000) + '/' + pk + '.jpg',
}
}
function entriesToStr(templateType, data) {
const template = getTemplate(templateType);
var rv = '';
for (var i = 0; i < data.length; i++) {
const entry = entryToDict(DB[data[i]]);
rv += renderTemplate(template, {
$IDX: data[i],
$IMG: entry.img_url,
$TITLE: (entry.title || '?').replace('<', '<'),
$VERSION: entry.version,
$BUNDLEID: entry.bundleId,
$MINOS: versionToStr(entry.minOS),
$PLATFORM: platformToStr(entry.platform),
$SIZE: humanSize(entry.size),
$URLNAME: entry.pathName.split('/').slice(-1), // decodeURI
$URL: validUrl(entry.ipa_url),
});
}
return rv;
}
function printIPA(offset) {
if (!offset) { offset = 0; }
const total = DB_result.length;
var content = '<h3>Results: ' + total;
if (previousSearch) {
content += ' -- Go to: <a onclick="restoreSearch()">previous search</a>';
}
content += '</h3>';
const page = Math.floor(offset / PER_PAGE);
const pages = Math.ceil(total / PER_PAGE);
if (pages > 1) {
content += paginationShort(page, pages);
}
const templateType = document.getElementById('unique').checked ? '.short' : '.entry';
content += entriesToStr(templateType, DB_result.slice(offset, offset + PER_PAGE));
if (pages > 1) {
content += paginationShort(page, pages);
content += paginationFull(page, pages);
}
document.getElementById('content').innerHTML = content;
window.scrollTo(0, 0);
}
/*
* Pagination
*/
function p(page) {
printIPA(page * PER_PAGE);
document.getElementById('page').value = page || null;
saveConfig();
}
function paginationShort(page, pages) {
return '<div class="shortpage">'
+ '<button onclick="p(' + (page - 1) + ')" ' + (page == 0 ? 'disabled' : '') + '>Prev</button>'
+ '<span>' + (page + 1) + ' / ' + pages + '</span>'
+ '<button onclick="p(' + (page + 1) + ')" ' + (page + 1 == pages ? 'disabled' : '') + '>Next</button>'
+ '</div>';
}
function paginationFull(page, pages) {
var rv = '<div id="pagination">Pages:';
for (var i = 0; i < pages; i++) {
if (i === page) {
rv += '\n<b>' + (i + 1) + '</b>';
} else {
rv += '\n<a onclick="p(' + i + ')">' + (i + 1) + '</a>';
}
}
return rv + '</div>';
}
/*
* Install on iDevice
*/
function setPlistGen() {
const testURL = document.getElementById('plistServer').value;
const scheme = testURL.slice(0, 7);
if (scheme != 'http://' && scheme != 'https:/') {
alert('URL must start with http:// or https://.');
return;
}
loadFile(testURL + '?d=' + btoa('{"u":"1"}'), alert, function (data) {
if (data.trim().slice(0, 6) != '<?xml ') {
alert('Server did not respond with a Plist file.');
return;
}
plistServerUrl = testURL;
document.getElementById('overlay').hidden = true;
saveConfig();
});
}
function urlWithSlash(url) {
return url.toString().slice(-1) === '/' ? url : (url + '/');
}
function utoa(data) {
return btoa(unescape(encodeURIComponent(data)));
}
function installIPA(idx) {
if (!plistServerUrl) {
document.getElementById('overlay').hidden = false;
return;
}
const thisServerUrl = location.href.replace(location.hash, '');
const entry = entryToDict(DB[idx]);
const json = JSON.stringify({
u: validUrl(entry.ipa_url),
n: entry.title,
b: entry.bundleId,
v: entry.version.split(' ')[0],
i: urlWithSlash(thisServerUrl) + entry.img_url,
}, null, 0)
var b64 = '';
try {
b64 = btoa(json);
} catch (error) {
b64 = utoa(json);
}
while (b64.slice(-1) === '=') {
b64 = b64.slice(0, -1);
}
// window.open(plistServerUrl + '?d=' + b64);
const plistUrl = plistServerUrl + '%3Fd%3D' + b64; // url encoded "?d="
window.open('itms-services://?action=download-manifest&url=' + plistUrl);
}