From 607e6976596fc208cb5bc79ffc2a4bf93ac51a87 Mon Sep 17 00:00:00 2001 From: lehtint6 <teemu.t.lehtinen@aalto.fi> Date: Sat, 28 Mar 2020 15:29:37 +0200 Subject: [PATCH] Catch some errors for display, Add config options --- static/webdev-editor.js | 27 +++++++++++++++++++++------ static/webdev-execute.js | 3 +++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/static/webdev-editor.js b/static/webdev-editor.js index ca9ba49..4668155 100644 --- a/static/webdev-editor.js +++ b/static/webdev-editor.js @@ -71,10 +71,9 @@ ACOSWebdev.prototype.extendGrade = function (eventOrMutations, cb) { }, 0); } }; - this.editorExecute(); - setTimeout(function () { + this.editorExecute(function () { cb(self.config.points(self.$element, self.config, accessor)); - }, 1000); + }); }; ACOSWebdev.prototype.extendProtocolFeedback = function (feedback) { @@ -83,7 +82,7 @@ ACOSWebdev.prototype.extendProtocolFeedback = function (feedback) { return '<pre><code>' + this.editor.getValue() + '</code></pre><div>' + $out.html() + '</div>'; }; -ACOSWebdev.prototype.editorExecute = function () { +ACOSWebdev.prototype.editorExecute = function (cb) { var $iframe = $('<iframe src="about:blank"></iframe>'); this.$editorOutput.empty().append($iframe); $iframe.get(0).contentWindow.contents = '<!DOCTYPE html>\n' @@ -92,10 +91,26 @@ ACOSWebdev.prototype.editorExecute = function () { + '<link href="/static/webdev-editor/webdev-editor.css" rel="stylesheet">\n' + '<script src="/static/webdev-editor/webdev-execute.js"></script>\n' + '</head>\n<body class="execute">\n' + + (this.config.preExecuteScript ? ('<script src="' + this.config.preExecuteScript + '"></script>\n') : '') + (this.config.preExecuteHtml || '') - + '<script>' + (this.config.preExecuteJs || '') + + '<script>' + + 'try {\n' + + (this.config.preExecuteJs || '') + this.editor.getValue() - + (this.config.postExecuteJs || '') + '</script>\n' + + (this.config.postExecuteJs || '') + + '} catch (error) {\n' + + ' display.err(error.message);\n' + + ' throw error;\n' + + '}\n' + + 'window.parent.postMessage({state: "done"}, "*");\n' + + '</script>\n' + + (this.config.postExecuteHtml || '') + + (this.config.postExecuteScript ? ('<script src="' + this.config.postExecuteScript + '"></script>\n') : '') + '</body>\n</html>\n'; + window.addEventListener('message', function (event) { + if (event.data.state == 'done') { + cb(); + } + }); $iframe.attr('src', 'javascript:window["contents"]'); }; diff --git a/static/webdev-execute.js b/static/webdev-execute.js index 51307aa..51e8e1b 100644 --- a/static/webdev-execute.js +++ b/static/webdev-execute.js @@ -28,6 +28,9 @@ var display = (function () { }, res: function (html, args) { addLine(html, 'res', args); + }, + err: function (html) { + addLine(html, 'error text-danger'); } }; })(); -- GitLab