File size: 8,878 Bytes
f2b0008 | 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | class PairDrop {
constructor() {
this.$headerNotificationBtn = $('notification');
this.$headerEditPairedDevicesBtn = $('edit-paired-devices');
this.$footerPairedDevicesBadge = $$('.discovery-wrapper .badge-room-secret');
this.$headerInstallBtn = $('install');
this.deferredStyles = [
"styles/styles-deferred.css"
];
this.deferredScripts = [
"scripts/browser-tabs-connector.js",
"scripts/util.js",
"scripts/network.js",
"scripts/ui.js",
"scripts/libs/heic2any.min.js",
"scripts/libs/no-sleep.min.js",
"scripts/libs/qr-code.min.js",
"scripts/libs/zip.min.js"
];
this.registerServiceWorker();
Events.on('beforeinstallprompt', e => this.onPwaInstallable(e));
this.persistentStorage = new PersistentStorage();
this.localization = new Localization();
this.themeUI = new ThemeUI();
this.backgroundCanvas = new BackgroundCanvas();
this.headerUI = new HeaderUI();
this.centerUI = new CenterUI();
this.footerUI = new FooterUI();
this.initialize()
.then(_ => {
console.log("Initialization completed.");
});
}
async initialize() {
// Translate page before fading in
await this.localization.setInitialTranslation()
console.log("Initial translation successful.");
// Show "Loading..." until connected to WsServer
await this.footerUI.showLoading();
// Evaluate css shifting UI elements and fade in UI elements
await this.evaluatePermissionsAndRoomSecrets();
await this.headerUI.evaluateOverflowing();
await this.headerUI.fadeIn();
await this.footerUI._evaluateFooterBadges();
await this.footerUI.fadeIn();
await this.centerUI.fadeIn();
await this.backgroundCanvas.fadeIn();
// Load deferred assets
console.log("Load deferred assets...");
await this.loadDeferredAssets();
console.log("Loading of deferred assets completed.");
console.log("Hydrate UI...");
await this.hydrate();
console.log("UI hydrated.");
// Evaluate url params as soon as ws is connected
console.log("Evaluate URL params as soon as websocket connection is established.");
Events.on('ws-connected', _ => this.evaluateUrlParams(), {once: true});
}
registerServiceWorker() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('service-worker.js')
.then(serviceWorker => {
console.log('Service Worker registered');
window.serviceWorker = serviceWorker
});
}
}
onPwaInstallable(e) {
if (!window.matchMedia('(display-mode: standalone)').matches) {
// only display install btn when not installed
this.$headerInstallBtn.removeAttribute('hidden');
this.$headerInstallBtn.addEventListener('click', () => {
this.$headerInstallBtn.setAttribute('hidden', true);
e.prompt();
});
}
return e.preventDefault();
}
async evaluatePermissionsAndRoomSecrets() {
// Check whether notification permissions have already been granted
if ('Notification' in window && Notification.permission !== 'granted') {
this.$headerNotificationBtn.removeAttribute('hidden');
}
let roomSecrets = await PersistentStorage.getAllRoomSecrets();
if (roomSecrets.length > 0) {
this.$headerEditPairedDevicesBtn.removeAttribute('hidden');
this.$footerPairedDevicesBadge.removeAttribute('hidden');
}
}
loadDeferredAssets() {
const stylePromises = this.deferredStyles.map(url => this.loadAndApplyStylesheet(url));
const scriptPromises = this.deferredScripts.map(url => this.loadAndApplyScript(url));
return Promise.all([...stylePromises, ...scriptPromises]);
}
loadStyleSheet(url) {
return new Promise((resolve, reject) => {
let stylesheet = document.createElement('link');
stylesheet.rel = 'preload';
stylesheet.as = 'style';
stylesheet.href = url;
stylesheet.onload = _ => {
stylesheet.onload = null;
stylesheet.rel = 'stylesheet';
resolve();
};
stylesheet.onerror = reject;
document.head.appendChild(stylesheet);
});
}
loadAndApplyStylesheet(url) {
return new Promise( async (resolve) => {
try {
await this.loadStyleSheet(url);
console.log(`Stylesheet loaded successfully: ${url}`);
resolve();
} catch (error) {
console.error('Error loading stylesheet:', error);
}
});
}
loadScript(url) {
return new Promise((resolve, reject) => {
let script = document.createElement("script");
script.src = url;
script.onload = resolve;
script.onerror = reject;
document.body.appendChild(script);
});
}
loadAndApplyScript(url) {
return new Promise( async (resolve) => {
try {
await this.loadScript(url);
console.log(`Script loaded successfully: ${url}`);
resolve();
} catch (error) {
console.error('Error loading script:', error);
}
});
}
async hydrate() {
this.aboutUI = new AboutUI();
this.peersUI = new PeersUI();
this.languageSelectDialog = new LanguageSelectDialog();
this.receiveFileDialog = new ReceiveFileDialog();
this.receiveRequestDialog = new ReceiveRequestDialog();
this.sendTextDialog = new SendTextDialog();
this.receiveTextDialog = new ReceiveTextDialog();
this.pairDeviceDialog = new PairDeviceDialog();
this.clearDevicesDialog = new EditPairedDevicesDialog();
this.publicRoomDialog = new PublicRoomDialog();
this.base64Dialog = new Base64Dialog();
this.shareTextDialog = new ShareTextDialog();
this.toast = new Toast();
this.notifications = new Notifications();
this.networkStatusUI = new NetworkStatusUI();
this.webShareTargetUI = new WebShareTargetUI();
this.webFileHandlersUI = new WebFileHandlersUI();
this.noSleepUI = new NoSleepUI();
this.broadCast = new BrowserTabsConnector();
this.server = new ServerConnection();
this.peers = new PeersManager(this.server);
}
async evaluateUrlParams() {
// get url params
const urlParams = new URLSearchParams(window.location.search);
const hash = window.location.hash.substring(1);
// evaluate url params
if (urlParams.has('pair_key')) {
const pairKey = urlParams.get('pair_key');
this.pairDeviceDialog._pairDeviceJoin(pairKey);
}
else if (urlParams.has('room_id')) {
const roomId = urlParams.get('room_id');
this.publicRoomDialog._joinPublicRoom(roomId);
}
else if (urlParams.has('base64text')) {
const base64Text = urlParams.get('base64text');
await this.base64Dialog.evaluateBase64Text(base64Text, hash);
}
else if (urlParams.has('base64zip')) {
const base64Zip = urlParams.get('base64zip');
await this.base64Dialog.evaluateBase64Zip(base64Zip, hash);
}
else if (urlParams.has("share_target")) {
const shareTargetType = urlParams.get("share_target");
const title = urlParams.get('title') || '';
const text = urlParams.get('text') || '';
const url = urlParams.get('url') || '';
await this.webShareTargetUI.evaluateShareTarget(shareTargetType, title, text, url);
}
else if (urlParams.has("file_handler")) {
await this.webFileHandlersUI.evaluateLaunchQueue();
}
else if (urlParams.has("init")) {
const init = urlParams.get("init");
if (init === "pair") {
this.pairDeviceDialog._pairDeviceInitiate();
}
else if (init === "public_room") {
this.publicRoomDialog._createPublicRoom();
}
}
// remove url params from url
const urlWithoutParams = getUrlWithoutArguments();
window.history.replaceState({}, "Rewrite URL", urlWithoutParams);
console.log("URL params evaluated.");
}
}
const pairDrop = new PairDrop(); |