diff options
-rw-r--r-- | ext/fg/js/client.js | 34 | ||||
-rw-r--r-- | ext/fg/js/source-range.js | 17 |
2 files changed, 28 insertions, 23 deletions
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js index 16ee0b24..46e4683e 100644 --- a/ext/fg/js/client.js +++ b/ext/fg/js/client.js @@ -19,17 +19,17 @@ class Client { constructor() { - this.popup = new Popup(); - this.audio = {}; - this.lastMousePos = null; - this.lastTextSource = null; - this.activateKey = 16; - this.activateBtn = 2; - this.enabled = false; - this.options = {}; - this.definitions = null; - this.sequence = 0; - this.fgRoot = chrome.extension.getURL('fg'); + this.popup = new Popup(); + this.audio = {}; + this.lastMousePos = null; + this.lastTextSource = null; + this.activateKey = 16; + this.activateBtn = 2; + this.enabled = false; + this.options = {}; + this.definitions = null; + this.sequence = 0; + this.fgRoot = chrome.extension.getURL('fg'); chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); window.addEventListener('message', this.onFrameMessage.bind(this)); @@ -88,7 +88,17 @@ class Client { } textSourceFromPoint(point) { - return Range.fromPoint(point); + const element = document.elementFromPoint(point.x, point.y); + if (element !== null && element.nodeName === 'IMG') { + return new ImageSource(element); + } + + const range = document.caretRangeFromPoint(point.x, point.y); + if (range !== null) { + return new RangeSource(range); + } + + return null; } searchAt(point) { diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js index c4cabf4b..a6c3d722 100644 --- a/ext/fg/js/source-range.js +++ b/ext/fg/js/source-range.js @@ -17,7 +17,7 @@ */ -class Range { +class RangeSource { constructor(range) { this.rng = range; } @@ -27,7 +27,7 @@ class Range { } setLength(length) { - const end = Range.seekEnd(this.rng.startContainer, this.rng.startOffset + length); + const end = RangeSource.seekEnd(this.rng.startContainer, this.rng.startOffset + length); this.rng.setEnd(end.node, end.offset); } @@ -70,18 +70,18 @@ class Range { static seekEnd(node, length) { const state = {node, offset: 0, length}; - if (!Range.seekEndRecurse(node, state)) { + if (!RangeSource.seekEndRecurse(node, state)) { return {node: state.node, offset: state.offset}; } for (let sibling = node.nextSibling; sibling !== null; sibling = sibling.nextSibling) { - if (!Range.seekEndRecurse(sibling, state)) { + if (!RangeSource.seekEndRecurse(sibling, state)) { return {node: state.node, offset: state.offset}; } } for (let sibling = node.parentElement.nextSibling; sibling !== null; sibling = sibling.nextSibling) { - if (!Range.seekEndRecurse(sibling, state)) { + if (!RangeSource.seekEndRecurse(sibling, state)) { return {node: state.node, offset: state.offset}; } } @@ -97,7 +97,7 @@ class Range { state.length -= consumed; } else { for (let i = 0; i < node.childNodes.length; ++i) { - if (!Range.seekEndRecurse(node.childNodes[i], state)) { + if (!RangeSource.seekEndRecurse(node.childNodes[i], state)) { break; } } @@ -105,9 +105,4 @@ class Range { return state.length > 0; } - - static fromPoint(point) { - const range = document.caretRangeFromPoint(point.x, point.y); - return range === null ? null : new Range(range); - } } |