2015-06-23 17:41:25 +02:00
|
|
|
/*
|
2016-01-07 05:17:56 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-02-24 12:45:28 +01:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2019-05-23 06:57:39 +02:00
|
|
|
Copyright 2018, 2019 New Vector Ltd
|
2019-06-26 22:08:04 +02:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2020-01-21 03:52:11 +01:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2015-06-23 17:41:25 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-12-09 23:57:46 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
|
|
// These are things that can run before the skin loads - be careful not to reference the react-sdk though.
|
|
|
|
import { parseQsFromFragment } from "./url_utils";
|
|
|
|
import './modernizr';
|
|
|
|
|
2017-01-18 17:06:02 +01:00
|
|
|
// Require common CSS here; this will make webpack process it into bundle.css.
|
|
|
|
// Our own CSS (which is themed) is imported via separate webpack entry points
|
|
|
|
// in webpack.config.js
|
2018-04-25 18:33:18 +02:00
|
|
|
require('gfm.css/gfm.css');
|
2020-12-07 22:46:56 +01:00
|
|
|
require('katex/dist/katex.css');
|
2015-12-01 19:05:43 +01:00
|
|
|
|
2021-08-01 16:28:38 +02:00
|
|
|
/**
|
2021-08-18 18:08:20 +02:00
|
|
|
* This require is necessary only for purposes of CSS hot-reload, as otherwise
|
2021-08-18 18:09:08 +02:00
|
|
|
* webpack has some incredible problems figuring out which CSS files should be
|
2021-08-18 18:08:20 +02:00
|
|
|
* hot-reloaded, even with proper hints for the loader.
|
2021-08-01 16:28:38 +02:00
|
|
|
*
|
|
|
|
* On production build it's going to be an empty module, so don't worry about that.
|
|
|
|
*/
|
|
|
|
require('./devcss');
|
2021-09-02 20:30:12 +02:00
|
|
|
require('./localstorage-fix');
|
2021-10-15 16:56:22 +02:00
|
|
|
|
2020-04-08 21:12:05 +02:00
|
|
|
async function settled(...promises: Array<Promise<any>>) {
|
|
|
|
for (const prom of promises) {
|
|
|
|
try {
|
|
|
|
await prom;
|
|
|
|
} catch (e) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error(e);
|
2020-04-08 21:12:05 +02:00
|
|
|
}
|
2020-04-04 18:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 01:55:36 +02:00
|
|
|
function checkBrowserFeatures() {
|
|
|
|
if (!window.Modernizr) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error("Cannot check features - Modernizr global is missing.");
|
2020-04-05 01:55:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:36:49 +01:00
|
|
|
// Custom checks atop Modernizr because it doesn't have ES2018/ES2019 checks
|
|
|
|
// in it for some features we depend on.
|
|
|
|
// Modernizr requires rules to be lowercase with no punctuation.
|
|
|
|
// ES2018: http://262.ecma-international.org/9.0/#sec-promise.prototype.finally
|
2020-04-05 01:55:36 +02:00
|
|
|
window.Modernizr.addTest("promiseprototypefinally", () =>
|
2021-02-08 17:36:49 +01:00
|
|
|
typeof window.Promise?.prototype?.finally === "function");
|
2021-05-25 17:12:54 +02:00
|
|
|
// ES2020: http://262.ecma-international.org/#sec-promise.allsettled
|
|
|
|
window.Modernizr.addTest("promiseallsettled", () =>
|
|
|
|
typeof window.Promise?.allSettled === "function");
|
2021-02-08 17:36:49 +01:00
|
|
|
// ES2018: https://262.ecma-international.org/9.0/#sec-get-regexp.prototype.dotAll
|
|
|
|
window.Modernizr.addTest("regexpdotall", () => (
|
|
|
|
window.RegExp?.prototype &&
|
|
|
|
!!Object.getOwnPropertyDescriptor(window.RegExp.prototype, "dotAll")?.get
|
|
|
|
));
|
|
|
|
// ES2019: http://262.ecma-international.org/10.0/#sec-object.fromentries
|
2020-04-05 01:55:36 +02:00
|
|
|
window.Modernizr.addTest("objectfromentries", () =>
|
2021-02-08 17:36:49 +01:00
|
|
|
typeof window.Object?.fromEntries === "function");
|
2020-04-05 01:55:36 +02:00
|
|
|
|
|
|
|
const featureList = Object.keys(window.Modernizr);
|
|
|
|
|
|
|
|
let featureComplete = true;
|
|
|
|
for (let i = 0; i < featureList.length; i++) {
|
|
|
|
if (window.Modernizr[featureList[i]] === undefined) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error(
|
2020-04-05 01:55:36 +02:00
|
|
|
"Looked for feature '%s' but Modernizr has no results for this. " +
|
|
|
|
"Has it been configured correctly?", featureList[i],
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (window.Modernizr[featureList[i]] === false) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error("Browser missing feature: '%s'", featureList[i]);
|
2020-04-05 01:55:36 +02:00
|
|
|
// toggle flag rather than return early so we log all missing features rather than just the first.
|
|
|
|
featureComplete = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return featureComplete;
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:50:06 +02:00
|
|
|
const supportedBrowser = checkBrowserFeatures();
|
2020-04-08 20:21:39 +02:00
|
|
|
|
2020-04-04 18:21:59 +02:00
|
|
|
// React depends on Map & Set which we check for using modernizr's es6collections
|
|
|
|
// if modernizr fails we may not have a functional react to show the error message.
|
|
|
|
// try in react but fallback to an `alert`
|
2020-04-08 17:24:40 +02:00
|
|
|
// We start loading stuff but don't block on it until as late as possible to allow
|
|
|
|
// the browser to use as much parallelism as it can.
|
2020-09-30 13:10:35 +02:00
|
|
|
// Load parallelism is based on research in https://github.com/vector-im/element-web/issues/12253
|
2020-04-04 18:21:59 +02:00
|
|
|
async function start() {
|
|
|
|
// load init.ts async so that its code is not executed immediately and we can catch any exceptions
|
2020-04-08 17:17:46 +02:00
|
|
|
const {
|
|
|
|
rageshakePromise,
|
2021-03-16 21:21:59 +01:00
|
|
|
setupLogStorage,
|
2020-04-08 17:17:46 +02:00
|
|
|
preparePlatform,
|
|
|
|
loadOlm,
|
|
|
|
loadConfig,
|
|
|
|
loadSkin,
|
|
|
|
loadLanguage,
|
|
|
|
loadTheme,
|
|
|
|
loadApp,
|
2020-04-08 20:47:52 +02:00
|
|
|
showError,
|
2020-04-08 22:41:22 +02:00
|
|
|
showIncompatibleBrowser,
|
2020-04-08 20:47:52 +02:00
|
|
|
_t,
|
2020-04-08 17:17:46 +02:00
|
|
|
} = await import(
|
2020-04-04 18:21:59 +02:00
|
|
|
/* webpackChunkName: "init" */
|
|
|
|
/* webpackPreload: true */
|
|
|
|
"./init");
|
|
|
|
|
2020-04-08 20:35:16 +02:00
|
|
|
try {
|
2020-04-14 15:44:12 +02:00
|
|
|
// give rageshake a chance to load/fail, we don't actually assert rageshake loads, we allow it to fail if no IDB
|
|
|
|
await settled(rageshakePromise);
|
2020-04-08 20:35:16 +02:00
|
|
|
|
|
|
|
const fragparts = parseQsFromFragment(window.location);
|
|
|
|
|
|
|
|
// don't try to redirect to the native apps if we're
|
|
|
|
// verifying a 3pid (but after we've loaded the config)
|
|
|
|
// or if the user is following a deep link
|
2020-09-30 13:10:35 +02:00
|
|
|
// (https://github.com/vector-im/element-web/issues/7378)
|
2020-04-08 20:35:16 +02:00
|
|
|
const preventRedirect = fragparts.params.client_secret || fragparts.location.length > 0;
|
|
|
|
|
|
|
|
if (!preventRedirect) {
|
|
|
|
const isIos = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
|
|
|
|
const isAndroid = /Android/.test(navigator.userAgent);
|
|
|
|
if (isIos || isAndroid) {
|
2020-07-13 18:32:17 +02:00
|
|
|
if (document.cookie.indexOf("element_mobile_redirect_to_guide=false") === -1) {
|
2020-04-08 20:35:16 +02:00
|
|
|
window.location.href = "mobile_guide/";
|
|
|
|
return;
|
|
|
|
}
|
2020-04-05 01:55:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:35:16 +02:00
|
|
|
const loadOlmPromise = loadOlm();
|
|
|
|
// set the platform for react sdk
|
|
|
|
preparePlatform();
|
|
|
|
// load config requires the platform to be ready
|
|
|
|
const loadConfigPromise = loadConfig();
|
2020-04-08 21:03:45 +02:00
|
|
|
await settled(loadConfigPromise); // wait for it to settle
|
|
|
|
// keep initialising so that we can show any possible error with as many features (theme, i18n) as possible
|
|
|
|
|
2021-03-16 21:21:59 +01:00
|
|
|
// now that the config is ready, try to persist logs
|
|
|
|
const persistLogsPromise = setupLogStorage();
|
|
|
|
|
2020-04-08 21:03:45 +02:00
|
|
|
// Load language after loading config.json so that settingsDefaults.language can be applied
|
|
|
|
const loadLanguagePromise = loadLanguage();
|
|
|
|
// as quickly as we possibly can, set a default theme...
|
|
|
|
const loadThemePromise = loadTheme();
|
|
|
|
const loadSkinPromise = loadSkin();
|
|
|
|
|
|
|
|
// await things settling so that any errors we have to render have features like i18n running
|
2020-04-08 21:12:05 +02:00
|
|
|
await settled(loadSkinPromise, loadThemePromise, loadLanguagePromise);
|
2020-04-08 21:03:45 +02:00
|
|
|
|
2020-04-23 16:50:06 +02:00
|
|
|
let acceptBrowser = supportedBrowser;
|
|
|
|
if (!acceptBrowser && window.localStorage) {
|
|
|
|
acceptBrowser = Boolean(window.localStorage.getItem("mx_accepts_unsupported_browser"));
|
|
|
|
}
|
|
|
|
|
2020-04-08 21:03:45 +02:00
|
|
|
// ##########################
|
|
|
|
// error handling begins here
|
|
|
|
// ##########################
|
|
|
|
if (!acceptBrowser) {
|
2021-01-27 15:35:02 +01:00
|
|
|
await new Promise<void>(resolve => {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error("Browser is missing required features.");
|
2020-04-08 22:41:22 +02:00
|
|
|
// take to a different landing page to AWOOOOOGA at the user
|
|
|
|
showIncompatibleBrowser(() => {
|
|
|
|
if (window.localStorage) {
|
|
|
|
window.localStorage.setItem('mx_accepts_unsupported_browser', String(true));
|
|
|
|
}
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("User accepts the compatibility risks.");
|
2020-04-08 22:41:22 +02:00
|
|
|
resolve();
|
|
|
|
});
|
2020-04-08 21:03:45 +02:00
|
|
|
});
|
|
|
|
}
|
2020-04-08 20:35:16 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// await config here
|
|
|
|
await loadConfigPromise;
|
2020-04-08 20:47:52 +02:00
|
|
|
} catch (error) {
|
|
|
|
// Now that we've loaded the theme (CSS), display the config syntax error if needed.
|
|
|
|
if (error.err && error.err instanceof SyntaxError) {
|
2020-07-13 15:12:44 +02:00
|
|
|
// This uses the default brand since the app config is unavailable.
|
|
|
|
return showError(_t("Your Element is misconfigured"), [
|
2020-07-21 12:30:28 +02:00
|
|
|
_t("Your Element configuration contains invalid JSON. " +
|
|
|
|
"Please correct the problem and reload the page."),
|
|
|
|
_t(
|
|
|
|
"The message from the parser is: %(message)s",
|
|
|
|
{ message: error.err.message || _t("Invalid JSON") },
|
|
|
|
),
|
2020-04-08 20:47:52 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
return showError(_t("Unable to load config file: please refresh the page to try again."));
|
2020-04-08 20:35:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 21:03:45 +02:00
|
|
|
// ##################################
|
|
|
|
// app load critical path starts here
|
2020-04-08 22:41:22 +02:00
|
|
|
// assert things started successfully
|
2020-04-08 21:03:45 +02:00
|
|
|
// ##################################
|
2020-04-08 20:35:16 +02:00
|
|
|
await loadOlmPromise;
|
2020-04-08 21:12:05 +02:00
|
|
|
await loadSkinPromise;
|
2020-04-08 20:35:16 +02:00
|
|
|
await loadThemePromise;
|
|
|
|
await loadLanguagePromise;
|
2020-04-08 17:12:36 +02:00
|
|
|
|
2021-03-16 21:21:59 +01:00
|
|
|
// We don't care if the log persistence made it through successfully, but we do want to
|
|
|
|
// make sure it had a chance to load before we move on. It's prepared much higher up in
|
|
|
|
// the process, making this the first time we check that it did something.
|
|
|
|
await settled(persistLogsPromise);
|
|
|
|
|
2020-04-08 20:47:52 +02:00
|
|
|
// Finally, load the app. All of the other react-sdk imports are in this file which causes the skinner to
|
|
|
|
// run on the components.
|
2020-04-08 22:41:22 +02:00
|
|
|
await loadApp(fragparts.params);
|
2020-04-08 20:47:52 +02:00
|
|
|
} catch (err) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error(err);
|
2020-04-09 11:32:04 +02:00
|
|
|
// Like the compatibility page, AWOOOOOGA at the user
|
2020-07-13 15:12:44 +02:00
|
|
|
// This uses the default brand since the app config is unavailable.
|
|
|
|
await showError(_t("Your Element is misconfigured"), [
|
2020-04-09 11:32:04 +02:00
|
|
|
err.translatedMessage || _t("Unexpected error preparing the app. See console for details."),
|
|
|
|
]);
|
2020-04-08 20:47:52 +02:00
|
|
|
}
|
2020-04-04 18:21:59 +02:00
|
|
|
}
|
2020-04-23 14:37:25 +02:00
|
|
|
|
2020-04-08 20:35:16 +02:00
|
|
|
start().catch(err => {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error(err);
|
2020-04-23 14:37:25 +02:00
|
|
|
// show the static error in an iframe to not lose any context / console data
|
|
|
|
// with some basic styling to make the iframe full page
|
|
|
|
delete document.body.style.height;
|
|
|
|
const iframe = document.createElement("iframe");
|
2020-07-21 12:30:28 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2020-04-28 01:42:36 +02:00
|
|
|
// @ts-ignore - typescript seems to only like the IE syntax for iframe sandboxing
|
|
|
|
iframe["sandbox"] = "";
|
2020-04-23 16:55:13 +02:00
|
|
|
iframe.src = supportedBrowser ? "static/unable-to-load.html" : "static/incompatible-browser.html";
|
2020-04-23 14:37:25 +02:00
|
|
|
iframe.style.width = "100%";
|
|
|
|
iframe.style.height = "100%";
|
|
|
|
iframe.style.position = "absolute";
|
|
|
|
iframe.style.top = "0";
|
|
|
|
iframe.style.left = "0";
|
|
|
|
iframe.style.right = "0";
|
|
|
|
iframe.style.bottom = "0";
|
|
|
|
iframe.style.border = "0";
|
|
|
|
document.getElementById("matrixchat").appendChild(iframe);
|
2020-04-08 20:35:16 +02:00
|
|
|
});
|