snappymail/dev/boot.js

160 lines
4 KiB
JavaScript
Raw Normal View History

(doc => {
const
2023-01-19 23:46:39 +08:00
qUri = path => doc.location.pathname.replace(/\/+$/,'') + '/?/' + path,
2021-07-26 21:06:28 +08:00
eId = id => doc.getElementById('rl-'+id),
2023-02-23 00:43:54 +08:00
admin = '1' == eId('app')?.dataset?.admin,
2020-09-03 22:34:23 +08:00
2023-02-23 00:43:54 +08:00
toggle = div => {
eId('loading').hidden = true;
div.hidden = false;
},
showError = msg => {
let div = eId('loading-error');
div.append(msg);
2023-02-23 00:43:54 +08:00
toggle(div);
},
2023-02-23 00:43:54 +08:00
loadScript = src => src ? new Promise((resolve, reject) => {
2020-09-03 22:34:23 +08:00
const script = doc.createElement('script');
script.onload = () => resolve();
2022-11-12 00:50:18 +08:00
script.onerror = () => reject('Failed loading ' + src);
2020-09-03 22:34:23 +08:00
script.src = src;
2022-10-02 21:19:23 +08:00
// script.async = true;
2020-09-03 22:34:23 +08:00
doc.head.append(script);
2023-02-23 00:43:54 +08:00
}) : Promise.reject('src is empty');
2022-11-28 17:39:28 +08:00
try {
2022-12-27 17:24:16 +08:00
let smctoken = doc.cookie.match(/(^|;) ?smctoken=([^;]+)/);
smctoken = smctoken ? smctoken[2] : localStorage.getItem('smctoken');
2022-11-28 17:39:28 +08:00
if (!smctoken) {
let data = new Uint8Array(16);
crypto.getRandomValues(data);
2022-12-28 03:57:11 +08:00
smctoken = encodeURIComponent(btoa(String.fromCharCode(...data)));
2022-11-28 17:39:28 +08:00
}
2022-12-27 17:24:16 +08:00
localStorage.setItem('smctoken', smctoken);
2023-02-02 19:55:06 +08:00
doc.cookie = 'smctoken='+smctoken+";path=/;samesite=strict";
2022-11-28 17:39:28 +08:00
} catch (e) {
console.error(e);
}
let RL_APP_DATA = {};
window.rl = {
adminArea: () => admin,
2022-04-25 15:57:32 +08:00
settings: {
2021-09-23 21:30:08 +08:00
get: name => RL_APP_DATA[name],
2020-09-23 17:20:00 +08:00
set: (name, value) => RL_APP_DATA[name] = value,
2022-04-25 15:57:32 +08:00
app: name => RL_APP_DATA.System[name]
2020-09-23 17:20:00 +08:00
},
2022-04-25 15:57:32 +08:00
2022-11-14 16:15:25 +08:00
setTitle: title =>
doc.title = (title || '') + (RL_APP_DATA.title ? (title ? ' - ' : '') + RL_APP_DATA.title : ''),
2016-05-06 23:14:40 +08:00
setData: appData => {
RL_APP_DATA = appData;
rl.app.refresh();
2022-03-10 16:57:27 +08:00
},
loadScript: loadScript,
fetch: (resource, init, postData) => {
init = Object.assign({
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
referrerPolicy: 'no-referrer',
credentials: 'same-origin',
headers: {}
}, init);
let asJSON = 1,
XToken = RL_APP_DATA.System?.token,
object = {};
if (postData) {
init.method = 'POST';
if (postData instanceof FormData) {
postData.forEach((value, key) => {
if (value instanceof File) {
asJSON = 0;
} else if (!Reflect.has(object, key)) {
object[key] = value;
} else {
Array.isArray(object[key]) || (object[key] = [object[key]]);
object[key].push(value);
}
});
if (asJSON) {
postData = object;
// postData.XToken = XToken;
} else {
XToken && postData.set('XToken', XToken);
}
}
if (asJSON) {
init.headers['Content-Type'] = 'application/json';
postData = JSON.stringify(postData);
}
init.body = postData;
}
XToken && (init.headers['X-SM-Token'] = XToken);
// init.headers = new Headers(init.headers);
return fetch(resource, init);
},
fetchJSON: (resource, init, postData) => {
init = Object.assign({ headers: {} }, init);
init.headers.Accept = 'application/json';
return rl.fetch(resource, init, postData).then(response => {
if (response.ok) {
/* TODO: use this for non-developers?
response.clone()
let data = response.text();
try {
return JSON.parse(data);
} catch (e) {
console.error(e);
// console.log(data);
return Promise.reject(Notifications.JsonParse);
return {
Result: false,
ErrorCode: 952, // Notifications.JsonParse
ErrorMessage: e.message,
ErrorMessageAdditional: data
}
}
*/
return response.json();
}
return Promise.reject('Network response error: ' + response.status);
});
}
};
if (!navigator.cookieEnabled) {
2023-02-23 00:43:54 +08:00
toggle(eId('NoCookie'));
} else if (![].flat) {
2023-02-23 00:43:54 +08:00
toggle(eId('BadBrowser'));
} else {
rl.fetchJSON(qUri(`${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`))
2023-02-22 03:05:02 +08:00
.then(appData => {
RL_APP_DATA = appData;
const url = appData.StaticLibsJs,
cb = () => rl.app.bootstart();
loadScript(url)
.then(() => loadScript(url.replace('/libs.', `/${admin?'admin':'app'}.`)))
.then(() => appData.PluginsLink ? loadScript(qUri(appData.PluginsLink)) : Promise.resolve())
.then(() => rl.app
? cb()
: doc.addEventListener('readystatechange', () => 'complete' == doc.readyState && cb())
)
.catch(e => {
showError(e);
throw e;
});
})
.catch(e => showError(e));
}
})(document);