diff options
| author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-09-06 14:39:18 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-06 14:39:18 -0400 | 
| commit | 7a59ca29262f6cb5b0dd2d87958b255451f92edc (patch) | |
| tree | cae19fb988705edbcd5e9dd767c9ccbebc497df2 | |
| parent | 115afb63b90b8e6841061bbf8d67e82cab42958b (diff) | |
Update the functions for upgrading anki templates to be more reusable (#779)
| -rw-r--r-- | ext/bg/js/options.js | 80 | 
1 files 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. |