-
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
ba19606
commit 7f47e9c
Showing
15 changed files
with
2,115 additions
and
3,043 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
44 changes: 0 additions & 44 deletions
44
apps/extension/src/content-scripts/injected-connection-port.ts
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
apps/extension/src/content-scripts/injected-disconnect-listener.ts
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
apps/extension/src/content-scripts/injected-page-listener.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
PraxMessage, | ||
isPraxConnectMessageEvent, | ||
isPraxDisconnectMessageEvent, | ||
} from './message-event'; | ||
import { PraxConnection } from '../message/prax'; | ||
import { PenumbraRequestFailure } from '@penumbra-zone/client/error'; | ||
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client'; | ||
|
||
const endMessage = { [PRAX]: PraxConnection.End } satisfies PraxMessage<PraxConnection.End>; | ||
|
||
let port: MessagePort | undefined; | ||
|
||
const failureMessage = (failure?: unknown): PraxMessage<PenumbraRequestFailure> => { | ||
if (typeof failure === 'string' && failure in PenumbraRequestFailure) { | ||
return { [PRAX]: failure }; | ||
} else { | ||
console.error('Bad response', failure); | ||
return { [PRAX]: PenumbraRequestFailure.BadResponse }; | ||
} | ||
}; | ||
|
||
window.addEventListener('message', (ev: MessageEvent<unknown>) => { | ||
if (ev.origin === window.origin) { | ||
void (async () => { | ||
// any response to these messages only indicates failure. | ||
let failure: PenumbraRequestFailure | undefined; | ||
|
||
if (isPraxConnectMessageEvent(ev)) { | ||
try { | ||
failure = await chrome.runtime.sendMessage(PraxConnection.Connect); | ||
} catch (e) { | ||
console.error(e); | ||
failure = PenumbraRequestFailure.NotHandled; | ||
} | ||
|
||
if (failure == null) { | ||
port ??= CRSessionClient.init(PRAX); | ||
window.postMessage({ [PRAX]: port } satisfies PraxMessage<MessagePort>, '/', [port]); | ||
} | ||
} else if (isPraxDisconnectMessageEvent(ev)) { | ||
port = undefined; | ||
|
||
try { | ||
failure = await chrome.runtime.sendMessage(PraxConnection.Disconnect); | ||
} catch (e) { | ||
console.error(e); | ||
failure = PenumbraRequestFailure.NotHandled; | ||
} | ||
|
||
if (failure == null) { | ||
window.postMessage(endMessage, '/'); | ||
} | ||
} | ||
|
||
if (failure != null) { | ||
window.postMessage(failureMessage(failure), '/'); | ||
} | ||
})(); | ||
} | ||
}); |
Oops, something went wrong.