Commit 75cec1a1 authored by Thomas Greiner's avatar Thomas Greiner

Noissue - Fixed: Messaging port mock events sent to all listeners with same name

parent eced060d
......@@ -51,7 +51,7 @@
{
parent.postMessage({
type: "port",
name: this._name,
id: this._id,
payload: msg
}, "*");
}
......@@ -64,7 +64,7 @@
if (event.data.type != "connect")
return;
listener(new ext._Port(event.data.name));
listener(new ext._Port(event.data.id, event.data.name));
});
}
window.browser.runtime.onConnect = {addListener: onConnect};
......
......@@ -77,8 +77,9 @@
class Port
{
constructor(name)
constructor(id, name)
{
this._id = id;
this._name = name;
}
......@@ -96,13 +97,13 @@
get onMessage()
{
const name = this._name;
const id = this._id;
return {
addListener(listener)
{
window.addEventListener("message", (event) =>
{
if (event.data.type != "port" || event.data.name != name)
if (event.data.type != "port" || event.data.id != id)
return;
listener(event.data.payload);
......
......@@ -96,11 +96,15 @@
});
};
// We initialize the ID using a random value to avoid
// potential conflicts with other IDs
let portId = Math.random();
function postMessage(msg)
{
ext.backgroundPage._sendRawMessage({
type: "port",
name: this._name,
id: this._id,
payload: msg
});
}
......@@ -108,8 +112,10 @@
function connect({name})
{
ext.backgroundPage._sendRawMessage({type: "connect", name});
return new ext._Port(name);
const id = ++portId;
const port = new ext._Port(id, name);
ext.backgroundPage._sendRawMessage({type: "connect", id, name});
return port;
}
browser.runtime.connect = connect;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment