Attach source frame info when communicating between frames on the same page

This commit is contained in:
Tamius Han 2023-03-02 01:08:16 +01:00
parent 26b78f1225
commit 3e542871c6
3 changed files with 28 additions and 3 deletions

View File

@ -15,6 +15,7 @@ export interface EventBusContext {
sender?: any,
port?: any,
frame?: any,
sourceFrame?: IframeData
forwardTo?: 'all' | 'active' | 'contentScript' | 'server' | 'sameOrigin' | 'popup' | 'all-frames',
}
}

View File

@ -133,8 +133,26 @@ class CommsClient {
return browser.runtime.sendMessage(null, message, null);
}
processReceivedMessage(message){
this.eventBus.send(message.command, message.config, {origin: CommsOrigin.Server});
processReceivedMessage(receivedMessage){
// when sending between frames, message will be enriched with two new properties
const {_sourceFrame, _sourcePort, ...message} = receivedMessage;
let comms;
if (_sourceFrame || _sourcePort) {
comms = {
port: _sourcePort,
sourceFrame: _sourceFrame
}
}
this.eventBus.send(
message.command,
message.config,
{
comms,
origin: CommsOrigin.Server
}
);
}
}

View File

@ -185,9 +185,15 @@ class CommsServer {
private async sendToOtherFrames(message, context) {
const sender = context.comms.sourceFrame;
const enrichedMessage = {
message,
_sourceFrame: context.comms.sourceFrame,
_sourcePort: context.comms.port
}
for (const frame in this.ports[sender.tabId]) {
if (frame !== sender.frameId) {
this.sendToFrameContentScripts(message, sender.tabId, sender.frameId);
this.sendToFrameContentScripts(enrichedMessage, sender.tabId, sender.frameId);
}
}
}