Skip to content

Commit

Permalink
Skip stream initialization during extension startup (#11)
Browse files Browse the repository at this point in the history
The extension will load this page during startup so that the service
worker can start up and cache all page contents, ensuring that the
page doesn't crash when the expected `href` and `hostname` are missing
from the URL hash.
  • Loading branch information
Gudahtt authored May 13, 2022
1 parent 112f0e7 commit 076c779
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ function createRandomId(): number {
return idCounter;
}

window.document.addEventListener('DOMContentLoaded', start);
/**
* Check whether this page is being loaded during the extension startup, in an
* attempt to ensure the service worker is installed.
*
* @returns Whether this appears to be an extension startup page load.
*/
function isExtensionStartup() {
const { hash } = window.location;
return hash === '#extensionStartup';
}

window.addEventListener('load', async () => {
if ('serviceWorker' in navigator) {
try {
Expand All @@ -35,8 +45,15 @@ window.addEventListener('load', async () => {
}
});

// Skip stream initialization on extension startup, when this page is loaded
// in a hidden iframe. No need to setup streams to handle user interaction in
// that case.
if (!isExtensionStartup()) {
window.document.addEventListener('DOMContentLoaded', start);
}

/**
* Initialize the phishing warning page.
* Initialize the phishing warning page streams.
*/
function start() {
const metamaskStream = new WindowPostMessageStream({
Expand Down

0 comments on commit 076c779

Please sign in to comment.