diff options
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/language/dictionary-importer.js | 4 | ||||
-rw-r--r-- | ext/js/media/media-loader.js | 17 | ||||
-rw-r--r-- | ext/js/media/media-util.js | 16 |
3 files changed, 24 insertions, 13 deletions
diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index b4429315..888d19b0 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -400,7 +400,9 @@ class DictionaryImporter { eventListeners.removeAllEventListeners(); reject(new Error('Image failed to load')); }, false); - image.src = `data:${mediaType};base64,${content}`; + const blob = MediaUtil.createBlobFromBase64Content(content, mediaType); + const url = URL.createObjectURL(blob); + image.src = url; }); } } diff --git a/ext/js/media/media-loader.js b/ext/js/media/media-loader.js index d9d40a36..4ac733c5 100644 --- a/ext/js/media/media-loader.js +++ b/ext/js/media/media-loader.js @@ -15,6 +15,10 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +/* global + * MediaUtil + */ + class MediaLoader { constructor() { this._token = {}; @@ -82,22 +86,11 @@ class MediaLoader { const token = this._token; const data = (await yomichan.api.getMedia([{path, dictionaryName}]))[0]; if (token === this._token && data !== null) { - const contentArrayBuffer = this._base64ToArrayBuffer(data.content); - const blob = new Blob([contentArrayBuffer], {type: data.mediaType}); + const blob = MediaUtil.createBlobFromBase64Content(data.content, data.mediaType); const url = URL.createObjectURL(blob); cachedData.data = data; cachedData.url = url; } return cachedData; } - - _base64ToArrayBuffer(content) { - const binaryContent = window.atob(content); - const length = binaryContent.length; - const array = new Uint8Array(length); - for (let i = 0; i < length; ++i) { - array[i] = binaryContent.charCodeAt(i); - } - return array.buffer; - } } diff --git a/ext/js/media/media-util.js b/ext/js/media/media-util.js index 11172c5c..f783038a 100644 --- a/ext/js/media/media-util.js +++ b/ext/js/media/media-util.js @@ -129,4 +129,20 @@ class MediaUtil { return null; } } + + /** + * Creates a new `Blob` object from a base64 string of content. + * @param content The binary content string encoded in base64. + * @param mediaType The type of the media. + * @returns A new `Blob` object corresponding to the specified content. + */ + static createBlobFromBase64Content(content, mediaType) { + const binaryContent = atob(content); + const length = binaryContent.length; + const array = new Uint8Array(length); + for (let i = 0; i < length; ++i) { + array[i] = binaryContent.charCodeAt(i); + } + return new Blob([array.buffer], {type: mediaType}); + } } |