summaryrefslogtreecommitdiff
path: root/ext/client.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-03-27 17:04:49 -0700
committerAlex Yatskov <alex@foosoft.net>2016-03-27 17:04:49 -0700
commit43d14c0c81824d71cda16c9921348719844044af (patch)
tree91cffa6faf971efbeaf6efab6a5995b251de961d /ext/client.js
parent4b35a6c5563ca7a0d050c049280aafeb097060c0 (diff)
Updates
Diffstat (limited to 'ext/client.js')
-rw-r--r--ext/client.js27
1 files changed, 23 insertions, 4 deletions
diff --git a/ext/client.js b/ext/client.js
index 535d3748..803f7db6 100644
--- a/ext/client.js
+++ b/ext/client.js
@@ -19,15 +19,23 @@
class Client {
constructor() {
- $('body').append('<div class="yomichan-popup"/>');
-
- this.popup = $('.yomichan-popup');
+ this.popup = $('<div class="yomichan-popup"/>');
this.popupOffset = 10;
+ this.enabled = false;
+
+ $('body').append(this.popup);
+
+ chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
+ window.addEventListener('mousemove', this.onMouseMove.bind(this));
- window.addEventListener('mousemove', whenEnabled(this.onMouseMove.bind(this)));
+ getState((state) => this.setEnabled(state === 'enabled'));
}
onMouseMove(e) {
+ if (!this.enabled) {
+ return;
+ }
+
const range = getRangeAtCursor(e, 10);
if (range === null) {
this.hidePopup();
@@ -43,6 +51,11 @@ class Client {
this.showPopup(range);
}
+ onMessage(request, sender, callback) {
+ this.setEnabled(request === 'enabled');
+ callback();
+ }
+
showPopup(range) {
const selection = window.getSelection();
selection.removeAllRanges();
@@ -58,6 +71,12 @@ class Client {
this.popup.css({visibility: 'hidden'});
}
+
+ setEnabled(enabled) {
+ if (!(this.enabled = enabled)) {
+ this.hidePopup();
+ }
+ }
}
window.yomiClient = new Client();