script: add httpGet function for local requests

This commit is contained in:
bordun 2019-10-30 11:39:19 +01:00
parent e0c56e6bef
commit cc1bafd88b
4 changed files with 33 additions and 7 deletions

View file

@ -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

View file

@ -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(

View file

@ -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) {

View file

@ -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