aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg/js')
-rw-r--r--ext/fg/js/api.js12
-rw-r--r--ext/fg/js/document.js74
-rw-r--r--ext/fg/js/frontend.js6
-rw-r--r--ext/fg/js/popup.js8
-rw-r--r--ext/fg/js/source.js57
5 files changed, 111 insertions, 46 deletions
diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js
index 4b4d9d74..0c86b412 100644
--- a/ext/fg/js/api.js
+++ b/ext/fg/js/api.js
@@ -33,8 +33,8 @@ function apiKanjiFind(text) {
return utilInvoke('kanjiFind', {text});
}
-function apiDefinitionAdd(definition, mode) {
- return utilInvoke('definitionAdd', {definition, mode});
+function apiDefinitionAdd(definition, mode, context) {
+ return utilInvoke('definitionAdd', {definition, mode, context});
}
function apiDefinitionsAddable(definitions, modes) {
@@ -53,6 +53,10 @@ function apiCommandExec(command) {
return utilInvoke('commandExec', {command});
}
-function apiAudioGetUrl(definition, source) {
- return utilInvoke('audioGetUrl', {definition, source});
+function apiScreenshotGet(options) {
+ return utilInvoke('screenshotGet', {options});
+}
+
+function apiForward(action, params) {
+ return utilInvoke('forward', {action, params});
}
diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js
index f58a64fc..86396a8a 100644
--- a/ext/fg/js/document.js
+++ b/ext/fg/js/document.js
@@ -17,6 +17,8 @@
*/
+const IS_FIREFOX = /Firefox/.test(navigator.userAgent);
+
function docOffsetCalc(element) {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
@@ -69,43 +71,23 @@ function docRangeFromPoint(point) {
const element = document.elementFromPoint(point.x, point.y);
let imposter = null;
if (element) {
- if (element.nodeName === 'IMG' || element.nodeName === 'BUTTON') {
- return new TextSourceElement(element);
- } else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
- imposter = docImposterCreate(element);
+ switch (element.nodeName) {
+ case 'IMG':
+ case 'BUTTON':
+ return new TextSourceElement(element);
+ case 'INPUT':
+ case 'TEXTAREA':
+ imposter = docImposterCreate(element);
+ break;
}
}
- if (!document.caretRangeFromPoint) {
- document.caretRangeFromPoint = (x, y) => {
- const position = document.caretPositionFromPoint(x,y);
- if (position && position.offsetNode && position.offsetNode.nodeType === Node.TEXT_NODE) {
- const range = document.createRange();
- range.setStart(position.offsetNode, position.offset);
- range.setEnd(position.offsetNode, position.offset);
- return range;
- }
- return null;
- };
- }
-
const range = document.caretRangeFromPoint(point.x, point.y);
- if (range === null) {
- return;
+ if (imposter !== null) {
+ imposter.style.zIndex = -2147483646;
}
- if(imposter !== null) imposter.style.zIndex = -2147483646;
-
- const rects = range.getClientRects();
- for (const rect of rects) {
- if (point.y <= rect.bottom + 2) {
- return new TextSourceRange(range);
- }
- }
-
- if (navigator.userAgent.match(/Firefox/)) {
- return new TextSourceRange(range);
- }
+ return range !== null && isPointInRange(point, range) ? new TextSourceRange(range) : null;
}
function docSentenceExtract(source, extent) {
@@ -178,3 +160,33 @@ function docSentenceExtract(source, extent) {
offset: position - startPos - padding
};
}
+
+function isPointInRange(point, range) {
+ if (IS_FIREFOX) {
+ // Always return true on Firefox due to an issue where range.getClientRects()
+ // does not return a correct set of rects for characters at the beginning of a line.
+ return true;
+ }
+
+ const y = point.y - 2;
+ for (const rect of range.getClientRects()) {
+ if (y <= rect.bottom) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+if (typeof document.caretRangeFromPoint !== 'function') {
+ document.caretRangeFromPoint = (x, y) => {
+ const position = document.caretPositionFromPoint(x, y);
+ if (position && position.offsetNode && position.offsetNode.nodeType === Node.TEXT_NODE) {
+ const range = document.createRange();
+ range.setStart(position.offsetNode, position.offset);
+ range.setEnd(position.offsetNode, position.offset);
+ return range;
+ }
+ return null;
+ };
+}
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 83fd9aff..3c5f2ac8 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -197,7 +197,7 @@ class Frontend {
}
onTouchMove(e) {
- if (!this.scrollPrevent || this.primaryTouchIdentifier === null) {
+ if (!this.scrollPrevent || !e.cancelable || this.primaryTouchIdentifier === null) {
return;
}
@@ -249,6 +249,10 @@ class Frontend {
if (!this.options.enable) {
this.searchClear();
}
+ },
+
+ popupSetVisible: ({visible}) => {
+ this.popup.setVisible(visible);
}
};
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js
index c8cc9baa..18dc0386 100644
--- a/ext/fg/js/popup.js
+++ b/ext/fg/js/popup.js
@@ -113,6 +113,14 @@ class Popup {
return this.injected && this.container.style.visibility !== 'hidden';
}
+ setVisible(visible) {
+ if (visible) {
+ this.container.style.setProperty('display', '');
+ } else {
+ this.container.style.setProperty('display', 'none', 'important');
+ }
+ }
+
containsPoint(point) {
if (!this.isVisible()) {
return false;
diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js
index 664dbec7..bbf00e30 100644
--- a/ext/fg/js/source.js
+++ b/ext/fg/js/source.js
@@ -16,6 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+// \u200c (Zero-width non-joiner) appears on Google Docs from Chrome 76 onwards
+const IGNORE_TEXT_PATTERN = /\u200c/;
+
/*
* TextSourceRange
@@ -124,11 +127,23 @@ class TextSourceRange {
static seekForwardHelper(node, state) {
if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) {
const offset = state.node === node ? state.offset : 0;
- const remaining = node.length - offset;
- const consumed = Math.min(remaining, state.remainder);
- state.content = state.content + node.nodeValue.substring(offset, offset + consumed);
+
+ let consumed = 0;
+ let stripped = 0;
+ while (state.remainder - consumed > 0) {
+ const currentChar = node.nodeValue[offset + consumed + stripped];
+ if (!currentChar) {
+ break;
+ } else if (currentChar.match(IGNORE_TEXT_PATTERN)) {
+ stripped++;
+ } else {
+ consumed++;
+ state.content += currentChar;
+ }
+ }
+
state.node = node;
- state.offset = offset + consumed;
+ state.offset = offset + consumed + stripped;
state.remainder -= consumed;
} else if (TextSourceRange.shouldEnter(node)) {
for (let i = 0; i < node.childNodes.length; ++i) {
@@ -161,11 +176,23 @@ class TextSourceRange {
static seekBackwardHelper(node, state) {
if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) {
const offset = state.node === node ? state.offset : node.length;
- const remaining = offset;
- const consumed = Math.min(remaining, state.remainder);
- state.content = node.nodeValue.substring(offset - consumed, offset) + state.content;
+
+ let consumed = 0;
+ let stripped = 0;
+ while (state.remainder - consumed > 0) {
+ const currentChar = node.nodeValue[offset - consumed - stripped]; // negative indices are undefined in JS
+ if (!currentChar) {
+ break;
+ } else if (currentChar.match(IGNORE_TEXT_PATTERN)) {
+ stripped++;
+ } else {
+ consumed++;
+ state.content = currentChar + state.content;
+ }
+ }
+
state.node = node;
- state.offset = offset - consumed;
+ state.offset = offset - consumed - stripped;
state.remainder -= consumed;
} else if (TextSourceRange.shouldEnter(node)) {
for (let i = node.childNodes.length - 1; i >= 0; --i) {
@@ -211,8 +238,18 @@ class TextSourceElement {
break;
}
- this.content = this.content || '';
- this.content = this.content.substring(0, length);
+ let consumed = 0;
+ let content = '';
+ for (let currentChar of this.content || '') {
+ if (consumed >= length) {
+ break;
+ } else if (!currentChar.match(IGNORE_TEXT_PATTERN)) {
+ consumed++;
+ content += currentChar;
+ }
+ }
+
+ this.content = content;
return this.content.length;
}