aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-24 14:01:21 -0400
committerGitHub <noreply@github.com>2020-05-24 14:01:21 -0400
commit6dd6af05e1ed3e0da4091af073c38e1d8ec0268d (patch)
tree7262add632fac645ca0481abbb63efb3b8291bb8 /ext/bg/js
parent3c4c82dcfc66a1b24a3df3d4b15283235c72cf66 (diff)
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
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/anki-note-builder.js2
-rw-r--r--ext/bg/js/backend.js22
-rw-r--r--ext/bg/js/background-main.js7
-rw-r--r--ext/bg/js/database.js2
4 files changed, 18 insertions, 15 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index 76199db7..31e67394 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -155,7 +155,7 @@ class AnkiNoteBuilder {
}
static arrayBufferToBase64(arrayBuffer) {
- return window.btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
+ return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}
static stringReplaceAsync(str, regex, replacer) {
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 90895737..80b00d5f 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -65,12 +65,14 @@ class Backend {
renderTemplate: this._renderTemplate.bind(this)
});
- this.optionsContext = {
- depth: 0,
- url: window.location.href
- };
+ const url = (typeof window === 'object' && window !== null ? window.location.href : '');
+ this.optionsContext = {depth: 0, url};
- this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target');
+ this.clipboardPasteTarget = (
+ typeof document === 'object' && document !== null ?
+ document.querySelector('#clipboard-paste-target') :
+ null
+ );
this.popupWindow = null;
@@ -704,6 +706,9 @@ class Backend {
return await navigator.clipboard.readText();
} else {
const clipboardPasteTarget = this.clipboardPasteTarget;
+ if (clipboardPasteTarget === null) {
+ throw new Error('Reading the clipboard is not supported in this context');
+ }
clipboardPasteTarget.value = '';
clipboardPasteTarget.focus();
document.execCommand('paste');
@@ -1005,13 +1010,8 @@ class Backend {
}
async _onCommandToggle() {
- const optionsContext = {
- depth: 0,
- url: window.location.href
- };
const source = 'popup';
-
- const options = this.getOptions(optionsContext);
+ const options = this.getOptions(this.optionsContext);
options.general.enable = !options.general.enable;
await this._onApiOptionsSave({source});
}
diff --git a/ext/bg/js/background-main.js b/ext/bg/js/background-main.js
index 24117f4e..345b4a77 100644
--- a/ext/bg/js/background-main.js
+++ b/ext/bg/js/background-main.js
@@ -20,6 +20,9 @@
*/
(async () => {
- window.yomichanBackend = new Backend();
- await window.yomichanBackend.prepare();
+ const backend = new Backend();
+ if (typeof window === 'object' && window !== null) {
+ window.yomichanBackend = backend;
+ }
+ await backend.prepare();
})();
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index 930cd0d0..65e267ab 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -596,7 +596,7 @@ class Database {
static _open(name, version, onUpgradeNeeded) {
return new Promise((resolve, reject) => {
- const request = window.indexedDB.open(name, version * 10);
+ const request = indexedDB.open(name, version * 10);
request.onupgradeneeded = (event) => {
try {