diff --git a/ext/js/display/display.js b/ext/js/display/display.js index 923c9dd2..8905d304 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -178,7 +178,7 @@ class Display extends EventDispatcher { ['previousEntryDifferentDictionary', () => { this._focusEntryWithDifferentDictionary(-1, true); }] ]); this.registerDirectMessageHandlers([ - ['Display.setOptionsContext', {async: false, handler: this._onMessageSetOptionsContext.bind(this)}], + ['Display.setOptionsContext', {async: true, handler: this._onMessageSetOptionsContext.bind(this)}], ['Display.setContent', {async: false, handler: this._onMessageSetContent.bind(this)}], ['Display.setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}], ['Display.setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}], @@ -459,21 +459,25 @@ class Display extends EventDispatcher { this._documentFocusController.blurElement(element); } - searchLast() { + searchLast(updateOptionsContext) { const type = this._contentType; if (type === 'clear') { return; } const query = this._query; + const hasState = this._historyHasState(); const state = ( - this._historyHasState() ? + hasState ? clone(this._history.state) : { focusEntry: 0, - optionsContext: this._optionsContext, + optionsContext: null, url: window.location.href, sentence: {text: query, offset: 0}, documentTitle: document.title } ); + if (!hasState || updateOptionsContext) { + state.optionsContext = clone(this._optionsContext); + } const details = { focus: false, historyMode: 'clear', @@ -551,9 +555,9 @@ class Display extends EventDispatcher { invokeMessageHandler(messageHandler, params, callback); } - _onMessageSetOptionsContext({optionsContext}) { - this.setOptionsContext(optionsContext); - this.searchLast(); + async _onMessageSetOptionsContext({optionsContext}) { + await this.setOptionsContext(optionsContext); + this.searchLast(true); } _onMessageSetContent({details}) { diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js index cd35481d..6131e0db 100644 --- a/ext/js/display/search-display-controller.js +++ b/ext/js/display/search-display-controller.js @@ -135,7 +135,7 @@ class SearchDisplayController { await this._display.updateOptions(); const query = this._queryInput.value; if (query) { - this._display.searchLast(); + this._display.searchLast(false); } } diff --git a/ext/js/pages/info-main.js b/ext/js/pages/info-main.js index 8e83b4e8..54e6ff45 100644 --- a/ext/js/pages/info-main.js +++ b/ext/js/pages/info-main.js @@ -111,7 +111,7 @@ function getOperatingSystemDisplayName(os) { })(); const settingsController = new SettingsController(); - settingsController.prepare(); + await settingsController.prepare(); const backupController = new BackupController(settingsController, null); await backupController.prepare(); diff --git a/ext/js/pages/permissions-main.js b/ext/js/pages/permissions-main.js index df4b07ee..acfecbad 100644 --- a/ext/js/pages/permissions-main.js +++ b/ext/js/pages/permissions-main.js @@ -93,8 +93,8 @@ function setupPermissionsToggles() { const modalController = new ModalController(); modalController.prepare(); - const settingsController = new SettingsController(0); - settingsController.prepare(); + const settingsController = new SettingsController(); + await settingsController.prepare(); const permissionsToggleController = new PermissionsToggleController(settingsController); permissionsToggleController.prepare(); diff --git a/ext/js/pages/settings/profile-controller.js b/ext/js/pages/settings/profile-controller.js index e31bc07e..018c9ec1 100644 --- a/ext/js/pages/settings/profile-controller.js +++ b/ext/js/pages/settings/profile-controller.js @@ -215,13 +215,13 @@ class ProfileController { this._updateProfileSelectOptions(); - // Modify settings - await this._settingsController.modifyGlobalSettings(modifications); - // Update profile index if (settingsProfileIndex === profileIndex) { this._settingsController.profileIndex = profileCurrentNew; } + + // Modify settings + await this._settingsController.modifyGlobalSettings(modifications); } async swapProfiles(index1, index2) { diff --git a/ext/js/pages/settings/settings-controller.js b/ext/js/pages/settings/settings-controller.js index b0180827..111b98e4 100644 --- a/ext/js/pages/settings/settings-controller.js +++ b/ext/js/pages/settings/settings-controller.js @@ -22,9 +22,9 @@ */ class SettingsController extends EventDispatcher { - constructor(profileIndex=0) { + constructor() { super(); - this._profileIndex = profileIndex; + this._profileIndex = 0; this._source = generateId(16); this._pageExitPreventions = new Set(); this._pageExitPreventionEventListeners = new EventListenerCollection(); @@ -42,23 +42,28 @@ class SettingsController extends EventDispatcher { set profileIndex(value) { if (this._profileIndex === value) { return; } - this._setProfileIndex(value); + this._setProfileIndex(value, true); } get permissionsUtil() { return this._permissionsUtil; } - prepare() { + async prepare() { yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this)); if (this._canObservePermissionsChanges()) { chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this)); chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this)); } + const optionsFull = await this.getOptionsFull(); + const {profiles, profileCurrent} = optionsFull; + if (profileCurrent >= 0 && profileCurrent < profiles.length) { + this._profileIndex = profileCurrent; + } } async refresh() { - await this._onOptionsUpdatedInternal(); + await this._onOptionsUpdatedInternal(true); } async getOptions() { @@ -73,7 +78,7 @@ class SettingsController extends EventDispatcher { async setAllSettings(value) { const profileIndex = value.profileCurrent; await yomichan.api.setAllSettings(value, this._source); - this._setProfileIndex(profileIndex); + this._setProfileIndex(profileIndex, true); } async getSettings(targets) { @@ -143,21 +148,29 @@ class SettingsController extends EventDispatcher { // Private - _setProfileIndex(value) { + _setProfileIndex(value, canUpdateProfileIndex) { this._profileIndex = value; this.trigger('optionsContextChanged'); - this._onOptionsUpdatedInternal(); + this._onOptionsUpdatedInternal(canUpdateProfileIndex); } _onOptionsUpdated({source}) { if (source === this._source) { return; } - this._onOptionsUpdatedInternal(); + this._onOptionsUpdatedInternal(true); } - async _onOptionsUpdatedInternal() { + async _onOptionsUpdatedInternal(canUpdateProfileIndex) { const optionsContext = this.getOptionsContext(); - const options = await this.getOptions(); - this.trigger('optionsChanged', {options, optionsContext}); + try { + const options = await this.getOptions(); + this.trigger('optionsChanged', {options, optionsContext}); + } catch (e) { + if (canUpdateProfileIndex) { + this._setProfileIndex(0, false); + return; + } + throw e; + } } _setupTargets(targets, extraFields) { diff --git a/ext/js/pages/settings/settings-main.js b/ext/js/pages/settings/settings-main.js index a3dcf357..aa94ef46 100644 --- a/ext/js/pages/settings/settings-main.js +++ b/ext/js/pages/settings/settings-main.js @@ -77,15 +77,13 @@ async function setupGenericSettingsController(genericSettingController) { } delete document.documentElement.dataset.loadingStalled; - const optionsFull = await yomichan.api.optionsGetFull(); - const preparePromises = []; const modalController = new ModalController(); modalController.prepare(); - const settingsController = new SettingsController(optionsFull.profileCurrent); - settingsController.prepare(); + const settingsController = new SettingsController(); + await settingsController.prepare(); const persistentStorageController = new PersistentStorageController(); persistentStorageController.prepare(); diff --git a/ext/js/pages/welcome-main.js b/ext/js/pages/welcome-main.js index 16ef7b52..72eef995 100644 --- a/ext/js/pages/welcome-main.js +++ b/ext/js/pages/welcome-main.js @@ -56,15 +56,13 @@ async function setupGenericSettingsController(genericSettingController) { setupEnvironmentInfo(); - const optionsFull = await yomichan.api.optionsGetFull(); - const preparePromises = []; const modalController = new ModalController(); modalController.prepare(); - const settingsController = new SettingsController(optionsFull.profileCurrent); - settingsController.prepare(); + const settingsController = new SettingsController(); + await settingsController.prepare(); const dictionaryController = new DictionaryController(settingsController, modalController, statusFooter); dictionaryController.prepare();