From f079db0471424a873f22315c7911571d467e97ad Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 17 Apr 2016 18:13:20 -0700 Subject: Support switching between edict and enamdict --- ext/fg/js/api.js | 4 ++-- ext/fg/js/client.js | 38 +++++++++++++++++++------------------- ext/fg/js/popup.js | 22 ++++++++++++++++++++++ 3 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 ext/fg/js/popup.js (limited to 'ext/fg') diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index c65b1702..7997a73f 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -21,8 +21,8 @@ function sendMessage(action, data, callback) { chrome.runtime.sendMessage({action: action, data: data}, callback); } -function findTerm(text, callback) { - sendMessage('findTerm', {text: text}, callback); +function findTerm(text, dict, callback) { + sendMessage('findTerm', {text: text, dict: dict}, callback); } function findKanji(text, callback) { diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js index b757123c..4df19594 100644 --- a/ext/fg/js/client.js +++ b/ext/fg/js/client.js @@ -19,11 +19,12 @@ class Client { constructor() { - this.lastMosePos = null; - this.popupQuery = ''; - this.popupOffset = 10; - this.enabled = false; - this.options = null; + this.popupMousePos = null; + this.popupQuery = ''; + this.popupOffset = 10; + this.enabled = false; + this.options = {}; + this.activeDict = ''; this.popup = document.createElement('iframe'); this.popup.classList.add('yomichan-popup'); @@ -40,29 +41,29 @@ class Client { window.addEventListener('resize', (e) => this.hidePopup()); getOptions((opts) => { - this.setDict('edict'); + this.setActiveDict('edict'); this.setOptions(opts); getState((state) => this.setEnabled(state === 'enabled')); }); } onKeyDown(e) { - if (this.enabled && this.lastMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) { - this.searchAtPoint(this.lastMousePos); + if (this.enabled && this.popupMousePos !== null && (e.keyCode === 16 || e.charCode === 16)) { + this.searchAtPoint(this.popupMousePos); } } onMouseMove(e) { - this.lastMousePos = {x: e.clientX, y: e.clientY}; + this.popupMousePos = {x: e.clientX, y: e.clientY}; if (this.enabled && (e.shiftKey || e.which === 2)) { - this.searchAtPoint(this.lastMousePos); + this.searchAtPoint(this.popupMousePos); } } onMouseDown(e) { - this.lastMousePos = {x: e.clientX, y: e.clientY}; + this.popupMousePos = {x: e.clientX, y: e.clientY}; if (this.enabled && (e.shiftKey || e.which === 2)) { - this.searchAtPoint(this.lastMousePos); + this.searchAtPoint(this.popupMousePos); } else { this.hidePopup(); } @@ -84,8 +85,8 @@ class Client { onFrameMessage(e) { const {action, data} = e.data; switch (action) { - case 'selectDict': - this.setDict(data); + case 'setActiveDict': + this.setActiveDict(data); break; } } @@ -113,11 +114,11 @@ class Client { return; } - findTerm(popupQuery, ({results, length}) => { + findTerm(popupQuery, this.activeDict, ({results, length}) => { if (length === 0) { this.hidePopup(); } else { - const params = {defs: results, root: chrome.extension.getURL('fg')}; + const params = {defs: results, root: chrome.extension.getURL('fg'), activeDict: this.activeDict}; renderText(params, 'defs.html', (html) => this.showPopup(range, html, popupQuery, length)); } }); @@ -168,9 +169,8 @@ class Client { this.options = opts; } - setDict(dict) { - this.dict = dict; - alert(dict); + setActiveDict(activeDict) { + this.activeDict = activeDict; } } diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js new file mode 100644 index 00000000..2753a863 --- /dev/null +++ b/ext/fg/js/popup.js @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +function setActiveDict(dict) { + parent.postMessage({action: 'setActiveDict', data: dict}, '*'); +} -- cgit v1.2.3