This commit is contained in:
Alex Yatskov 2016-07-23 15:14:13 -07:00
parent 6d9b4392e2
commit 22d73228ca
3 changed files with 24 additions and 30 deletions

View File

@ -40,6 +40,8 @@ class Yomichan {
chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this)); chrome.runtime.onInstalled.addListener(this.onInstalled.bind(this));
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(this.onTabReady.bind(this));
chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this));
loadOptions((opts) => { loadOptions((opts) => {
this.setOptions(opts); this.setOptions(opts);
@ -66,6 +68,11 @@ class Yomichan {
return true; return true;
} }
onTabReady(tab) {
this.tabInvoke(tab, 'setOptions', this.options);
this.tabInvoke(tab, 'setEnabled', this.state === 'enabled');
}
onBrowserAction(tab) { onBrowserAction(tab) {
switch (this.state) { switch (this.state) {
case 'disabled': case 'disabled':
@ -97,26 +104,30 @@ class Yomichan {
break; break;
} }
this.notifyTabs('state', this.state); this.tabInvokeAll('setEnabled', this.state === 'enabled');
} }
setOptions(options) { setOptions(options) {
this.options = options; this.options = options;
this.notifyTabs('options', this.options); this.tabInvokeAll('setOptions', this.options);
} }
getApiVersion() { getApiVersion() {
return 1; return 1;
} }
notifyTabs(name, value) { tabInvokeAll(action, params) {
chrome.tabs.query({}, (tabs) => { chrome.tabs.query({}, (tabs) => {
for (let tab of tabs) { for (let tab of tabs) {
chrome.tabs.sendMessage(tab.id, {name, value}, () => null); this.tabInvoke(tab.id, action, params);
} }
}); });
} }
tabInvoke(tabId, action, params) {
chrome.tabs.sendMessage(tabId, {action, params}, () => null);
}
ankiInvokeSafe(action, params, pool, callback) { ankiInvokeSafe(action, params, pool, callback) {
this.api_getVersion({callback: (version) => { this.api_getVersion({callback: (version) => {
if (version === this.getApiVersion()) { if (version === this.getApiVersion()) {

View File

@ -33,14 +33,6 @@ function bgRenderText(data, template, callback) {
bgSendMessage('renderText', {data, template}, callback); bgSendMessage('renderText', {data, template}, callback);
} }
function bgGetOptions(callback) {
bgSendMessage('getOptions', {}, callback);
}
function bgGetState(callback) {
bgSendMessage('getState', {}, callback);
}
function bgCanAddDefinitions(definitions, modes, callback) { function bgCanAddDefinitions(definitions, modes, callback) {
bgSendMessage('canAddDefinitions', {definitions, modes}, callback); bgSendMessage('canAddDefinitions', {definitions, modes}, callback);
} }

View File

@ -38,11 +38,6 @@ class Client {
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());
bgGetOptions((opts) => {
this.setOptions(opts);
bgGetState((state) => this.setEnabled(state === 'enabled'));
});
} }
onKeyDown(e) { onKeyDown(e) {
@ -67,14 +62,10 @@ class Client {
} }
} }
onBgMessage({name, value}, sender, callback) { onBgMessage({action, params}, sender, callback) {
switch (name) { const method = this['api_' + action];
case 'state': if (typeof(method) === 'function') {
this.setEnabled(value === 'enabled'); method.call(this, params);
break;
case 'options':
this.setOptions(value);
break;
} }
callback(); callback();
@ -159,16 +150,16 @@ class Client {
this.definitions = null; this.definitions = null;
} }
setEnabled(enabled) { api_setOptions(opts) {
this.options = opts;
}
api_setEnabled(enabled) {
if (!(this.enabled = enabled)) { if (!(this.enabled = enabled)) {
this.hidePopup(); this.hidePopup();
} }
} }
setOptions(opts) {
this.options = opts;
}
api_addNote({index, mode}) { api_addNote({index, mode}) {
const state = {}; const state = {};
state[mode] = false; state[mode] = false;