load settings from content

jh-changes
Shin'ya Ueoka 7 years ago
parent 8ff302a1f2
commit 30641f1b75
  1. 25
      src/components/background.js
  2. 2
      src/components/content-input.js
  3. 15
      src/content/index.js
  4. 3
      src/content/messages.js

@ -19,8 +19,6 @@ export default class BackgroundComponent {
}); });
} }
}); });
browser.tabs.onUpdated.addListener(this.onTabUpdated.bind(this));
browser.tabs.onActivated.addListener(this.onTabActivated.bind(this));
} }
update() { update() {
@ -59,28 +57,23 @@ export default class BackgroundComponent {
text: e.message, text: e.message,
}); });
}); });
case messages.SETTINGS_QUERY:
return Promise.resolve(this.store.getState().setting.settings);
case messages.CONSOLE_QUERY_COMPLETIONS: case messages.CONSOLE_QUERY_COMPLETIONS:
return commands.complete(message.text, this.settings); return commands.complete(message.text, this.settings);
case messages.SETTINGS_RELOAD: case messages.SETTINGS_RELOAD:
this.store.dispatch(settingsActions.load()); this.store.dispatch(settingsActions.load());
return this.broadcastSettingsChanged();
} }
} }
onTabActivated(info) { broadcastSettingsChanged() {
this.syncSettings(info.tabId); return browser.tabs.query({}).then((tabs) => {
} for (let tab of tabs) {
browser.tabs.sendMessage(tab.id, {
onTabUpdated(id, info) { type: messages.SETTINGS_CHANGED,
if (info.status === 'complete') { });
this.syncSettings(id);
}
} }
syncSettings(id) {
let { settings } = this.store.getState().setting;
return browser.tabs.sendMessage(id, {
type: messages.CONTENT_SET_SETTINGS,
settings,
}); });
} }
} }

@ -13,7 +13,7 @@ export default class ContentInputComponent {
update() { update() {
let settings = this.store.getState().setting.settings; let settings = this.store.getState().setting.settings;
if (!settings) { if (!settings || !settings.json) {
return; return;
} }
let input = this.store.getState().input; let input = this.store.getState().input;

@ -21,16 +21,25 @@ store.subscribe(() => {
consoleFrames.initialize(window.document); consoleFrames.initialize(window.document);
const reloadSettings = () => {
return browser.runtime.sendMessage({
type: messages.SETTINGS_QUERY,
}).then((settings) => {
store.dispatch(settingActions.set(settings));
});
};
browser.runtime.onMessage.addListener((action) => { browser.runtime.onMessage.addListener((action) => {
switch (action.type) { switch (action.type) {
case messages.CONSOLE_HIDE: case messages.CONSOLE_HIDE:
window.focus(); window.focus();
consoleFrames.blur(window.document); consoleFrames.blur(window.document);
return Promise.resolve(); return Promise.resolve();
case messages.CONTENT_SET_SETTINGS: case messages.SETTINGS_CHANGED:
store.dispatch(settingActions.set(action.settings)); return reloadSettings();
return Promise.resolve();
default: default:
return Promise.resolve(); return Promise.resolve();
} }
}); });
reloadSettings();

@ -1,5 +1,6 @@
export default { export default {
CONTENT_SET_SETTINGS: 'content.set.settings', SETTINGS_QUERY: 'settings.query',
BACKGROUND_OPERATION: 'background.operation', BACKGROUND_OPERATION: 'background.operation',
CONSOLE_BLURRED: 'console.blured', CONSOLE_BLURRED: 'console.blured',