Repository navigation
Expand file tree
/
Copy pathostore-installer.js
More file actions
158 lines (148 loc) · 6.96 KB
/
Copy pathostore-installer.js
File metadata and controls
158 lines (148 loc) · 6.96 KB
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
(function () {
"use strict";
var UPDATE_URL = "https://Garbagebuild.github.io/JScalated-Store/Apps-directory/OStore/update.webmanifest";
var SCRIPT_BASE = new URL(".", document.currentScript.src).href;
var root = document.getElementById("ostore-installer");
if (root) { root.style.display = "block"; return; }
root = document.createElement("div");
root.id = "ostore-installer";
root.innerHTML = [
'<style>',
'#ostore-installer{position:fixed;z-index:2147483647;inset:0;overflow:auto;background:#07111f;color:#e2e8f0;font:14px Arial,sans-serif}',
'#ostore-installer *{box-sizing:border-box}',
'#ostore-installer .wrap{max-width:580px;margin:auto;padding:15px}',
'#ostore-installer h1{color:#38bdf8;font-size:24px}',
'#ostore-installer p{line-height:1.4}',
'#ostore-installer .panel{background:#0b1728;border:1px solid #20324a;border-radius:11px;padding:13px}',
'#ostore-installer button{width:100%;margin:8px 0;padding:12px;border:0;border-radius:7px;background:#22c55e;color:#020617;font-weight:bold}',
'#ostore-installer button.secondary{background:#334155;color:#fff}',
'#ostore-installer .warning{color:#fca5a5}',
'#ostore-installer pre{min-height:95px;white-space:pre-wrap;word-break:break-word;background:#020617;color:#22c55e;padding:10px;border-radius:7px;font:11px monospace}',
'</style>',
'<div class="wrap"><h1>Install OStore</h1>',
'<div class="panel"><p>JScalated will install OStore through KaiOS AppsManager.</p>',
'<p class="warning">The supplied package tool does not produce a valid cryptographic signature. Installation depends on this device accepting the package.</p>',
'<button id="ostore-retry" type="button">Retry installation</button>',
'<button id="ostore-close" class="secondary" type="button">Close</button></div>',
'<pre id="ostore-log" role="status" aria-live="polite">Starting…</pre></div>'
].join("");
(document.body || document.documentElement).appendChild(root);
var logNode = document.getElementById("ostore-log");
var retry = document.getElementById("ostore-retry");
var busy = false;
var client = null;
function log(message) {
logNode.textContent += "\n" + message;
logNode.scrollTop = logNode.scrollHeight;
}
function callUrlMethod(manager, method, url, rest) {
var address = new URL(String(url)).href;
var args = rest || [];
var requests = {
installPackage: ["InstallPackageRequest", "updateUrl"],
checkForUpdate: ["CheckForUpdateRequest", "updateUrl"],
update: ["UpdateRequest", "manifestUrl"]
};
var request = requests[method];
var codec = request && manager.wrapper_payload_message &&
manager.wrapper_payload_message[request[0]];
function accepts(value) {
if (!codec || typeof codec.encode !== "function") { return false; }
var payload = {};
payload[request[1]] = value;
if (method === "checkForUpdate") { payload.appsOption = args[0]; }
try { codec.encode(payload); return true; }
catch (_) { return false; }
}
function call(value) {
return Promise.resolve().then(function () {
return manager[method].apply(manager, [value].concat(args));
});
}
var first = codec && !accepts(address) && accepts(new URL(address)) ?
new URL(address) : address;
return call(first).catch(function (error) {
var message = String((error && error.message) || error || "");
if (typeof first === "string" &&
/Expected\s+URL\s*,\s*(?:bug\s+|but\s+)?got\s+string/i.test(message)) {
return call(new URL(address));
}
if (typeof first !== "string" &&
/Expected\s+string\s*,\s*(?:bug\s+|but\s+)?got\s+(?:object|URL)/i.test(message)) {
return call(address);
}
throw error;
});
}
function remoteUpdateUrl(app) {
try { return new URL(String((app && (app.updateURL || app.updateUrl)) || "")).href; }
catch (_) { return ""; }
}
function isOStore(app) {
if (!app) { return false; }
var update = remoteUpdateUrl(app);
var manifest = String(app.manifestURL || app.manifestUrl || "");
var origin = String(app.origin || "");
function hasOStoreHost(value) {
try { return new URL(value).hostname === "ostore.localhost"; }
catch (_) { return false; }
}
return update === new URL(UPDATE_URL).href || origin === "ostore" ||
hasOStoreHost(origin) || hasOStoreHost(manifest) || app.name === "OStore";
}
function start() {
if (busy) { return; }
busy = true;
retry.disabled = true;
log("Connecting to AppsManager…");
client.connect().then(function (manager) {
return manager.getAll().then(function (apps) {
var existing = (apps || []).filter(isOStore)[0];
if (existing && remoteUpdateUrl(existing) === new URL(UPDATE_URL).href &&
(existing.manifestURL || existing.manifestUrl)) {
log("OStore is installed from GitHub Pages. Checking for an update…");
return callUrlMethod(manager, "checkForUpdate", UPDATE_URL,
[{ autoInstall: false }]).then(function (available) {
if (!available) {
log("OStore is already installed and up to date at its GitHub update URL.");
return null;
}
log("Updating OStore…");
return callUrlMethod(manager, "update",
existing.manifestURL || existing.manifestUrl);
});
}
if (existing) { log("OStore uses another update URL. Trying the GitHub source; a one-time migration may be required."); }
log("Installing OStore from " + UPDATE_URL);
return callUrlMethod(manager, "installPackage", UPDATE_URL);
});
}).then(function (result) {
if (result) { log("OStore installed or updated. Open it from the app list."); }
}).catch(function (error) {
log("Install failed: " + window.JScalatedAppsManager.errorText(error));
}).then(function () {
busy = false;
retry.disabled = false;
});
}
function loadClient() {
if (window.JScalatedAppsManager) {
client = new window.JScalatedAppsManager(log);
client.onEvent = function (name, event) {
if (name === "APP_DOWNLOAD_FAILED_EVENT") {
log("Download failed: " + window.JScalatedAppsManager.errorText(event));
} else { log(name.replace(/_EVENT$/, "")); }
};
start();
return;
}
var script = document.createElement("script");
script.src = new URL("appsmanager.js", SCRIPT_BASE).href;
script.onload = loadClient;
script.onerror = function () { log("Could not load AppsManager helper script."); };
(document.head || document.documentElement).appendChild(script);
}
retry.onclick = function () { if (client) { start(); } else { loadClient(); } };
document.getElementById("ostore-close").onclick = function () { root.style.display = "none"; };
loadClient();
}());