Slow Glass Variable from Resources
We can also create variables from resources loaded over HTTP, e.g. from our own or from other web servers.
CORS Proxy
(cors \| proxy \|) {url}
If you are intending to load resources from a different server you may need to use a CORS proxy. Explanation of this is beyond the scope of this document but the address of the proxy can be set using the command above. (Also see Web Page Integration for an alternative method to set this using JavaScript)
Loading Resources
(load \| upload) {resource-type} \[from\] {url} \[named\] {resource-name}
The {resource-type} can be any of the following:
- image - see Sprite Creation
- audio - see Audio Management
- text - plain text organised by lines
- file - as above
- html - html files
- xml - xml files (including RSS)
- data - structured data file (key value pairs by line)
- array - as above
Creating Variables from Resources
Each of the resource types has a slightly different syntax for variable creation. In all the sections below the command get can be replace with the alternative parse.
From Text Files
get {variable-name} from {resource-name} line {line-number}
The named variable will be created with the contents of the 1-based line number of the file, or NONE if there is no line of that number. Note that empty lines create empty variables. (I might change this in the future as it is hard to detect empty variables in expressions, perhaps it should be set to EMPTY?)
From HTML Files
get {variable-name} from {resource-name} {item-number} {element-pattern}
The named variable is set to the inner HTML of the item-number element matching the element pattern. (The pattern is passed unchanged to the document.querySelectorAll JavaScript method - look at the description of this method for full details of the syntax). For example:
get para2 from htmlfile 2 p
Will put the contents of the second paragraph element (if it exists) into the variable $para2. If no match is found the variable is set to NONE.
From XML Files
get {variable-name} from {resource-name} {item-number} {pattern}
The named variable is set to the inner HTML of the item-number element matching the given pattern, as per above. For example if we have loaded an RSS new feed:
get news1 from rssfile 1 item > title
Will put the title of the first news item into the variable. If no match is found the variable is set to NONE.
From Data Files
get {variable-name} from {resource-name}
If the loaded resource was a data file then the named variable will be created as an array with the first word on each line being the used as the key and the rest of the line as the value.
Lines beginning with a ‘#’ character and empty lines will be ignored. (This is the same data format used by the make … from command) - see Mutliple Array Values.
Deleting Resources
A resource can simply be overwritten by using load with the same resource name. Large resources that are no longer needed can be removed with the command:
unload {resource-name}
TBD implement this!