2022-03-11 19:58:12 +08:00
|
|
|
(doc => {
|
2020-08-10 16:40:25 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
2022-11-07 06:10:34 +08:00
|
|
|
showError = msg => {
|
|
|
|
let div = eId('loading-error');
|
2023-01-30 16:10:49 +08:00
|
|
|
div.append(msg);
|
2023-02-23 00:43:54 +08:00
|
|
|
toggle(div);
|
2022-11-07 06:10:34 +08:00
|
|
|
},
|
|
|
|
|
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');
|
2021-04-29 04:59:42 +08:00
|
|
|
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');
|
2020-08-10 16:40:25 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:58:12 +08:00
|
|
|
let RL_APP_DATA = {};
|
2021-02-16 21:40:11 +08:00
|
|
|
|
2022-03-11 19:58:12 +08:00
|
|
|
window.rl = {
|
2021-02-17 03:12:23 +08:00
|
|
|
adminArea: () => admin,
|
2022-04-25 15:57:32 +08:00
|
|
|
|
2020-09-04 18:05:17 +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 =>
|
2023-02-21 17:22:59 +08:00
|
|
|
doc.title = (title || '') + (RL_APP_DATA.title ? (title ? ' - ' : '') + RL_APP_DATA.title : ''),
|
2016-05-06 23:14:40 +08:00
|
|
|
|
2021-11-01 18:24:11 +08:00
|
|
|
setData: appData => {
|
|
|
|
RL_APP_DATA = appData;
|
|
|
|
rl.app.refresh();
|
2022-03-10 16:57:27 +08:00
|
|
|
},
|
|
|
|
|
2023-02-21 16:04:47 +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 => {
|
2023-02-23 20:54:32 +08:00
|
|
|
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
|
|
|
|
}
|
2023-02-21 16:04:47 +08:00
|
|
|
}
|
2023-02-23 20:54:32 +08:00
|
|
|
*/
|
|
|
|
return response.json();
|
2023-02-21 16:04:47 +08:00
|
|
|
}
|
2023-02-23 20:54:32 +08:00
|
|
|
return Promise.reject('Network response error: ' + response.status);
|
2023-02-21 16:04:47 +08:00
|
|
|
});
|
|
|
|
}
|
2020-08-12 06:25:36 +08:00
|
|
|
};
|
|
|
|
|
2023-01-30 16:10:49 +08:00
|
|
|
if (!navigator.cookieEnabled) {
|
2023-02-23 00:43:54 +08:00
|
|
|
toggle(eId('NoCookie'));
|
2023-01-30 16:10:49 +08:00
|
|
|
} else if (![].flat) {
|
2023-02-23 00:43:54 +08:00
|
|
|
toggle(eId('BadBrowser'));
|
2023-01-30 16:10:49 +08:00
|
|
|
} else {
|
2023-02-21 16:04:47 +08:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
})
|
2022-11-07 06:10:34 +08:00
|
|
|
.catch(e => showError(e));
|
2023-01-30 16:10:49 +08:00
|
|
|
}
|
2020-08-10 16:40:25 +08:00
|
|
|
|
2022-03-11 19:58:12 +08:00
|
|
|
})(document);
|