From cc1bafd88b6226765324fb759d051a1520f8d500 Mon Sep 17 00:00:00 2001 From: bordun Date: Wed, 30 Oct 2019 11:39:19 +0100 Subject: [PATCH] script: add httpGet function for local requests --- CHANGELOG.md | 2 ++ src/client/app/widgets/inputs/script.js | 25 ++++++++++++++++++++++--- src/client/app/widgets/plots/frame.js | 5 ++--- src/client/app/widgets/utils.js | 8 +++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be56b0ee..82a3d210 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - bug fixes - editor: checkbox position in multiline fields - patchbay: widget not responding to osc after being edited +- widgets + - script: added `httpGet` function for simple local http requests ## 0.48.1 diff --git a/src/client/app/widgets/inputs/script.js b/src/client/app/widgets/inputs/script.js index 720c306c..8a97746f 100644 --- a/src/client/app/widgets/inputs/script.js +++ b/src/client/app/widgets/inputs/script.js @@ -2,7 +2,8 @@ var Widget = require('../common/widget'), widgetManager = require('../../managers/widgets'), {icon} = require('../../ui/utils'), html = require('nanohtml'), - raw = require('nanohtml/raw') + raw = require('nanohtml/raw'), + {urlParser} = require('../utils') class Script extends Widget { @@ -31,6 +32,7 @@ class Script extends Widget { '- `set(id, value)`: function for setting a widget\'s value', '- `get(id)`: function for getting a widget\'s value (dynamic equivalent of @{id})', '- `getProp(id, property)`: function for getting a widget\'s property value ((dynamic equivalent of @{id.property})', + '- `httpGet(url, callback)`: function for making http requests (asynchronous "GET" request and local urls only)', '', '* Note: `value` or `linkId` properties can be used to receive other widgets\' values. The `value` property must actually change to trigger the execution, where linked widgets via `linkId` can submit the same value over and over and trigger the execution', ]} @@ -120,7 +122,23 @@ class Script extends Widget { send: options.send ? this.scriptSend.bind(this) : ()=>{}, set: (id, value)=>{Script.scriptSet(id, value, options)}, get: (id)=>{return Script.scriptGet(id)}, - getProp: (id, prop)=>{return Script.scriptGetProp(id, prop)} + getProp: (id, prop)=>{return Script.scriptGetProp(id, prop)}, + httpGet: (url, callback)=>{ + var parser = urlParser(url), + err = (e)=>{console.error(e)} + + if (!parser.isLocal()) return err('httpGet error (' + url + '): non-local url') + + var xhr = new XMLHttpRequest() + xhr.open("GET", url, true) + xhr.onload = (e)=>{ + if (xhr.readyState === 4 && xhr.status === 200 && callback) { + callback(xhr.responseText) + } + } + xhr.onerror = (e)=>err('httpGet error (' + url + '): ' + xhr.statusText) + xhr.send(null) + } } var condition = this.resolveProp('condition', undefined, false, false, false, {value: v}) @@ -149,7 +167,8 @@ Script.parsersContexts.script = { send: ()=>{}, set: ()=>{}, get: ()=>{}, - getProp: ()=>{} + getProp: ()=>{}, + httpGet: ()=>{} } Script.dynamicProps = Script.prototype.constructor.dynamicProps.concat( diff --git a/src/client/app/widgets/plots/frame.js b/src/client/app/widgets/plots/frame.js index 7d5903fb..d7aa378c 100644 --- a/src/client/app/widgets/plots/frame.js +++ b/src/client/app/widgets/plots/frame.js @@ -1,8 +1,7 @@ var Widget = require('../common/widget'), html = require('nanohtml'), {urlParser} = require('../utils'), - locales = require('../../locales'), - localUrlRe = /(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)/ + locales = require('../../locales') class Frame extends Widget { @@ -54,7 +53,7 @@ class Frame extends Widget { var parser = urlParser(this.value) - if (this.value && parser.hostname.match(localUrlRe)) { + if (this.value && parser.isLocal()) { this.frame.setAttribute('src', this.value) this.widget.classList.remove('error') if (this.errorTextMounted) { diff --git a/src/client/app/widgets/utils.js b/src/client/app/widgets/utils.js index ae4a48c1..b77203b6 100644 --- a/src/client/app/widgets/utils.js +++ b/src/client/app/widgets/utils.js @@ -85,7 +85,13 @@ module.exports = { urlParser: (()=>{ - var parser = document.createElement('a') + var parser = document.createElement('a'), + localUrlRe = /(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)/ + + parser.isLocal = ()=>{ + return parser.hostname.match(localUrlRe) + } + return (url)=>{ parser.href = url return parser