From d5d0c5bc8cf375eb8715c5f0aeedf8e2e0df8e52 Mon Sep 17 00:00:00 2001 From: Jon Nordby Date: Mon, 22 Aug 2016 14:46:41 +0800 Subject: [PATCH] runner: Use HTTP request to get input JSON Going through HTML/DOM for the JSON was a bad idea, and there never was a good reason for it in the first place. --- src/runner.coffee | 48 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/runner.coffee b/src/runner.coffee index 8bd6451..d193c63 100644 --- a/src/runner.coffee +++ b/src/runner.coffee @@ -22,7 +22,7 @@ htmlEscape = (html) -> .replace(/>/g, '>') .replace(/"/g, '"') -generateHtml = (filter, page, options) -> +generateHtml = (filter, options) -> library = """ window.jsJobEvent = function(id, payload) { @@ -45,6 +45,19 @@ generateHtml = (filter, page, options) -> } return obj; }; + var getData = function(callback) { + var xhr = new XMLHttpRequest(); + xhr.open('GET', window.location.href+'/data', false); + xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); + xhr.onreadystatechange = function () { + if (xhr.readyState === 4) { + var json = xhr.responseText; + var data = JSON.parse(json); + return callback(null, data); + } + }; + xhr.send(); + }; var sendResponse = function(err, solution, details) { var xhr = new XMLHttpRequest(); xhr.open('POST', window.location.href, true); @@ -63,20 +76,17 @@ generateHtml = (filter, page, options) -> }; var main = function() { console.log('poly: main start'); - var dataElement = document.getElementById("poly-input-data"); - var json = dataElement.innerHTML.substring("".length); - var data = JSON.parse(json); - console.log('poly: starting solving'); - window.jsJobRun(data.page, data.options, cb); - console.log('poly: started'); + + getData(function(err, data) { + console.log('poly: starting solving'); + window.jsJobRun(data.input, data.options, cb); + console.log('poly: started'); + }); }; window.onload = main; // main(); """ - payload = { page: page, options: options } - json = JSON.stringify payload, null, 4 - scriptTags = ("" for s in options.scripts).join("\n") body = """ @@ -85,7 +95,6 @@ generateHtml = (filter, page, options) -> #{scriptTags} - @@ -186,11 +195,25 @@ class Runner jobId = paths[2] if paths[3] == 'event' return @handleEventRequest jobId, request, response + else if paths[3] == 'data' + return @handleDataRequest jobId, request, response else return response.end() else return response.end() + handleDataRequest: (jobId, request, response) -> + console.log "#{request.method} #{jobId}" if @options.verbose + job = @jobs[jobId] + if not job + # multiple callbacks for same id, or wrong id + debug 'could not find solve job', jobId + return + + response.writeHead 200, {"Content-Type": "application/json; charset=utf-8"} + body = JSON.stringify { input: job.page, options: job.options } + response.end body + handleSolveRequest: (jobId, request, response) -> console.log "#{request.method} #{jobId}" if @options.verbose job = @jobs[jobId] @@ -200,9 +223,8 @@ class Runner return if request.method == 'GET' - # FIXME: make only for GET response.writeHead 200, {"Content-Type": "text/html; charset=utf-8"} - body = generateHtml job.filter, job.page, job.options + body = generateHtml job.filter, job.options response.end body else if request.method == 'POST' data = ""