diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/background.html | 2 | ||||
-rw-r--r-- | ext/bg/js/api.js | 17 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 9 | ||||
-rw-r--r-- | ext/sb/js/sandbox.js | 4 |
4 files changed, 29 insertions, 3 deletions
diff --git a/ext/bg/background.html b/ext/bg/background.html index 151d7e81..7f4a5098 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -25,6 +25,6 @@ <script src="/bg/js/backend.js"></script> - <iframe src="/sb/sandbox.html"></iframe> + <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 96147d95..aba1b722 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -100,6 +100,23 @@ async function apiTemplateRender(template, data) { return handlebarsRender(template, data); } +async function apiTemplateRenderDynamic(template, data) { + return new Promise((resolve, reject) => { + const sequence = utilBackend().sequenceNew(); + const handler = event => { + if (event.data.sequence === sequence) { + resolve(event.data.result); + window.removeEventListener('message', handler); + } + }; + + window.addEventListener('message', handler); + + const sandbox = utilBackend().sandbox(); + sandbox.postMessage({template, data, sequence, command: 'render'}, '*'); + }); +} + async function apiCommandExec(command) { const handlers = { search: () => { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6b3acaa9..9802ea7c 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -22,6 +22,7 @@ class Backend { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; + this.sequence = 0; } async prepare() { @@ -36,6 +37,14 @@ class Backend { } } + sequenceNew() { + return this.sequence++; + } + + sandbox() { + return document.getElementById('sandbox').contentWindow; + } + onOptionsUpdated(options) { this.options = utilIsolate(options); diff --git a/ext/sb/js/sandbox.js b/ext/sb/js/sandbox.js index 76b7b511..bf1d56c1 100644 --- a/ext/sb/js/sandbox.js +++ b/ext/sb/js/sandbox.js @@ -24,7 +24,7 @@ window.addEventListener('message', event => { if (event.data.command === 'render') { const template = Handlebars.compile(event.data.template || ''); - const result = template(event.data.context || {}); - event.source.postMessage({result, sequence: event.data.sequence}); + const result = template(event.data.data || {}); + event.source.postMessage({result, sequence: event.data.sequence}, '*'); } }); |