Skip to content

Instantly share code, notes, and snippets.

@jsinai
Last active May 23, 2017 22:33
Show Gist options
  • Save jsinai/06b89fd27d3af66e497ccea180b2b9e2 to your computer and use it in GitHub Desktop.
Save jsinai/06b89fd27d3af66e497ccea180b2b9e2 to your computer and use it in GitHub Desktop.
Provides the ability to read and write values to the registry from Javascript. Use any Javascript http library such as fetch to get and post values.
' Example of registry entries to save and retrieve:
'{
' "mystring": "foo",
' "myinteger": 1,
' "myboolean": true
'}
' Example of retrieving values using curl on MacOs:
' curl http://172.16.1.96:8081/registry-settings?keys=mystring,myinteger
'
' Example of saving values with curl on MacOs:
' curl -H "Content-Type: application/json" -X POST -d '{ "mystring": "foo", "myinteger": 1, "myboolean": true }' http://172.16.1.96:8081/registry-settings
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Main()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
m.SetupFunctionHandlers = SetupFunctionHandlers
m.SetupFunctionHandlers()
m.registrySection = CreateObject("roRegistrySection", "myregsection")
m.msgPort = CreateObject("roMessagePort")
m.AddHttpHandlers()
m.EventLoop()
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SetupFunctionHandlers()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
m.EventLoop = EventLoop
m.RegGetBool = RegGetBool
m.RegGetInt = RegGetInt
m.RegGetString = RegGetString
m.RegSaveBool = RegSaveBool
m.RegSaveInt = RegSaveInt
m.RegSaveString = RegSaveString
m.RegGetValue = RegGetValue
m.RegSaveValue = RegSaveValue
m.SendHttpResponse = SendHttpResponse
m.AddHttpHandlers = AddHttpHandlers
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RegGetString(key$ As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
return m.registrySection.Read(key$+"_val")
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RegSaveString(key$ As String, value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If value = invalid Then Return
m.registrySection.Write(key$+"_val", value)
m.registrySection.Write(key$+"_type", "String")
m.registrySection.Flush()
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RegGetBool(key$ As String) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
value$ = m.registrySection.Read(key$+"_val")
ret = false
If value$ = "true" Then ret = true
Return ret
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RegSaveBool(key$ As String, value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If value = invalid Then Return
enabledStr$ = "false"
If value Then enabledStr$ = "true"
m.registrySection.Write(key$+"_val", enabledStr$)
m.registrySection.Write(key$+"_type", "Boolean")
m.registrySection.Flush()
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RegGetInt(key$ As String) As Integer
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Return Int(Val(m.registrySection.Read(key$+"_val")))
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RegSaveInt(key$ As String, value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If value = invalid Then Return
m.registrySection.Write(key$+"_val", str(value).trim())
m.registrySection.Write(key$+"_type", "Integer")
m.registrySection.Flush()
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RegGetValue(key$ As String)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
regType$ = m.registrySection.Read(key$+"_type")
If regType$ = "Integer" Then Return m.RegGetInt(key$)
If regType$ = "Boolean" Then Return m.RegGetBool(key$)
If regType$ = "String" Then Return m.RegGetString(key$)
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub RegSaveValue(key$ As String, value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Type(value) = "Integer" Then m.RegSaveInt(key$, value)
If Type(value) = "Boolean" Then m.RegSaveBool(key$, value)
If Type(value) = "String" Then m.RegSaveString(key$, value)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub AddHttpHandlers()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Local server runs on 8081; change if necessary
m.localServer = CreateObject("roHttpServer", { port: 8081 })
m.localServer.SetPort(m.msgPort)
getRegistrySettingsAA = { __invoke: GetRegistrySettings, mVar: m }
m.localServer.AddGetFromEvent({ url_path: "/registry-settings", user_data: getRegistrySettingsAA, passwords: invalid })
saveRegistrySettingsAA = { __invoke: SaveRegistrySettings, mVar: m }
m.localServer.AddPostToString({ url_path: "/registry-settings", user_data: saveRegistrySettingsAA, passwords: invalid })
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetRegistrySettings(userData as Object, e as Object)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
mv = userData.mVar
retAA = {}
keys = e.GetRequestParam("keys")
r = CreateObject("roRegex", ",", "i")
args = r.Split(keys)
If args = invalid Then
mv.SendHttpResponse(e, "Bad request", 400)
return
End If
For Each arg in args
retAA[arg] = mv.RegGetValue(arg)
Next
mv.SendHttpResponse(e, retAA, 200)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SaveRegistrySettings(userData as Object, e as Object)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
mv = userData.mVar
jsonStr$ = e.GetRequestBodyString()
If jsonStr$ = invalid Then
mv.SendHttpResponse(e, "Bad request", 400)
return
End If
args=ParseJson(jsonStr$)
If args = invalid Then
mv.SendHttpResponse(e, "Bad JSON", 400)
return
End If
For Each arg in args
mv.RegSaveValue(arg, args[arg])
Next
mv.SendHttpResponse(e, "OK", 200)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub SendHttpResponse(e as Object, retAaOrStr, statusCode as Integer)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If type(retAaOrStr) = "roAssociativeArray" Then
ret$ = FormatJson(retAaOrStr)
Else
ret$ = retAaOrStr
End If
If m.debugMode$ = "1" Then
e.AddResponseHeader("Access-Control-Allow-Origin", "http://localhost:9090")
End If
e.SetResponseBodyString(ret$)
e.SendResponse(statusCode)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub EventLoop()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
While true
event = wait(0, m.msgPort)
userData = event.GetUserData()
If type(userData) = "roAssociativeArray" and type(userData.__invoke) = "roFunction" Then
userData.__invoke(userData, event)
End If
End while
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment