From 7a59ca29262f6cb5b0dd2d87958b255451f92edc Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 6 Sep 2020 14:39:18 -0400 Subject: [PATCH] Update the functions for upgrading anki templates to be more reusable (#779) --- ext/bg/js/options.js | 80 +++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 1cabf2cf..fc2403e1 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -345,6 +345,47 @@ class OptionsUtil { // Private + static async _addFieldTemplatesToOptions(options, additionSourceUrl) { + let addition = null; + for (const {options: profileOptions} of options.profiles) { + const fieldTemplates = profileOptions.anki.fieldTemplates; + if (fieldTemplates !== null) { + if (addition === null) { + addition = await this._readFile(additionSourceUrl); + } + profileOptions.anki.fieldTemplates = this._addFieldTemplatesBeforeEnd(fieldTemplates, addition); + } + } + } + + static async _addFieldTemplatesBeforeEnd(fieldTemplates, addition) { + const pattern = /[ \t]*\{\{~?>\s*\(\s*lookup\s*\.\s*"marker"\s*\)\s*~?\}\}/; + const newline = '\n'; + let replaced = false; + fieldTemplates = fieldTemplates.replace(pattern, (g0) => { + replaced = true; + return `${addition}${newline}${g0}`; + }); + if (!replaced) { + fieldTemplates += newline; + fieldTemplates += addition; + } + return fieldTemplates; + } + + static async _readFile(url) { + url = chrome.runtime.getURL(url); + const response = await fetch(url, { + method: 'GET', + mode: 'no-cors', + cache: 'default', + credentials: 'omit', + redirect: 'follow', + referrerPolicy: 'no-referrer' + }); + return await response.text(); + } + static _getStringHashCode(string) { let hashCode = 0; @@ -423,47 +464,10 @@ class OptionsUtil { static async _updateVersion3(options) { // Version 3 changes: // Pitch accent Anki field templates added. - let addition = null; - for (const {options: profileOptions} of options.profiles) { - const fieldTemplates = profileOptions.anki.fieldTemplates; - if (fieldTemplates !== null) { - if (addition === null) { - addition = await this._updateVersion3GetAnkiFieldTemplates(); - } - profileOptions.anki.fieldTemplates = this._addFieldTemplatesBeforeEnd(fieldTemplates, addition); - } - } + await this._addFieldTemplatesToOptions(options, '/bg/data/anki-field-templates-upgrade-v2.handlebars'); return options; } - static async _updateVersion3GetAnkiFieldTemplates() { - const url = chrome.runtime.getURL('/bg/data/anki-field-templates-upgrade-v2.handlebars'); - const response = await fetch(url, { - method: 'GET', - mode: 'no-cors', - cache: 'default', - credentials: 'omit', - redirect: 'follow', - referrerPolicy: 'no-referrer' - }); - return await response.text(); - } - - static async _addFieldTemplatesBeforeEnd(fieldTemplates, addition) { - const pattern = /[ \t]*\{\{~?>\s*\(\s*lookup\s*\.\s*"marker"\s*\)\s*~?\}\}/; - const newline = '\n'; - let replaced = false; - fieldTemplates = fieldTemplates.replace(pattern, (g0) => { - replaced = true; - return `${addition}${newline}${g0}`; - }); - if (!replaced) { - fieldTemplates += newline; - fieldTemplates += addition; - } - return fieldTemplates; - } - static _updateVersion4(options) { // Version 4 changes: // Options conditions converted to string representations.