diff options
Diffstat (limited to 'ext/bg')
-rw-r--r-- | ext/bg/background.html | 2 | ||||
-rw-r--r-- | ext/bg/js/api.js | 22 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 9 | ||||
-rw-r--r-- | ext/bg/js/handlebars.js | 19 | ||||
-rw-r--r-- | ext/bg/js/settings.js | 2 | ||||
-rw-r--r-- | ext/bg/settings.html | 49 |
6 files changed, 47 insertions, 56 deletions
diff --git a/ext/bg/background.html b/ext/bg/background.html index 7f4a5098..97b20f46 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -24,7 +24,5 @@ <script src="/mixed/js/japanese.js"></script> <script src="/bg/js/backend.js"></script> - - <iframe src="/sb/sandbox.html" id="sandbox"></iframe> </body> </html> diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 6ab130a7..5c1aebb6 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -100,27 +100,9 @@ async function apiNoteView(noteId) { async function apiTemplateRender(template, data, dynamic) { if (dynamic) { - return new Promise((resolve, reject) => { - const sequence = utilBackend().sequenceNew(); - const handler = event => { - if (event.data.sequence === sequence) { - if (event.data.command === 'error') { - reject(event.data.result); - } else { - resolve(event.data.result); - } - - window.removeEventListener('message', handler); - } - }; - - window.addEventListener('message', handler); - - const sandbox = utilBackend().sandbox(); - sandbox.postMessage({template, data, sequence, command: 'render'}, '*'); - }); + return handlebarsRenderDynamic(template, data); } else { - return handlebarsRender(template, data); + return handlebarsRenderStatic(template, data); } } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 5061557b..7d68ed84 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -22,7 +22,6 @@ class Backend { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; - this.sequence = 0; } async prepare() { @@ -37,14 +36,6 @@ class Backend { } } - sequenceNew() { - return this.sequence++; - } - - sandbox() { - return document.getElementById('sandbox').contentWindow; - } - onOptionsUpdated(options) { this.options = utilIsolate(options); diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index e0804986..66d5fa2b 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -75,7 +75,7 @@ function handlebarsMultiLine(options) { return options.fn(this).split('\n').join('<br>'); } -function handlebarsRender(template, data) { +function handlebarsRegisterHelpers() { if (Handlebars.partials !== Handlebars.templates) { Handlebars.partials = Handlebars.templates; Handlebars.registerHelper('dumpObject', handlebarsDumpObject); @@ -84,6 +84,21 @@ function handlebarsRender(template, data) { Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks); Handlebars.registerHelper('multiLine', handlebarsMultiLine); } +} + +function handlebarsRenderStatic(name, data) { + handlebarsRegisterHelpers(); + return Handlebars.templates[name](data).trim(); +} + +function handlebarsRenderDynamic(template, data) { + handlebarsRegisterHelpers(); + + Handlebars.yomichan_cache = Handlebars.yomichan_cache || {}; + let instance = Handlebars.yomichan_cache[template]; + if (!instance) { + instance = Handlebars.yomichan_cache[template] = Handlebars.compile(template); + } - return Handlebars.templates[template](data).trim(); + return instance(data).trim(); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index d4e6ab17..55b469d0 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -218,7 +218,7 @@ async function dictionaryGroupsPopulate(options) { for (const dictRow of dictRowsSort(dictRows, options)) { const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0}; - const dictHtml = handlebarsRender('dictionary.html', { + const dictHtml = await apiTemplateRender('dictionary.html', { title: dictRow.title, version: dictRow.version, revision: dictRow.revision, diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 9aa9ea4d..0a5c205c 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -22,7 +22,7 @@ #field-templates { font-family: monospace; overflow-x: hidden; - white-space: nowrap; + white-space: pre; } </style> </head> @@ -195,11 +195,6 @@ <input type="text" id="interface-server" class="form-control"> </div> - <div class="form-group options-advanced"> - <label for="field-templates">Field templates (<a href="#" id="field-templates-reset">reset to defaults</a>)</label> - <textarea class="form-control" rows="10" id="field-templates"></textarea> - </div> - <div id="anki-format"> <p class="help-block"> Specify the information you would like included in your flashcards in the field editor below. @@ -252,28 +247,38 @@ </table> </div> </div> + + <div class="options-advanced"> + <p class="help-block"> + Fields are formatted using the <a href="http://handlebarsjs.com/">Handlebars.js</a> template rendering + engine. Advanced users can modify these templates for ultimate control of what information gets included in + their Anki cards. If you encounter problems with your changes you can always <a href="#" id="field-templates-reset">reset to default</a> + template settings. + </p> + <textarea class="form-control" rows="10" id="field-templates"></textarea> + </div> </div> </div> + </div> - <div> - <h3>Support Development</h3> - - <p class="help-block"> - Yomichan is provided to you <em>completely free</em> of charge. Unlike numerous other "free" services, you are not - shown ads, pestered with "offers", or have your browser usage information analyzed and sold to third parties. - </p> - <p class="help-block"> - If you find Yomichan useful, please consider making a small donation as a way to show your appreciation for the - countless hours that I have devoted to this extension. - </p> - <p> - <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4DBTN9A3CUAFN" target="_blank"><img src="/bg/img/paypal.gif" alt></a> - </p> - </div> + <div> + <h3>Support Development</h3> - <pre id="debug"></pre> + <p class="help-block"> + Yomichan is provided to you <em>completely free</em> of charge. Unlike numerous other "free" services, you are not + shown ads, pestered with "offers", or have your browser usage information analyzed and sold to third parties. + </p> + <p class="help-block"> + If you find Yomichan useful, please consider making a small donation as a way to show your appreciation for the + countless hours that I have devoted to this extension. + </p> + <p> + <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4DBTN9A3CUAFN" target="_blank"><img src="/bg/img/paypal.gif" alt></a> + </p> </div> + <pre id="debug"></pre> + <div class="pull-right"> <small><a href="https://foosoft.net/projects/yomichan/" target="_blank">Homepage</a> • <a href="legal.html">Legal</a></small> </div> |