-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93cc701
commit 2287a89
Showing
22 changed files
with
1,544 additions
and
1,195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'chrome-extension': patch | ||
--- | ||
|
||
scope init to documentId |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
38 changes: 33 additions & 5 deletions
38
apps/extension/src/content-scripts/injected-connection-port.ts
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,16 +1,44 @@ | ||
import { PraxMessage } from './message-event'; | ||
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client'; | ||
import { PraxConnection } from '../message/prax'; | ||
import { PraxMessage } from './message-event'; | ||
|
||
// this script will init the page session upon instruction from an extension | ||
// worker. init does not arrive in direct response to an emitted message, it is | ||
// independent. | ||
|
||
// handler to listen for init command for this document | ||
const initOnce = ( | ||
req: unknown, | ||
// content script message handlers are activated only by another | ||
// script in the same extension using chrome.tabs.sendMessage | ||
sender: chrome.runtime.MessageSender, | ||
// this handler will only ever send an empty response | ||
emptyResponse: (no?: never) => void, | ||
) => { | ||
if (req !== PraxConnection.Init) { | ||
// boolean return in handlers signals intent to respond | ||
return false; | ||
} | ||
|
||
// this inits the session client that transports messages on the DOM channel through the Chrome runtime | ||
const initOnce = (req: unknown, _sender: chrome.runtime.MessageSender, respond: () => void) => { | ||
if (req !== PraxConnection.Init) return false; | ||
chrome.runtime.onMessage.removeListener(initOnce); | ||
|
||
if (sender.id !== PRAX) { | ||
throw new Error(`Unexpected sender ${sender.id}`); | ||
} | ||
|
||
// create session, post port to window where the injected global can catch it. | ||
const port = CRSessionClient.init(PRAX); | ||
window.postMessage({ [PRAX]: port } satisfies PraxMessage<MessagePort>, '/', [port]); | ||
respond(); | ||
|
||
// handler is done | ||
emptyResponse(); | ||
|
||
// boolean return in handlers signals intent to respond | ||
return true; | ||
}; | ||
|
||
// attach handler | ||
chrome.runtime.onMessage.addListener(initOnce); | ||
|
||
// announce | ||
void chrome.runtime.sendMessage(PraxConnection.Init); |
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
Oops, something went wrong.