Update background global object usage (#556)
* Omit global window object for scripts used on the background page * Validate document exists before using * Remove dom.js from background.html
This commit is contained in:
parent
3c4c82dcfc
commit
6dd6af05e1
@ -20,7 +20,6 @@
|
|||||||
<script src="/mixed/lib/wanakana.min.js"></script>
|
<script src="/mixed/lib/wanakana.min.js"></script>
|
||||||
|
|
||||||
<script src="/mixed/js/core.js"></script>
|
<script src="/mixed/js/core.js"></script>
|
||||||
<script src="/mixed/js/dom.js"></script>
|
|
||||||
<script src="/mixed/js/environment.js"></script>
|
<script src="/mixed/js/environment.js"></script>
|
||||||
<script src="/mixed/js/japanese.js"></script>
|
<script src="/mixed/js/japanese.js"></script>
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ class AnkiNoteBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static arrayBufferToBase64(arrayBuffer) {
|
static arrayBufferToBase64(arrayBuffer) {
|
||||||
return window.btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static stringReplaceAsync(str, regex, replacer) {
|
static stringReplaceAsync(str, regex, replacer) {
|
||||||
|
@ -65,12 +65,14 @@ class Backend {
|
|||||||
renderTemplate: this._renderTemplate.bind(this)
|
renderTemplate: this._renderTemplate.bind(this)
|
||||||
});
|
});
|
||||||
|
|
||||||
this.optionsContext = {
|
const url = (typeof window === 'object' && window !== null ? window.location.href : '');
|
||||||
depth: 0,
|
this.optionsContext = {depth: 0, url};
|
||||||
url: window.location.href
|
|
||||||
};
|
|
||||||
|
|
||||||
this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target');
|
this.clipboardPasteTarget = (
|
||||||
|
typeof document === 'object' && document !== null ?
|
||||||
|
document.querySelector('#clipboard-paste-target') :
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
this.popupWindow = null;
|
this.popupWindow = null;
|
||||||
|
|
||||||
@ -704,6 +706,9 @@ class Backend {
|
|||||||
return await navigator.clipboard.readText();
|
return await navigator.clipboard.readText();
|
||||||
} else {
|
} else {
|
||||||
const clipboardPasteTarget = this.clipboardPasteTarget;
|
const clipboardPasteTarget = this.clipboardPasteTarget;
|
||||||
|
if (clipboardPasteTarget === null) {
|
||||||
|
throw new Error('Reading the clipboard is not supported in this context');
|
||||||
|
}
|
||||||
clipboardPasteTarget.value = '';
|
clipboardPasteTarget.value = '';
|
||||||
clipboardPasteTarget.focus();
|
clipboardPasteTarget.focus();
|
||||||
document.execCommand('paste');
|
document.execCommand('paste');
|
||||||
@ -1005,13 +1010,8 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _onCommandToggle() {
|
async _onCommandToggle() {
|
||||||
const optionsContext = {
|
|
||||||
depth: 0,
|
|
||||||
url: window.location.href
|
|
||||||
};
|
|
||||||
const source = 'popup';
|
const source = 'popup';
|
||||||
|
const options = this.getOptions(this.optionsContext);
|
||||||
const options = this.getOptions(optionsContext);
|
|
||||||
options.general.enable = !options.general.enable;
|
options.general.enable = !options.general.enable;
|
||||||
await this._onApiOptionsSave({source});
|
await this._onApiOptionsSave({source});
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
window.yomichanBackend = new Backend();
|
const backend = new Backend();
|
||||||
await window.yomichanBackend.prepare();
|
if (typeof window === 'object' && window !== null) {
|
||||||
|
window.yomichanBackend = backend;
|
||||||
|
}
|
||||||
|
await backend.prepare();
|
||||||
})();
|
})();
|
||||||
|
@ -596,7 +596,7 @@ class Database {
|
|||||||
|
|
||||||
static _open(name, version, onUpgradeNeeded) {
|
static _open(name, version, onUpgradeNeeded) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const request = window.indexedDB.open(name, version * 10);
|
const request = indexedDB.open(name, version * 10);
|
||||||
|
|
||||||
request.onupgradeneeded = (event) => {
|
request.onupgradeneeded = (event) => {
|
||||||
try {
|
try {
|
||||||
|
@ -177,7 +177,7 @@ function promiseTimeout(delay, resolveValue) {
|
|||||||
const complete = (callback, value) => {
|
const complete = (callback, value) => {
|
||||||
if (callback === null) { return; }
|
if (callback === null) { return; }
|
||||||
if (timer !== null) {
|
if (timer !== null) {
|
||||||
window.clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
promiseResolve = null;
|
promiseResolve = null;
|
||||||
@ -192,7 +192,7 @@ function promiseTimeout(delay, resolveValue) {
|
|||||||
promiseResolve = resolve2;
|
promiseResolve = resolve2;
|
||||||
promiseReject = reject2;
|
promiseReject = reject2;
|
||||||
});
|
});
|
||||||
timer = window.setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
timer = null;
|
timer = null;
|
||||||
resolve(resolveValue);
|
resolve(resolveValue);
|
||||||
}, delay);
|
}, delay);
|
||||||
@ -331,7 +331,7 @@ const yomichan = (() => {
|
|||||||
|
|
||||||
generateId(length) {
|
generateId(length) {
|
||||||
const array = new Uint8Array(length);
|
const array = new Uint8Array(length);
|
||||||
window.crypto.getRandomValues(array);
|
crypto.getRandomValues(array);
|
||||||
let id = '';
|
let id = '';
|
||||||
for (const value of array) {
|
for (const value of array) {
|
||||||
id += value.toString(16).padStart(2, '0');
|
id += value.toString(16).padStart(2, '0');
|
||||||
@ -364,7 +364,7 @@ const yomichan = (() => {
|
|||||||
const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
|
const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
|
||||||
let timeoutId = null;
|
let timeoutId = null;
|
||||||
if (timeout !== null) {
|
if (timeout !== null) {
|
||||||
timeoutId = window.setTimeout(() => {
|
timeoutId = setTimeout(() => {
|
||||||
timeoutId = null;
|
timeoutId = null;
|
||||||
eventHandler.removeListener(runtimeMessageCallback);
|
eventHandler.removeListener(runtimeMessageCallback);
|
||||||
reject(new Error(`Listener timed out in ${timeout} ms`));
|
reject(new Error(`Listener timed out in ${timeout} ms`));
|
||||||
@ -373,7 +373,7 @@ const yomichan = (() => {
|
|||||||
|
|
||||||
const cleanupResolve = (value) => {
|
const cleanupResolve = (value) => {
|
||||||
if (timeoutId !== null) {
|
if (timeoutId !== null) {
|
||||||
window.clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
timeoutId = null;
|
timeoutId = null;
|
||||||
}
|
}
|
||||||
eventHandler.removeListener(runtimeMessageCallback);
|
eventHandler.removeListener(runtimeMessageCallback);
|
||||||
@ -453,10 +453,12 @@ const yomichan = (() => {
|
|||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
|
_getUrl() {
|
||||||
|
return (typeof window === 'object' && window !== null ? window.location.href : '');
|
||||||
|
}
|
||||||
|
|
||||||
_getLogContext() {
|
_getLogContext() {
|
||||||
return {
|
return {url: this._getUrl()};
|
||||||
url: window.location.href
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessage({action, params}, sender, callback) {
|
_onMessage({action, params}, sender, callback) {
|
||||||
@ -469,7 +471,7 @@ const yomichan = (() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_onMessageGetUrl() {
|
_onMessageGetUrl() {
|
||||||
return {url: window.location.href};
|
return {url: this._getUrl()};
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessageOptionsUpdated({source}) {
|
_onMessageOptionsUpdated({source}) {
|
||||||
|
Loading…
Reference in New Issue
Block a user