wait for getBackgroundPage to have a result

This commit is contained in:
Kyle Spearrin 2018-09-29 22:58:36 -04:00
parent 8425e65544
commit 2cbdb33e0f
2 changed files with 32 additions and 14 deletions

View File

@ -5,10 +5,40 @@ import 'web-animations-js';
// tslint:disable-next-line // tslint:disable-next-line
require('./scss/popup.scss'); require('./scss/popup.scss');
import { BrowserApi } from '../browser/browserApi';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
if (process.env.ENV === 'production') { if (process.env.ENV === 'production') {
enableProdMode(); enableProdMode();
} }
function bootstrapModule() {
platformBrowserDynamic().bootstrapModule(AppModule, { preserveWhitespaces: true }); platformBrowserDynamic().bootstrapModule(AppModule, { preserveWhitespaces: true });
}
// Bug in Edge 18 has null getBackgroundPage() result initially. Can be removed in future.
if (BrowserApi.getBackgroundPage() == null && window.navigator.userAgent.indexOf(' Edge/18.') !== -1) {
const sleep = (time: number) => new Promise((resolve) => window.setTimeout(resolve, time));
const bootstrapForEdge18 = async () => {
let bgAttempts = 1;
while (BrowserApi.getBackgroundPage() == null) {
if (bgAttempts > 30) {
break;
}
// tslint:disable-next-line
console.log('Waiting for background page to not be null. Attempt #' + bgAttempts);
await sleep(200);
bgAttempts++;
}
if (BrowserApi.getBackgroundPage() == null) {
// tslint:disable-next-line
console.log('Reload page.');
window.location.reload();
} else {
bootstrapModule();
}
};
bootstrapForEdge18();
} else {
bootstrapModule();
}

View File

@ -14,19 +14,7 @@ export class LaunchGuardService implements CanActivate {
constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router) { } constructor(private cryptoService: CryptoService, private userService: UserService, private router: Router) { }
async canActivate() { async canActivate() {
const bg = BrowserApi.getBackgroundPage(); if (BrowserApi.getBackgroundPage() == null) {
if (bg == null) {
if (window.navigator.userAgent.indexOf(' Edge/') !== -1) {
// tslint:disable-next-line
console.log('Background page is null.');
// tslint:disable-next-line
console.log(bg);
window.setTimeout(() => {
// tslint:disable-next-line
console.log('Reload page for Edge.');
window.location.reload();
}, 1000);
}
this.router.navigate(['private-mode']); this.router.navigate(['private-mode']);
return false; return false;
} }