aboutsummaryrefslogtreecommitdiff
path: root/ext/js/pages/settings/permissions-origin-controller.js
blob: a4271f929effdc9e166370281e65661521c5ef48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2023  Yomitan Authors
 * Copyright (C) 2021-2022  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import {EventListenerCollection} from '../../core.js';

export class PermissionsOriginController {
    /**
     * @param {import('./settings-controller.js').SettingsController} settingsController
     */
    constructor(settingsController) {
        /** @type {import('./settings-controller.js').SettingsController} */
        this._settingsController = settingsController;
        /** @type {?HTMLElement} */
        this._originContainer = null;
        /** @type {?HTMLElement} */
        this._originEmpty = null;
        /** @type {?NodeListOf<HTMLInputElement>} */
        this._originToggleNodes = null;
        /** @type {?HTMLInputElement} */
        this._addOriginInput = null;
        /** @type {?HTMLElement} */
        this._errorContainer = null;
        /** @type {ChildNode[]} */
        this._originContainerChildren = [];
        /** @type {EventListenerCollection} */
        this._eventListeners = new EventListenerCollection();
    }

    /** */
    async prepare() {
        this._originContainer = /** @type {HTMLElement} */ (document.querySelector('#permissions-origin-list'));
        this._originEmpty = /** @type {HTMLElement} */ (document.querySelector('#permissions-origin-list-empty'));
        this._originToggleNodes = /** @type {NodeListOf<HTMLInputElement>} */ (document.querySelectorAll('.permissions-origin-toggle'));
        this._addOriginInput = /** @type {HTMLInputElement} */ (document.querySelector('#permissions-origin-new-input'));
        this._errorContainer = /** @type {HTMLElement} */ (document.querySelector('#permissions-origin-list-error'));
        const addButton = /** @type {HTMLButtonElement} */ (document.querySelector('#permissions-origin-add'));

        for (const node of this._originToggleNodes) {
            node.addEventListener('change', this._onOriginToggleChange.bind(this), false);
        }
        addButton.addEventListener('click', this._onAddButtonClick.bind(this), false);

        this._settingsController.on('permissionsChanged', this._onPermissionsChanged.bind(this));
        await this._updatePermissions();
    }

    // Private

    /**
     * @param {import('settings-controller').PermissionsChangedEvent} details
     */
    _onPermissionsChanged({permissions}) {
        this._eventListeners.removeAllEventListeners();
        for (const node of this._originContainerChildren) {
            if (node.parentNode === null) { continue; }
            node.parentNode.removeChild(node);
        }
        this._originContainerChildren = [];

        /** @type {Set<string>} */
        const originsSet = new Set(permissions.origins);
        for (const node of /** @type {NodeListOf<HTMLInputElement>} */ (this._originToggleNodes)) {
            const {origin} = node.dataset;
            node.checked = typeof origin === 'string' && originsSet.has(origin);
        }

        let any = false;
        const excludeOrigins = new Set([
            '<all_urls>'
        ]);
        const fragment = document.createDocumentFragment();
        for (const origin of originsSet) {
            if (excludeOrigins.has(origin)) { continue; }
            const node = this._settingsController.instantiateTemplateFragment('permissions-origin');
            const input = /** @type {HTMLInputElement} */ (node.querySelector('.permissions-origin-input'));
            const menuButton = /** @type {HTMLElement} */ (node.querySelector('.permissions-origin-button'));
            input.value = origin;
            this._eventListeners.addEventListener(menuButton, 'menuClose', this._onOriginMenuClose.bind(this, origin), false);
            this._originContainerChildren.push(...node.childNodes);
            fragment.appendChild(node);
            any = true;
        }
        const container = /** @type {HTMLElement} */ (this._originContainer);
        container.insertBefore(fragment, container.firstChild);
        /** @type {HTMLElement} */ (this._originEmpty).hidden = any;

        /** @type {HTMLElement} */ (this._errorContainer).hidden = true;
    }

    /**
     * @param {Event} e
     */
    _onOriginToggleChange(e) {
        const node = /** @type {HTMLInputElement} */ (e.currentTarget);
        const value = node.checked;
        node.checked = !value;

        const {origin} = node.dataset;
        if (typeof origin !== 'string') { return; }
        this._setOriginPermissionEnabled(origin, value);
    }

    /**
     * @param {string} origin
     */
    _onOriginMenuClose(origin) {
        this._setOriginPermissionEnabled(origin, false);
    }

    /** */
    _onAddButtonClick() {
        this._addOrigin();
    }

    /** */
    async _addOrigin() {
        const input = /** @type {HTMLInputElement} */ (this._addOriginInput);
        const origin = input.value;
        const added = await this._setOriginPermissionEnabled(origin, true);
        if (added) {
            input.value = '';
        }
    }

    /** */
    async _updatePermissions() {
        const permissions = await this._settingsController.permissionsUtil.getAllPermissions();
        this._onPermissionsChanged({permissions});
    }

    /**
     * @param {string} origin
     * @param {boolean} enabled
     * @returns {Promise<boolean>}
     */
    async _setOriginPermissionEnabled(origin, enabled) {
        let added = false;
        try {
            added = await this._settingsController.permissionsUtil.setPermissionsGranted({origins: [origin]}, enabled);
        } catch (e) {
            const errorContainer = /** @type {HTMLElement} */ (this._errorContainer);
            errorContainer.hidden = false;
            errorContainer.textContent = e instanceof Error ? e.message : `${e}`;
        }
        if (!added) { return false; }
        await this._updatePermissions();
        return true;
    }
}