You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
The idea sparked up as a direct result of issue #7. This issue popped up Firefox support was added through webextension-polyfill. Basically, there's some code that can't be loaded into the window context.
The fix? - let's just not have that code run in the window context then!
Having separate modules for each runtime context not only fixes the issue mentioned above, but it actually has some sick benefits (explained later), first, this is what it would look like:
// any script injected by a content scriptimportBridgefrom'crx-bridge/window';// within content scriptsimportBridgefrom'crx-bridge/content';// within devtoolsimportBridgefrom'crx-bridge/devtools';// and finally, background pagesimportBridgefrom'crx-bridge/background';
Now, on to the benefits:
Lightweight - the window context doesn't care about the webextension API's, and the background doesn't care about postMessage. While there's no concept of "loading time" in an extension, parsing time reduction is still welcome.
Strict, more precise "localized" typings - because each module has its own designated scope, instead of a general crx-bridge scope, types can be contained as well. For example, setNamespace inside any other context besides the window script is a noop, and allowWindowMessaging is only applicable in a content script.
More readable - The moment you see import Bridge from 'crx-bridge/background', you know that this file is meant to run in the background page. With just import Bridge from 'crx-bridge', not so much.
Strict errors during development - The moment import Bridge from 'crx-bridge/window' statement runs within a content script, an error is thrown to let you know that you're mixing up things. This strict behavior keeps the previous bullet point about readability reinforced at all times.
The idea sparked up as a direct result of issue #7. This issue popped up Firefox support was added through
webextension-polyfill
. Basically, there's some code that can't be loaded into thewindow
context.The fix? - let's just not have that code run in the
window
context then!Having separate modules for each runtime context not only fixes the issue mentioned above, but it actually has some sick benefits (explained later), first, this is what it would look like:
Now, on to the benefits:
window
context doesn't care about the webextension API's, and thebackground
doesn't care aboutpostMessage
. While there's no concept of "loading time" in an extension, parsing time reduction is still welcome.crx-bridge
scope, types can be contained as well. For example,setNamespace
inside any other context besides the window script is a noop, andallowWindowMessaging
is only applicable in a content script.import Bridge from 'crx-bridge/background'
, you know that this file is meant to run in the background page. With justimport Bridge from 'crx-bridge'
, not so much.import Bridge from 'crx-bridge/window'
statement runs within a content script, an error is thrown to let you know that you're mixing up things. This strict behavior keeps the previous bullet point about readability reinforced at all times.cc @smeijer @antfu @pakholeung37 @marik-d
The text was updated successfully, but these errors were encountered: