This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #2843, lazily load code into the background page
This takes a minimal approach, loading scripts using the script tag on the first button click. Also creates and intercepts the contextMenu action, and any incoming communication Reads onboarding flag and changes icon as necessary
- Loading branch information
Showing
6 changed files
with
112 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
These are all the files/modules for the background worker. This is the higher-privileged WebExtension worker that has no interface. | ||
|
||
- `startBackground.js` handles the browserAction click event and context menu, and loads everything else on the first button of context menu click. | ||
- `analytics.js` handles sending events to the server for use with GA. | ||
- `auth.js` handles the initial authentication call, and handles subsequent requests using that authentication. It also informs `analytics.js` of any A/B tests so those can be submitted. | ||
- `clipboard.js` handles copying to the clipboard. | ||
- `communication.js` handles communication with the content workers. | ||
- `deviceinfo.js` gets information about this device that is used in some logging and analytics. | ||
- `selectorLoader.js` handles loading the selector worker, and contains the list of files that the selector loads. | ||
- `main.js` loads and launches everything. | ||
- `main.js` launches everything | ||
- `takeshot.js` takes the shot the content worker has constructed, sends it to the server, and handles copying and opening the tab. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* globals browser, main, communication */ | ||
/* This file handles: | ||
browser.browserAction.onClicked | ||
browser.contextMenus.onClicked | ||
browser.runtime.onMessage | ||
and loads the rest of the background page in response to those events, forwarding | ||
the events to main.onClicked, main.onClickedContextMenu, or communication.onMessage | ||
*/ | ||
|
||
this.startBackground = (function() { | ||
const backgroundScripts = [ | ||
"log.js", | ||
"makeUuid.js", | ||
"catcher.js", | ||
"background/selectorLoader.js", | ||
"background/communication.js", | ||
"background/auth.js", | ||
"background/senderror.js", | ||
"build/raven.js", | ||
"build/shot.js", | ||
"background/analytics.js", | ||
"background/deviceInfo.js", | ||
"background/takeshot.js", | ||
"background/main.js" | ||
]; | ||
|
||
browser.browserAction.onClicked.addListener((tab) => { | ||
loadIfNecessary().then(() => { | ||
main.onClicked(tab); | ||
}).catch((error) => { | ||
console.error("Error loading Screenshots:", error); | ||
}); | ||
}); | ||
|
||
|
||
browser.contextMenus.create({ | ||
id: "create-screenshot", | ||
title: browser.i18n.getMessage("contextMenuLabel"), | ||
contexts: ["page"], | ||
documentUrlPatterns: ["<all_urls>"] | ||
}); | ||
|
||
browser.contextMenus.onClicked.addListener((info, tab) => { | ||
loadIfNecessary().then(() => { | ||
main.onClickedContextMenu(info, tab); | ||
}).catch((error) => { | ||
console.error("Error loading Screenshots:", error); | ||
}); | ||
}); | ||
|
||
// Note this duplicates functionality in main.js, but we need to change | ||
// the onboarding icon before main.js loads up | ||
browser.storage.local.get(["hasSeenOnboarding"]).then((result) => { | ||
let hasSeenOnboarding = !!result.hasSeenOnboarding; | ||
if (!hasSeenOnboarding) { | ||
let path = "icons/icon-starred-32.svg"; | ||
browser.browserAction.setIcon({path}); | ||
} | ||
}).catch((error) => { | ||
console.error("Error loading Screenshots onboarding flag:", error); | ||
}); | ||
|
||
browser.runtime.onMessage.addListener((req, sender, sendResponse) => { | ||
loadIfNecessary().then(() => { | ||
return communication.onMessage(req, sender, sendResponse); | ||
}).catch((error) => { | ||
console.error("Error loading Screenshots:", error); | ||
}); | ||
return true; | ||
}); | ||
|
||
let loadedPromise; | ||
|
||
function loadIfNecessary() { | ||
if (loadedPromise) { | ||
return loadedPromise; | ||
} | ||
loadedPromise = Promise.resolve(); | ||
backgroundScripts.forEach((script) => { | ||
loadedPromise = loadedPromise.then(() => { | ||
return new Promise((resolve, reject) => { | ||
let tag = document.createElement("script"); | ||
tag.src = browser.extension.getURL(script); | ||
tag.onload = () => { | ||
resolve(); | ||
}; | ||
tag.onerror = (error) => { | ||
let exc = new Error(`Error loading script: ${error.message}`); | ||
exc.scriptName = script; | ||
reject(exc); | ||
}; | ||
document.head.appendChild(tag); | ||
}); | ||
}); | ||
}); | ||
return loadedPromise; | ||
} | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters