diff --git a/.eslintrc.json b/.eslintrc.json index 3ad61648..15bb96ea 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -193,6 +193,7 @@ "ext/js/data/json-schema.js", "ext/js/data/options-util.js", "ext/js/data/permissions-util.js", + "ext/js/data/sandbox/string-util.js", "ext/js/dom/simple-dom-parser.js", "ext/js/general/cache-map.js", "ext/js/general/object-property-accessor.js", diff --git a/ext/background.html b/ext/background.html index 57caa671..6259e519 100644 --- a/ext/background.html +++ b/ext/background.html @@ -36,6 +36,7 @@ + diff --git a/ext/info.html b/ext/info.html index 5872dda4..74d669c4 100644 --- a/ext/info.html +++ b/ext/info.html @@ -68,6 +68,7 @@ + diff --git a/ext/js/data/sandbox/string-util.js b/ext/js/data/sandbox/string-util.js new file mode 100644 index 00000000..37e021c9 --- /dev/null +++ b/ext/js/data/sandbox/string-util.js @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2021 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 . + */ + +/** + * Class containing generic string utility functions. + */ +class StringUtil { + /** + * Decodes the contents of an ArrayBuffer using UTF8. + * @param arrayBuffer The input ArrayBuffer. + * @returns A UTF8-decoded string. + */ + static arrayBufferUtf8Decode(arrayBuffer) { + try { + return new TextDecoder('utf-8').decode(arrayBuffer); + } catch (e) { + return decodeURIComponent(escape(this.arrayBufferToBinaryString(arrayBuffer))); + } + } + + /** + * Converts the contents of an ArrayBuffer to a base64 string. + * @param arrayBuffer The input ArrayBuffer. + * @returns A base64 string representing the binary content. + */ + static arrayBufferToBase64(arrayBuffer) { + return btoa(this.arrayBufferToBinaryString(arrayBuffer)); + } + + /** + * Converts the contents of an ArrayBuffer to a binary string. + * @param arrayBuffer The input ArrayBuffer. + * @returns A string representing the binary content. + */ + static arrayBufferToBinaryString(arrayBuffer) { + const bytes = new Uint8Array(arrayBuffer); + try { + return String.fromCharCode(...bytes); + } catch (e) { + let binary = ''; + for (let i = 0, ii = bytes.byteLength; i < ii; ++i) { + binary += String.fromCharCode(bytes[i]); + } + return binary; + } + } +} diff --git a/ext/js/media/audio-downloader.js b/ext/js/media/audio-downloader.js index e3b507b4..4bc4cf1b 100644 --- a/ext/js/media/audio-downloader.js +++ b/ext/js/media/audio-downloader.js @@ -19,6 +19,7 @@ * JsonSchema * NativeSimpleDOMParser * SimpleDOMParser + * StringUtil */ class AudioDownloader { @@ -257,7 +258,7 @@ class AudioDownloader { throw new Error('Could not retrieve audio'); } - const data = this._arrayBufferToBase64(arrayBuffer); + const data = StringUtil.arrayBufferToBase64(arrayBuffer); const contentType = response.headers.get('Content-Type'); return {data, contentType}; } @@ -288,10 +289,6 @@ class AudioDownloader { return digest; } - _arrayBufferToBase64(arrayBuffer) { - return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer))); - } - _createSimpleDOMParser(content) { if (typeof NativeSimpleDOMParser !== 'undefined' && NativeSimpleDOMParser.isSupported()) { return new NativeSimpleDOMParser(content); diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index 02ad368c..cf1a6b0c 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -18,6 +18,7 @@ /* global * DictionaryController * OptionsUtil + * StringUtil */ class BackupController { @@ -317,17 +318,8 @@ class BackupController { return warnings; } - _utf8Decode(arrayBuffer) { - try { - return new TextDecoder('utf-8').decode(arrayBuffer); - } catch (e) { - const binaryString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)); - return decodeURIComponent(escape(binaryString)); - } - } - async _importSettingsFile(file) { - const dataString = this._utf8Decode(await this._readFileArrayBuffer(file)); + const dataString = StringUtil.arrayBufferUtf8Decode(await this._readFileArrayBuffer(file)); const data = JSON.parse(dataString); // Type check diff --git a/ext/settings.html b/ext/settings.html index cd2376d6..3260810c 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -3438,6 +3438,7 @@ + diff --git a/ext/sw.js b/ext/sw.js index 6394260e..6443c57f 100644 --- a/ext/sw.js +++ b/ext/sw.js @@ -33,6 +33,7 @@ self.importScripts( '/js/data/json-schema.js', '/js/data/options-util.js', '/js/data/permissions-util.js', + '/js/data/sandbox/string-util.js', '/js/dom/simple-dom-parser.js', '/js/general/cache-map.js', '/js/general/object-property-accessor.js',