Fix activation bug

This commit is contained in:
Alex Yatskov 2016-09-28 20:14:21 -07:00
parent 071902cd05
commit 004a0d5281
3 changed files with 25 additions and 8 deletions

View File

@ -23,6 +23,7 @@ class Yomichan {
Handlebars.registerHelper('kanjiLinks', kanjiLinks); Handlebars.registerHelper('kanjiLinks', kanjiLinks);
this.translator = new Translator(); this.translator = new Translator();
this.options = null;
this.importTabId = null; this.importTabId = null;
this.asyncPools = {}; this.asyncPools = {};
this.ankiConnectVer = 0; this.ankiConnectVer = 0;
@ -30,8 +31,6 @@ class Yomichan {
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this)); chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id));
chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this));
loadOptions().then(opts => { loadOptions().then(opts => {
this.setOptions(opts); this.setOptions(opts);
@ -66,11 +65,6 @@ class Yomichan {
return true; return true;
} }
onTabReady(tabId) {
this.tabInvoke(tabId, 'setOptions', this.options);
this.tabInvoke(tabId, 'setEnabled', this.state === 'enabled');
}
onBrowserAction() { onBrowserAction() {
switch (this.state) { switch (this.state) {
case 'disabled': case 'disabled':
@ -258,6 +252,14 @@ class Yomichan {
return note; return note;
} }
api_getEnabled({callback}) {
callback(this.state === 'enabled');
}
api_getOptions({callback}) {
loadOptions().then(opts => callback(opts));
}
api_addDefinition({definition, mode, callback}) { api_addDefinition({definition, mode, callback}) {
const note = this.formatNote(definition, mode); const note = this.formatNote(definition, mode);
this.ankiInvokeSafe('addNote', {note}, null, callback); this.ankiInvokeSafe('addNote', {note}, null, callback);

View File

@ -25,7 +25,7 @@ class Driver {
this.lastTextSource = null; this.lastTextSource = null;
this.pendingLookup = false; this.pendingLookup = false;
this.enabled = false; this.enabled = false;
this.options = {}; this.options = null;
this.definitions = null; this.definitions = null;
this.sequence = 0; this.sequence = 0;
this.fgRoot = chrome.extension.getURL('fg'); this.fgRoot = chrome.extension.getURL('fg');
@ -37,6 +37,13 @@ class Driver {
window.addEventListener('keydown', this.onKeyDown.bind(this)); window.addEventListener('keydown', this.onKeyDown.bind(this));
window.addEventListener('scroll', e => this.hidePopup()); window.addEventListener('scroll', e => this.hidePopup());
window.addEventListener('resize', e => this.hidePopup()); window.addEventListener('resize', e => this.hidePopup());
getOptions().then(opts => {
this.options = opts;
return getEnabled();
}).then(enabled => {
this.enabled = enabled;
});
} }
onKeyDown(e) { onKeyDown(e) {

View File

@ -21,6 +21,14 @@ function invokeApiBg(action, params) {
return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve)); return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve));
} }
function getEnabled() {
return invokeApiBg('getEnabled', {});
}
function getOptions() {
return invokeApiBg('getOptions', {});
}
function findTerm(text) { function findTerm(text) {
return invokeApiBg('findTerm', {text}); return invokeApiBg('findTerm', {text});
} }