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.js51
1 files changed, 27 insertions, 24 deletions
diff --git a/ext/fg/js/frame-offset-forwarder.js b/ext/fg/js/frame-offset-forwarder.js
index f692364a..94775105 100644
--- a/ext/fg/js/frame-offset-forwarder.js
+++ b/ext/fg/js/frame-offset-forwarder.js
@@ -20,20 +20,14 @@
*/
class FrameOffsetForwarder {
- constructor() {
+ constructor(frameId) {
+ this._frameId = frameId;
this._isPrepared = false;
this._cacheMaxSize = 1000;
this._frameCache = new Set();
this._unreachableContentWindowCache = new Set();
-
- this._forwardFrameOffset = (
- window !== window.parent ?
- this._forwardFrameOffsetParent.bind(this) :
- this._forwardFrameOffsetOrigin.bind(this)
- );
-
this._windowMessageHandlers = new Map([
- ['getFrameOffset', ({offset, uniqueId}, e) => this._onGetFrameOffset(offset, uniqueId, e)]
+ ['getFrameOffset', this._onMessageGetFrameOffset.bind(this)]
]);
}
@@ -44,6 +38,10 @@ class FrameOffsetForwarder {
}
async getOffset() {
+ if (window === window.parent) {
+ return [0, 0];
+ }
+
const uniqueId = yomichan.generateId(16);
const frameOffsetPromise = yomichan.getTemporaryListenerResult(
@@ -56,13 +54,7 @@ class FrameOffsetForwarder {
5000
);
- window.parent.postMessage({
- action: 'getFrameOffset',
- params: {
- uniqueId,
- offset: [0, 0]
- }
- }, '*');
+ this._getFrameOffsetParent([0, 0], uniqueId, this._frameId);
const {offset} = await frameOffsetPromise;
return offset;
@@ -84,7 +76,7 @@ class FrameOffsetForwarder {
}
}
- _onGetFrameOffset(offset, uniqueId, e) {
+ _onMessageGetFrameOffset({offset, uniqueId, frameId}, e) {
let sourceFrame = null;
if (!this._unreachableContentWindowCache.has(e.source)) {
sourceFrame = this._findFrameWithContentWindow(e.source);
@@ -92,7 +84,7 @@ class FrameOffsetForwarder {
if (sourceFrame === null) {
// closed shadow root etc.
this._addToCache(this._unreachableContentWindowCache, e.source);
- this._forwardFrameOffsetOrigin(null, uniqueId);
+ this._replyFrameOffset(null, uniqueId, frameId);
return;
}
@@ -100,7 +92,11 @@ class FrameOffsetForwarder {
const {x, y} = sourceFrame.getBoundingClientRect();
offset = [forwardedX + x, forwardedY + y];
- this._forwardFrameOffset(offset, uniqueId);
+ if (window === window.parent) {
+ this._replyFrameOffset(offset, uniqueId, frameId);
+ } else {
+ this._getFrameOffsetParent(offset, uniqueId, frameId);
+ }
}
_findFrameWithContentWindow(contentWindow) {
@@ -148,7 +144,7 @@ class FrameOffsetForwarder {
}
yield frameCache;
// will contain duplicates, but frame elements are cheap to handle
- yield [...document.querySelectorAll('frame, iframe:not(.yomichan-float)')];
+ yield [...document.querySelectorAll('frame,iframe')];
yield [document.documentElement];
}
@@ -164,11 +160,18 @@ class FrameOffsetForwarder {
cache.add(value);
}
- _forwardFrameOffsetParent(offset, uniqueId) {
- window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*');
+ _getFrameOffsetParent(offset, uniqueId, frameId) {
+ window.parent.postMessage({
+ action: 'getFrameOffset',
+ params: {
+ offset,
+ uniqueId,
+ frameId
+ }
+ }, '*');
}
- _forwardFrameOffsetOrigin(offset, uniqueId) {
- api.broadcastTab('frameOffset', {offset, uniqueId});
+ _replyFrameOffset(offset, uniqueId, frameId) {
+ api.sendMessageToFrame(frameId, 'frameOffset', {offset, uniqueId});
}
}