This commit is contained in:
Alex Yatskov 2016-07-23 12:47:42 -07:00
parent 6099de71d8
commit a889e1ffaa
2 changed files with 28 additions and 23 deletions

View File

@ -19,17 +19,17 @@
class Client { class Client {
constructor() { constructor() {
this.popup = new Popup(); this.popup = new Popup();
this.audio = {}; this.audio = {};
this.lastMousePos = null; this.lastMousePos = null;
this.lastTextSource = null; this.lastTextSource = null;
this.activateKey = 16; this.activateKey = 16;
this.activateBtn = 2; this.activateBtn = 2;
this.enabled = false; this.enabled = false;
this.options = {}; this.options = {};
this.definitions = null; this.definitions = null;
this.sequence = 0; this.sequence = 0;
this.fgRoot = chrome.extension.getURL('fg'); this.fgRoot = chrome.extension.getURL('fg');
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
window.addEventListener('message', this.onFrameMessage.bind(this)); window.addEventListener('message', this.onFrameMessage.bind(this));
@ -88,7 +88,17 @@ class Client {
} }
textSourceFromPoint(point) { 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) { searchAt(point) {

View File

@ -17,7 +17,7 @@
*/ */
class Range { class RangeSource {
constructor(range) { constructor(range) {
this.rng = range; this.rng = range;
} }
@ -27,7 +27,7 @@ class Range {
} }
setLength(length) { 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); this.rng.setEnd(end.node, end.offset);
} }
@ -70,18 +70,18 @@ class Range {
static seekEnd(node, length) { static seekEnd(node, length) {
const state = {node, offset: 0, length}; const state = {node, offset: 0, length};
if (!Range.seekEndRecurse(node, state)) { if (!RangeSource.seekEndRecurse(node, state)) {
return {node: state.node, offset: state.offset}; return {node: state.node, offset: state.offset};
} }
for (let sibling = node.nextSibling; sibling !== null; sibling = sibling.nextSibling) { 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}; return {node: state.node, offset: state.offset};
} }
} }
for (let sibling = node.parentElement.nextSibling; sibling !== null; sibling = sibling.nextSibling) { 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}; return {node: state.node, offset: state.offset};
} }
} }
@ -97,7 +97,7 @@ class Range {
state.length -= consumed; state.length -= consumed;
} else { } else {
for (let i = 0; i < node.childNodes.length; ++i) { for (let i = 0; i < node.childNodes.length; ++i) {
if (!Range.seekEndRecurse(node.childNodes[i], state)) { if (!RangeSource.seekEndRecurse(node.childNodes[i], state)) {
break; break;
} }
} }
@ -105,9 +105,4 @@ class Range {
return state.length > 0; return state.length > 0;
} }
static fromPoint(point) {
const range = document.caretRangeFromPoint(point.x, point.y);
return range === null ? null : new Range(range);
}
} }