summaryrefslogtreecommitdiff
path: root/ext/fg/js/client.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-04-14 21:49:34 -0700
committerAlex Yatskov <alex@foosoft.net>2016-04-14 21:49:34 -0700
commit0cd7830281a87bbe449a547ed868ae1f62c5bc97 (patch)
tree6a7a1dd35049593f466ca49f97b4c699942123b4 /ext/fg/js/client.js
parent5f74c473cef4564ad2040d9211f70b4f0ac048f1 (diff)
Optimization
Diffstat (limited to 'ext/fg/js/client.js')
-rw-r--r--ext/fg/js/client.js31
1 files changed, 16 insertions, 15 deletions
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js
index ea18e41f..99d86019 100644
--- a/ext/fg/js/client.js
+++ b/ext/fg/js/client.js
@@ -20,7 +20,7 @@
class Client {
constructor() {
this.lastMosePos = null;
- this.popupText = '';
+ this.popupQuery = '';
this.popupOffset = 10;
this.enabled = false;
this.options = null;
@@ -38,9 +38,7 @@ class Client {
getOptions((opts) => {
this.setOptions(opts);
- getState((state) => {
- this.setEnabled(state === 'enabled');
- });
+ getState((state) => this.setEnabled(state === 'enabled'));
});
}
@@ -97,26 +95,25 @@ class Client {
return;
}
- const text = range.toString();
- if (text === this.popupText) {
+ const popupQuery = range.toString();
+ if (popupQuery === this.popupQuery) {
return;
}
- findTerm(text, ({results, length}) => {
+ findTerm(popupQuery, ({results, length}) => {
if (length === 0) {
this.hidePopup();
} else {
- range.setEnd(range.endContainer, range.startOffset + length);
- renderText({defs: results, root: chrome.extension.getURL('fg')}, 'defs.html', (html) => {
- this.popup.setAttribute('srcdoc', html);
- this.showPopup(range);
- });
+ const params = {defs: results, root: chrome.extension.getURL('fg')};
+ renderText(params, 'defs.html', (html) => this.showPopup(range, html, popupQuery, length));
}
});
}
- showPopup(range) {
+ showPopup(range, html, popupQuery, length) {
if (this.options.highlightText) {
+ range.setEnd(range.endContainer, range.startOffset + length);
+
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
@@ -124,10 +121,14 @@ class Client {
const pos = getPopupPositionForRange(this.popup, range, this.popupOffset);
- this.popupText = range.toString();
+ if (this.popup.getAttribute('srcdoc') !== html) {
+ this.popup.setAttribute('srcdoc', html);
+ }
+
this.popup.style.left = pos.x + 'px';
this.popup.style.top = pos.y + 'px';
this.popup.style.visibility = 'visible';
+ this.popupQuery = popupQuery;
}
hidePopup() {
@@ -140,8 +141,8 @@ class Client {
selection.removeAllRanges();
}
- this.popupText = '';
this.popup.style.visibility = 'hidden';
+ this.popupQuery = '';
}
setEnabled(enabled) {