Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
at lambda-fork/main 93 lines 2.6 kB view raw
1/* 2 * Copyright (C) 2023-2025 Yomitan Authors 3 * Copyright (C) 2021-2022 Yomichan Authors 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19import {EventDispatcher} from '../core/event-dispatcher.js'; 20 21/** 22 * @augments EventDispatcher<import('search-persistent-state-controller').Events> 23 */ 24export class SearchPersistentStateController extends EventDispatcher { 25 constructor() { 26 super(); 27 /** @type {import('display').SearchMode} */ 28 this._mode = null; 29 } 30 31 /** @type {import('display').SearchMode} */ 32 get mode() { 33 return this._mode; 34 } 35 36 set mode(value) { 37 this._setMode(value, true); 38 } 39 40 /** */ 41 prepare() { 42 this._updateMode(); 43 } 44 45 // Private 46 47 /** */ 48 _updateMode() { 49 let mode = null; 50 try { 51 mode = sessionStorage.getItem('mode'); 52 } catch (e) { 53 // Browsers can throw a SecurityError when cookie blocking is enabled. 54 } 55 this._setMode(this._normalizeMode(mode), false); 56 } 57 58 /** 59 * @param {import('display').SearchMode} mode 60 * @param {boolean} save 61 */ 62 _setMode(mode, save) { 63 if (mode === this._mode) { return; } 64 if (save) { 65 try { 66 if (mode === null) { 67 sessionStorage.removeItem('mode'); 68 } else { 69 sessionStorage.setItem('mode', mode); 70 } 71 } catch (e) { 72 // Browsers can throw a SecurityError when cookie blocking is enabled. 73 } 74 } 75 this._mode = mode; 76 document.documentElement.dataset.searchMode = (mode !== null ? mode : ''); 77 this.trigger('modeChange', {mode}); 78 } 79 80 /** 81 * @param {?string} mode 82 * @returns {import('display').SearchMode} 83 */ 84 _normalizeMode(mode) { 85 switch (mode) { 86 case 'popup': 87 case 'action-popup': 88 return mode; 89 default: 90 return null; 91 } 92 } 93}