aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js/frame-offset-forwarder.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-06-14 14:06:52 -0400
committerGitHub <noreply@github.com>2020-06-14 14:06:52 -0400
commitb612bd8b8dc62a83e6800b052cc5f673a287dbe8 (patch)
treecc9778e15932de9a51ac8ee749b212ce44414f81 /ext/fg/js/frame-offset-forwarder.js
parent8d1a276a83f954d587266cd94ef55063a5828b7e (diff)
PopupProxy refactor (#609)
* Remove setDisabled member; replace with an event * Pass frameOffsetForwarder directly to PopupProxy * Replace .start with .prepare * Make onMessage private * Make message safer and handle unexpected inputs
Diffstat (limited to 'ext/fg/js/frame-offset-forwarder.js')
-rw-r--r--ext/fg/js/frame-offset-forwarder.js30
1 files changed, 19 insertions, 11 deletions
diff --git a/ext/fg/js/frame-offset-forwarder.js b/ext/fg/js/frame-offset-forwarder.js
index 10e3b5be..f692364a 100644
--- a/ext/fg/js/frame-offset-forwarder.js
+++ b/ext/fg/js/frame-offset-forwarder.js
@@ -21,8 +21,7 @@
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) {