aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js/frame-offset-forwarder.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg/js/frame-offset-forwarder.js')
-rw-r--r--ext/fg/js/frame-offset-forwarder.js34
1 files changed, 21 insertions, 13 deletions
diff --git a/ext/fg/js/frame-offset-forwarder.js b/ext/fg/js/frame-offset-forwarder.js
index 9b68d34e..f692364a 100644
--- a/ext/fg/js/frame-offset-forwarder.js
+++ b/ext/fg/js/frame-offset-forwarder.js
@@ -16,13 +16,12 @@
*/
/* global
- * apiBroadcastTab
+ * api
*/
class FrameOffsetForwarder {
constructor() {
- this._started = false;
-
+ this._isPrepared = false;
this._cacheMaxSize = 1000;
this._frameCache = new Set();
this._unreachableContentWindowCache = new Set();
@@ -38,10 +37,10 @@ class FrameOffsetForwarder {
]);
}
- start() {
- if (this._started) { return; }
- window.addEventListener('message', this.onMessage.bind(this), false);
- this._started = true;
+ prepare() {
+ if (this._isPrepared) { return; }
+ window.addEventListener('message', this._onMessage.bind(this), false);
+ this._isPrepared = true;
}
async getOffset() {
@@ -69,11 +68,20 @@ class FrameOffsetForwarder {
return offset;
}
- onMessage(e) {
- const {action, params} = e.data;
- const handler = this._windowMessageHandlers.get(action);
- if (typeof handler !== 'function') { return; }
- handler(params, e);
+ // Private
+
+ _onMessage(event) {
+ const data = event.data;
+ if (data === null || typeof data !== 'object') { return; }
+
+ try {
+ const {action, params} = event.data;
+ const handler = this._windowMessageHandlers.get(action);
+ if (typeof handler !== 'function') { return; }
+ handler(params, event);
+ } catch (e) {
+ // NOP
+ }
}
_onGetFrameOffset(offset, uniqueId, e) {
@@ -161,6 +169,6 @@ class FrameOffsetForwarder {
}
_forwardFrameOffsetOrigin(offset, uniqueId) {
- apiBroadcastTab('frameOffset', {offset, uniqueId});
+ api.broadcastTab('frameOffset', {offset, uniqueId});
}
}