diff options
Diffstat (limited to 'ext/client.js')
| -rw-r--r-- | ext/client.js | 27 | 
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();  |