snappymail/dev/boot.js

100 lines
2.6 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),
app = eId('app'),
admin = app && '1' == app.dataset.admin,
layout = doc.cookie.match(/(^|;) ?rllayout=([^;]+)/) || '',
2020-09-03 22:34:23 +08:00
showError = msg => {
let div = eId('loading-error');
div.append(msg);
eId('loading').hidden = true;
div.hidden = false;
},
2020-09-03 22:34:23 +08:00
loadScript = src => {
if (!src) {
throw new Error('src should not be empty.');
}
return new Promise((resolve, reject) => {
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);
});
};
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=${doc.location.pathname};samesite=strict;secure";
// doc.cookie = 'smctoken='+smctoken+";path=/;samesite=lax";
doc.cookie = 'smctoken='+smctoken+";path=/;samesite=strict";
2022-11-28 17:39:28 +08:00
} catch (e) {
console.error(e);
}
let RL_APP_DATA = {};
2022-02-28 22:10:04 +08:00
doc.documentElement.classList.toggle('rl-mobile', 'mobile' === layout[2] || (!layout && 1000 > innerWidth));
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 =>
2022-11-12 00:50:18 +08:00
doc.title = (title || '') + (RL_APP_DATA.Title ? (title ? ' - ' : '') + RL_APP_DATA.Title : ''),
2016-05-06 23:14:40 +08:00
2020-09-23 17:20:00 +08:00
initData: appData => {
RL_APP_DATA = appData;
const url = appData.StaticLibsJs,
2022-11-14 16:15:25 +08:00
cb = () => rl.app.bootstart();
loadScript(url)
.then(() => loadScript(url.replace('/libs.', `/${admin?'admin':'app'}.`)))
2023-01-19 23:46:39 +08:00
.then(() => appData.PluginsLink ? loadScript(qUri(appData.PluginsLink)) : Promise.resolve())
.then(() => rl.app
2022-04-16 08:38:57 +08:00
? cb()
: doc.addEventListener('readystatechange', () => 'complete' == doc.readyState && cb())
)
2020-09-23 17:20:00 +08:00
.catch(e => {
showError(e);
throw e;
2021-09-23 21:30:08 +08:00
});
},
setData: appData => {
RL_APP_DATA = appData;
rl.app.refresh();
2022-03-10 16:57:27 +08:00
},
loadScript: loadScript
};
if (!navigator.cookieEnabled) {
eId('loading').hidden = true;
eId('NoCookie').hidden = false;
} else if (![].flat) {
eId('loading').hidden = true;
eId('BadBrowser').hidden = false;
} else {
loadScript(qUri(`${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`))
.catch(e => showError(e));
}
})(document);