//////////////////////////////////
// within index.html: (this loads fine)
...
<script src="url_block.js"></script>
...
function initApi(interpreter, scope) {
// Add an API for the url block. See get_url_block.js
initInterpreterGetURL(interpreter, scope);
...
//////////////////////////////////
// get_url_block.js: (this loads fine, but crashes when run)
Blockly.defineBlocksWithJsonArray([{
"type": "url_get",
"message0": "Return a URL GET",
"output": "String",
"colour": 60,
"tooltip": "",
"helpUrl": ""
}]);
Blockly.JavaScript['url_get'] = function(block) {
// this is a url test site:
var url = "https://postman-echo.com/get?foo1=123";
var code = 'getURL(' + url + ');\n';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
function initInterpreterGetURL(interpreter, scope) {
Blockly.JavaScript.addReservedWords('getURL');
var wrapper = interpreter.createAsyncFunction(
function(href, callback) {
var req = new XMLHttpRequest();
req.open('GET', href, true);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
callback(req.responseText);
}
};
});
interpreter.setProperty(scope, 'getURL', wrapper);
}
--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/e54ec7d3-b0a0-475e-ab45-38d62eed80c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
var code = 'getURL(' + url + ');\n';
Oh, and I can see the syntax error. Your block is a value block, meaning it returns a value. But your generated code is a statement. Just trim off the semicolon and new line here:var code = 'getURL(' + url + ');\n';
On Sun, 2 Jun 2019 at 19:18, Neil Fraser <ro...@neil.fraser.name> wrote:
Acorn reporting a syntax error just means that the code you are attempting to execute with the JS Interpreter is syntactically malformed. Do a console.log(code) just before creating the Interpreter and find out what you are attempting to execute. Once you can see that, fixing this particular issue should be "really easy". :)
To unsubscribe from this group and stop receiving emails from it, send an email to blo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/e54ec7d3-b0a0-475e-ab45-38d62eed80c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--Neil Fraser
https://neil.fraser.name
--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.