2020-12-12 17:35:56 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2020-2021 Yomichan Authors
|
2020-12-12 17:35:56 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global
|
2020-12-28 22:41:59 +00:00
|
|
|
* DocumentFocusController
|
2020-12-12 17:35:56 +00:00
|
|
|
* api
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function setupEnvironmentInfo() {
|
2020-12-18 22:00:34 +00:00
|
|
|
const {manifest_version: manifestVersion} = chrome.runtime.getManifest();
|
2020-12-12 17:35:56 +00:00
|
|
|
const {browser, platform} = await api.getEnvironmentInfo();
|
|
|
|
document.documentElement.dataset.browser = browser;
|
|
|
|
document.documentElement.dataset.os = platform.os;
|
2020-12-18 22:00:34 +00:00
|
|
|
document.documentElement.dataset.manifestVersion = `${manifestVersion}`;
|
2020-12-12 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function isAllowedIncognitoAccess() {
|
|
|
|
return await new Promise((resolve) => chrome.extension.isAllowedIncognitoAccess(resolve));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isAllowedFileSchemeAccess() {
|
|
|
|
return await new Promise((resolve) => chrome.extension.isAllowedFileSchemeAccess(resolve));
|
|
|
|
}
|
|
|
|
|
2021-01-30 20:05:08 +00:00
|
|
|
function hasPermissions(permissions) {
|
|
|
|
return new Promise((resolve) => chrome.permissions.contains({permissions}, (result) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
resolve(!e && result);
|
|
|
|
}));
|
2020-12-12 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 20:05:08 +00:00
|
|
|
function setPermissionsGranted(permissions, shouldHave) {
|
|
|
|
return (
|
2020-12-12 17:35:56 +00:00
|
|
|
shouldHave ?
|
2021-01-30 20:05:08 +00:00
|
|
|
new Promise((resolve, reject) => chrome.permissions.request({permissions}, (result) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
if (e) {
|
|
|
|
reject(new Error(e.message));
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
})) :
|
|
|
|
new Promise((resolve, reject) => chrome.permissions.remove({permissions}, (result) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
if (e) {
|
|
|
|
reject(new Error(e.message));
|
|
|
|
} else {
|
|
|
|
resolve(!result);
|
|
|
|
}
|
|
|
|
}))
|
2020-12-12 17:35:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-30 20:05:08 +00:00
|
|
|
function setupPermissionCheckbox(checkbox, permissions) {
|
|
|
|
checkbox.addEventListener('change', (e) => {
|
|
|
|
updatePermissionCheckbox(checkbox, permissions, e.currentTarget.checked);
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updatePermissionCheckbox(checkbox, permissions, value) {
|
|
|
|
checkbox.checked = !value;
|
|
|
|
const hasPermission = await setPermissionsGranted(permissions, value);
|
|
|
|
checkbox.checked = hasPermission;
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:35:56 +00:00
|
|
|
(async () => {
|
|
|
|
try {
|
2020-12-28 22:41:59 +00:00
|
|
|
const documentFocusController = new DocumentFocusController();
|
|
|
|
documentFocusController.prepare();
|
|
|
|
|
2021-01-31 01:44:54 +00:00
|
|
|
for (const node of document.querySelectorAll('.extension-id-example')) {
|
|
|
|
node.textContent = chrome.runtime.getURL('/');
|
|
|
|
}
|
2020-12-12 17:35:56 +00:00
|
|
|
|
|
|
|
api.forwardLogsToBackend();
|
|
|
|
await yomichan.prepare();
|
|
|
|
|
|
|
|
setupEnvironmentInfo();
|
|
|
|
|
|
|
|
const permissionsCheckboxes = [
|
|
|
|
document.querySelector('#permission-checkbox-clipboard-read'),
|
|
|
|
document.querySelector('#permission-checkbox-allow-in-private-windows'),
|
|
|
|
document.querySelector('#permission-checkbox-allow-file-url-access')
|
|
|
|
];
|
|
|
|
|
|
|
|
const permissions = await Promise.all([
|
|
|
|
hasPermissions(['clipboardRead']),
|
|
|
|
isAllowedIncognitoAccess(),
|
|
|
|
isAllowedFileSchemeAccess()
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let i = 0, ii = permissions.length; i < ii; ++i) {
|
|
|
|
permissionsCheckboxes[i].checked = permissions[i];
|
|
|
|
}
|
|
|
|
|
2021-01-30 20:05:08 +00:00
|
|
|
setupPermissionCheckbox(permissionsCheckboxes[0], ['clipboardRead']);
|
2020-12-12 17:35:56 +00:00
|
|
|
|
|
|
|
await promiseTimeout(100);
|
|
|
|
|
|
|
|
document.documentElement.dataset.loaded = 'true';
|
|
|
|
} catch (e) {
|
|
|
|
yomichan.logError(e);
|
|
|
|
}
|
|
|
|
})();
|