Attach source frame info when communicating between frames on the same page
This commit is contained in:
parent
26b78f1225
commit
3e542871c6
@ -15,6 +15,7 @@ export interface EventBusContext {
|
||||
sender?: any,
|
||||
port?: any,
|
||||
frame?: any,
|
||||
sourceFrame?: IframeData
|
||||
forwardTo?: 'all' | 'active' | 'contentScript' | 'server' | 'sameOrigin' | 'popup' | 'all-frames',
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user