This commit is contained in:
Alex Yatskov 2017-01-07 18:39:36 -08:00
parent dc4162eeed
commit fab7a03b6c
4 changed files with 45 additions and 40 deletions

View File

@ -32,11 +32,11 @@ class Driver {
window.addEventListener('mousedown', this.onMouseDown.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this));
window.addEventListener('keydown', this.onKeyDown.bind(this));
window.addEventListener('resize', e => this.hidePopup());
window.addEventListener('resize', e => this.searchClear());
getOptions().then(opts => {
this.options = opts;
return getEnabled();
return isEnabled();
}).then(enabled => {
this.enabled = enabled;
});
@ -60,7 +60,7 @@ class Driver {
if (this.enabled && this.lastMousePos !== null && e.keyCode === 16 /* shift */) {
this.searchAt(this.lastMousePos, true);
} else if (e.keyCode === 27 /* esc */) {
this.hidePopup();
this.searchClear();
}
}
@ -87,7 +87,7 @@ class Driver {
}
const searcher = () => this.searchAt(this.lastMousePos, false);
if (!this.popup.visible() || e.shiftKey || e.which === 2 /* mmb */) {
if (!this.popup.isVisible() || e.shiftKey || e.which === 2 /* mmb */) {
searcher();
} else {
this.popupTimerSet(searcher);
@ -97,7 +97,7 @@ class Driver {
onMouseDown(e) {
this.lastMousePos = {x: e.clientX, y: e.clientY};
this.popupTimerClear();
this.hidePopup();
this.searchClear();
}
onBgMessage({action, params}, sender, callback) {
@ -117,14 +117,14 @@ class Driver {
const textSource = textSourceFromPoint(point);
if (textSource === null || !textSource.containsPoint(point)) {
if (hideNotFound) {
this.hidePopup();
this.searchClear();
}
return;
}
if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
return true;
return;
}
this.pendingLookup = true;
@ -132,12 +132,12 @@ class Driver {
if (!found) {
this.searchKanji(textSource).then(found => {
if (!found && hideNotFound) {
this.hidePopup();
this.searchClear();
}
});
}
}).catch(error => {
alert('Error: ' + error);
window.alert('Error: ' + error);
}).then(() => {
this.pendingLookup = false;
});
@ -159,12 +159,13 @@ class Driver {
});
this.popup.showNextTo(textSource.getRect());
this.popup.invoke('showTermDefs', {definitions, options: this.options});
this.popup.showTermDefs(definitions, this.options);
return true;
}
}).catch(error => {
alert('Error: ' + error);
window.alert('Error: ' + error);
return false;
});
}
@ -178,16 +179,17 @@ class Driver {
definitions.forEach(definition => definition.url = window.location.href);
this.popup.showNextTo(textSource.getRect());
this.popup.invoke('showKanjiDefs', {definitions, options: this.options});
this.popup.showKanjiDefs(definitions, this.options);
return true;
}
}).catch(error => {
alert('Error: ' + error);
window.alert('Error: ' + error);
return false;
});
}
hidePopup() {
searchClear() {
this.popup.hide();
if (this.options.selectMatchedText && this.lastTextSource !== null) {
@ -195,7 +197,6 @@ class Driver {
}
this.lastTextSource = null;
this.definitions = null;
}
api_setOptions(opts) {
@ -204,7 +205,7 @@ class Driver {
api_setEnabled(enabled) {
if (!(this.enabled = enabled)) {
this.hidePopup();
this.searchClear();
}
}
}

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class FrameContext {
class Frame {
constructor() {
this.definitions = [];
this.audioCache = {};
@ -156,4 +156,4 @@ class FrameContext {
}
}
window.frameContext = new FrameContext();
window.frame = new Frame();

View File

@ -30,7 +30,7 @@ class Popup {
document.body.appendChild(this.container);
}
show(rect) {
showAt(rect) {
this.container.style.left = rect.x + 'px';
this.container.style.top = rect.y + 'px';
this.container.style.height = rect.height + 'px';
@ -59,22 +59,26 @@ class Popup {
y = elementRect.top - height - this.offset;
}
this.show({x, y, width, height});
}
visible() {
return this.container !== null && this.container.style.visibility !== 'hidden';
this.showAt({x, y, width, height});
}
hide() {
if (this.container !== null) {
this.container.style.visibility = 'hidden';
}
this.container.style.visibility = 'hidden';
}
invoke(action, params) {
if (this.container !== null) {
this.container.contentWindow.postMessage({action, params}, '*');
}
isVisible() {
return this.container.style.visibility !== 'hidden';
}
showTermDefs(definitions, options) {
this.invokeApi('showTermDefs', {definitions, options});
}
showKanjiDefs(definitions, options) {
this.invokeApi('showKanjiDefs', {definitions, options});
}
invokeApi(action, params) {
this.container.contentWindow.postMessage({action, params}, '*');
}
}

View File

@ -17,7 +17,7 @@
*/
function invokeApiBg(action, params) {
function invokeBgApi(action, params) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({action, params}, ({result, error}) => {
if (error) {
@ -29,32 +29,32 @@ function invokeApiBg(action, params) {
});
}
function getEnabled() {
return invokeApiBg('getEnabled', {});
function isEnabled() {
return invokeBgApi('getEnabled', {});
}
function getOptions() {
return invokeApiBg('getOptions', {});
return invokeBgApi('getOptions', {});
}
function findTerm(text) {
return invokeApiBg('findTerm', {text});
return invokeBgApi('findTerm', {text});
}
function findKanji(text) {
return invokeApiBg('findKanji', {text});
return invokeBgApi('findKanji', {text});
}
function renderText(data, template) {
return invokeApiBg('renderText', {data, template});
return invokeBgApi('renderText', {data, template});
}
function canAddDefinitions(definitions, modes) {
return invokeApiBg('canAddDefinitions', {definitions, modes}).catch(() => null);
return invokeBgApi('canAddDefinitions', {definitions, modes}).catch(() => null);
}
function addDefinition(definition, mode) {
return invokeApiBg('addDefinition', {definition, mode});
return invokeBgApi('addDefinition', {definition, mode});
}
function textSourceFromPoint(point) {