From 5f46006e8da1d51a66291f25a2bc75959ec81efd Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 9 Sep 2017 12:59:49 -0700 Subject: [PATCH] scrap sandbox --- ext/bg/background.html | 2 -- ext/bg/js/api.js | 22 ++--------------- ext/bg/js/backend.js | 9 ------- ext/bg/js/handlebars.js | 21 +++++++++++++--- ext/bg/js/settings.js | 2 +- ext/bg/settings.html | 53 ++++++++++++++++++++++------------------- ext/manifest.json | 4 +--- ext/sb/js/sandbox.js | 46 ----------------------------------- ext/sb/sandbox.html | 10 -------- 9 files changed, 51 insertions(+), 118 deletions(-) delete mode 100644 ext/sb/js/sandbox.js delete mode 100644 ext/sb/sandbox.html 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 @@ - - 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('
'); } -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); } - - return Handlebars.templates[template](data).trim(); +} + +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 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; } @@ -195,11 +195,6 @@ -
- - -
-

Specify the information you would like included in your flashcards in the field editor below. @@ -252,28 +247,38 @@

+ +
+

+ Fields are formatted using the Handlebars.js 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 reset to default + template settings. +

+ +
- -
-

Support Development

- -

- Yomichan is provided to you completely free 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. -

-

- 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. -

-

- -

-
- -

             
 
+            
+

Support Development

+ +

+ Yomichan is provided to you completely free 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. +

+

+ 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. +

+

+ +

+
+ +

+
             
HomepageLegal
diff --git a/ext/manifest.json b/ext/manifest.json index e95cc496..0da3283c 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -49,14 +49,12 @@ } }, "web_accessible_resources": ["fg/float.html"], + "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "applications": { "gecko": { "id": "yomichan-live@foosoft.net", "strict_min_version": "52.0", "update_url": "https://foosoft.net/projects/yomichan/dl/updates.json" } - }, - "sandbox": { - "pages": ["sb/sandbox.html"] } } diff --git a/ext/sb/js/sandbox.js b/ext/sb/js/sandbox.js deleted file mode 100644 index c3430afe..00000000 --- a/ext/sb/js/sandbox.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2017 Alex Yatskov - * Author: Alex Yatskov - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -window.addEventListener('message', event => { - if (event.data.command === 'render') { - window.yomichan_cache = window.yomichan_cache || {}; - - let template = window.yomichan_cache[event.data.template]; - if (!template) { - template = Handlebars.compile(event.data.template || ''); - window.yomichan_cache[event.data.template] = template; - } - - let result = null; - let command = null; - try { - command = 'render'; - result = template(event.data.data || {}); - } catch (e) { - command = 'error'; - result = e; - } - - event.source.postMessage({result, command, sequence: event.data.sequence}, '*'); - } -}); diff --git a/ext/sb/sandbox.html b/ext/sb/sandbox.html deleted file mode 100644 index b9d33cf6..00000000 --- a/ext/sb/sandbox.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - -