Index: ext/content.js |
=================================================================== |
--- a/ext/content.js |
+++ b/ext/content.js |
@@ -24,62 +24,68 @@ |
let backgroundFrame = document.createElement("iframe"); |
backgroundFrame.setAttribute("src", |
"background.html" + window.location.search); |
backgroundFrame.style.display = "none"; |
window.addEventListener("DOMContentLoaded", () => |
{ |
document.body.appendChild(backgroundFrame); |
- }, false); |
Manish Jethani
2017/10/11 15:42:41
Not related but might as well.
|
+ }); |
let messageQueue = []; |
let maxMessageId = -1; |
let loadHandler = (event) => |
{ |
if (event.data.type == "backgroundPageLoaded") |
{ |
let queue = messageQueue; |
messageQueue = null; |
if (queue) |
{ |
for (let message of queue) |
backgroundFrame.contentWindow.postMessage(message, "*"); |
} |
- window.removeEventListener("message", loadHandler, false); |
+ window.removeEventListener("message", loadHandler); |
} |
}; |
- window.addEventListener("message", loadHandler, false); |
+ window.addEventListener("message", loadHandler); |
ext.backgroundPage = { |
_sendRawMessage(message) |
{ |
if (messageQueue) |
messageQueue.push(message); |
else |
backgroundFrame.contentWindow.postMessage(message, "*"); |
- }, |
- sendMessage(message, responseCallback) |
- { |
- let messageId = ++maxMessageId; |
+ } |
+ }; |
+ |
+ /* Polyfills */ |
Manish Jethani
2017/10/11 15:42:41
This will move to polyfill.js as part of the other
|
- this._sendRawMessage({ |
- type: "message", |
- messageId, |
- payload: message |
- }); |
+ if (!("runtime" in chrome)) |
Thomas Greiner
2017/10/11 15:58:16
Suggestion: `chrome` is defined in `ext/common.js`
Manish Jethani
2017/10/11 16:11:20
Is that really necessary? The chrome object is use
|
+ chrome.runtime = {}; |
+ |
+ chrome.runtime.sendMessage = (message, responseCallback) => |
+ { |
+ let messageId = ++maxMessageId; |
- if (responseCallback) |
+ ext.backgroundPage._sendRawMessage({ |
+ type: "message", |
+ messageId, |
+ payload: message |
+ }); |
+ |
+ if (responseCallback) |
+ { |
+ let callbackWrapper = event => |
Manish Jethani
2017/10/11 15:42:41
Just modernized the code a bit, using arrow functi
|
{ |
- let callbackWrapper = function(event) |
+ if (event.data.type == "response" && event.data.messageId == messageId) |
{ |
- if (event.data.type == "response" && |
- event.data.messageId == messageId) |
- { |
- window.removeEventListener("message", callbackWrapper, false); |
- responseCallback(event.data.payload); |
- } |
- }; |
- window.addEventListener("message", callbackWrapper, false); |
- } |
+ window.removeEventListener("message", callbackWrapper); |
+ responseCallback(event.data.payload); |
+ } |
+ }; |
+ |
+ window.addEventListener("message", callbackWrapper); |
} |
}; |
}()); |