summaryrefslogtreecommitdiff
path: root/ext/fg/js/frame-offset-forwarder.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2020-06-27 19:04:19 -0700
committerAlex Yatskov <alex@foosoft.net>2020-06-27 19:04:19 -0700
commit88af95d20bfdbeb59d44bf0f0d46e772a329f839 (patch)
treed1dfa7268f274fed32061221c0f030e3647f9ae2 /ext/fg/js/frame-offset-forwarder.js
parent19197a9a5d6a1f54a179d894577dfac513b97401 (diff)
parent0a6c08d0f53090a4ad48663bc5846ddae5723d52 (diff)
Merge branch 'master' into testing
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});
}
}