polish and initial command support

This commit is contained in:
Alex Yatskov 2017-03-18 13:00:29 -07:00
parent e782a43432
commit 57697ee2a0
6 changed files with 49 additions and 14 deletions

View File

@ -20,8 +20,13 @@
window.displayWindow = new class extends Display {
constructor() {
super($('#spinner'), $('#content'));
$('#search').click(this.onSearch.bind(this));
window.wanakana.bind($('#query').get(0));
const search = $('#search');
search.click(this.onSearch.bind(this));
const query = $('#query');
query.on('input', () => search.prop('disabled', query.val().length === 0));
window.wanakana.bind(query.get(0));
}
definitionAdd(definition, mode) {

View File

@ -18,7 +18,7 @@
$(document).ready(() => {
$('#open-search').click(() => window.open(chrome.extension.getURL('/bg/search.html')));
$('#open-search').click(() => commandExec('search'));
$('#open-options').click(() => chrome.runtime.openOptionsPage());
$('#open-help').click(() => window.open('http://foosoft.net/projects/yomichan'));
@ -26,9 +26,6 @@ $(document).ready(() => {
const toggle = $('#enable-search');
toggle.prop('checked', options.general.enable).change();
toggle.bootstrapToggle();
toggle.change(() => {
options.general.enable = toggle.prop('checked');
optionsSave(options).then(() => instYomi().optionsSet(options));
});
toggle.change(() => commandExec('toggle'));
});
});

View File

@ -33,6 +33,15 @@ function promiseCallback(promise, callback) {
}
/*
* Commands
*/
function commandExec(command) {
instYomi().onCommand(command);
}
/*
* Instance
*/

View File

@ -25,12 +25,13 @@ window.yomichan = new class {
this.anki = new AnkiNull();
this.options = null;
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
if (chrome.runtime.onInstalled) {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
}
this.translator.prepare().then(optionsLoad).then(this.optionsSet.bind(this));
this.translator.prepare().then(optionsLoad).then(this.optionsSet.bind(this)).then(() => {
chrome.commands.onCommand.addListener(this.onCommand.bind(this));
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
if (chrome.runtime.onInstalled) {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
}
});
}
optionsSet(options) {
@ -153,6 +154,15 @@ window.yomichan = new class {
}
}
onCommand(command) {
if (command === 'search') {
window.open(chrome.extension.getURL('/bg/search.html'));
} else if (command === 'toggle') {
this.options.general.enable = !this.options.general.enable;
optionsSave(this.options).then(() => this.optionsSet(this.options));
}
}
onMessage(request, sender, callback) {
const handlers = new class {
api_optionsGet({callback}) {

View File

@ -20,7 +20,7 @@
<form class="input-group">
<input type="text" class="form-control" placeholder="Search for..." id="query" autofocus>
<span class="input-group-btn">
<input type="submit" class="btn btn-default form-control" id="search" value="Search">
<input type="submit" class="btn btn-default form-control" id="search" value="Search" disabled>
</span>
</form>
</p>

View File

@ -31,6 +31,20 @@
"<all_urls>",
"storage"
],
"commands": {
"toggle": {
"suggested_key": {
"default": "Alt+Delete"
},
"description": "Toggle text scanning"
},
"search": {
"suggested_key": {
"default": "Alt+Insert"
},
"description": "Open search window"
}
},
"web_accessible_resources": ["fg/frame.html"],
"applications": {
"gecko": {