added more lua snippets

This commit is contained in:
BroodjeAap 2023-01-14 15:29:28 +00:00
parent 04dbfdf86c
commit b62552d8cb
3 changed files with 142 additions and 24 deletions

View file

@ -626,13 +626,13 @@ function onTypeChange(node) {
}
var1Input_1.rows = 10;
var1Div.appendChild(var1Input_1);
// dev download link
// dev copy link
var devCopyA_1 = document.createElement('a');
var results = node == null ? [] : node.results;
var luaScript_1 = "inputs = {\"" + results.join('","') + "\"}\noutputs = {}\n\n " + var1Input_1.value;
devCopyA_1.setAttribute('href', '#');
devCopyA_1.classList.add("btn", "btn-primary", "btn-sm");
devCopyA_1.innerHTML = "Copy Script";
devCopyA_1.innerHTML = "Copy script with inputs";
devCopyA_1.onclick = function () {
if (navigator.clipboard) {
navigator.clipboard.writeText(luaScript_1);
@ -644,17 +644,28 @@ function onTypeChange(node) {
};
var1Div.appendChild(devCopyA_1);
var luaSnippets = new Map([
["http", "\nlocal http = require(\"http\")\nlocal client = http.client()\n\nlocal request = http.request(\"GET\", \"https://api.ipify.org\")\nlocal result, err = client:do_request(request)\nif err then\n error(err)\nend\nif not (result.code == 200) then\n error(\"code\")\nend\n\ntable.insert(outputs, result.body)\n "],
["strings", "\nlocal strings = require(\"strings\")\nfor i,input in pairs(inputs) do\n table.insert(outputs, strings.trim_space(input))\nend\n "],
["filter", "\nfor i,input in pairs(inputs) do\n number = tonumber(input)\n if number % 2 == 0 then\n table.insert(outputs, input)\n end\nend\n "],
["HTTP GET", "local http = require(\"http\")\nlocal client = http.client()\n\nlocal request = http.request(\"GET\", \"https://api.ipify.org\")\nlocal result, err = client:do_request(request)\nif err then\n table.insert(logs, err)\n error(err)\nend\nif not (result.code == 200) then\n table.insert(logs, err)\n error(err)\nend\n\ntable.insert(outputs, result.body)\n "],
["HTTP POST", "local http = require(\"http\")\nlocal client = http.client()\n\nlocal request = http.request(\"POST\", \"http://httpbin.org/post\", \"{}\")\nlocal result, err = client:do_request(request)\nif err then\n table.insert(logs, err)\n error(err)\nend\nif not (result.code == 200) then\n table.insert(logs, err)\n error(err)\nend\n\ntable.insert(outputs, result.body)\n "],
["HTTP Auth", "local http = require(\"http\")\nlocal client = http.client()\n\nlocal request = http.request(\"GET\", \"http://httpbin.org/basic-auth/gowatch/gowatch123\")\nrequest:set_basic_auth(\"gowatch\", \"gowatch123\")\nlocal result, err = client:do_request(request)\nif err then\n table.insert(logs, err)\n error(err)\nend\nif not (result.code == 200) then\n table.insert(logs, err)\n error(err)\nend\n\ntable.insert(outputs, result.body)\n "],
["HTTP Bearer", "local http = require(\"http\")\nlocal client = http.client()\n\nlocal request = http.request(\"GET\", \"http://httpbin.org/bearer\")\nlocal token = \"gowatch123\"\nrequest:header_set(\"Authorization\", \"Bearer \" .. token)\nlocal result, err = client:do_request(request)\nif err then\n table.insert(logs, err)\n error(err)\nend\nif not (result.code == 200) then\n table.insert(logs, err)\n error(err)\nend\n\ntable.insert(outputs, result.body)\n "],
["HTTP Proxy", "local http = require(\"http\")\nlocal client = http.client({ proxy = \"http://login:password@hostname.com\" })\n\nlocal request = http.request(\"GET\", \"https://api.ipify.org\")\nlocal result, err = client:do_request(request)\nif err then\n table.insert(logs, err)\n error(err)\nend\nif not (result.code == 200) then\n table.insert(logs, err)\n error(err)\nend\n\ntable.insert(outputs, result.body)\n "],
["HTTP Browserless", "local http = require(\"http\")\nlocal client = http.client()\n\n# note \" for keys/values\nlocal data = '{\"url\": \"https://api.ipify.org\"}'\nlocal request = http.request(\"POST\", \"http://browserless:3000/content\", data)\nrequest:header_set(\"Content-Type\", \"application/json\")\nlocal result, err = client:do_request(request)\nif err then\n table.insert(logs, err)\n error(err)\nend\nif not (result.code == 200) then\n table.insert(logs, \"Response != 200 - \" .. result.code)\nend\n\ntable.insert(outputs, result.body) \n "],
["XPath", "local xmlpath = require(\"xmlpath\")\n\nlocal query = \"//td[@class='price']\"\nlocal query, err = xmlpath.compile(query)\nif err then\n table.insert(logs, err)\n error(err)\nend\n\nfor i,input in pairs(inputs) do\n local node, err = xmlpath.load(input)\n if err then\n table.insert(logs, err)\n error(err)\n end\n local iter = query:iter(node)\n for k, v in pairs(iter) do\n table.insert(outputs, v:string())\n end\nend\n "],
["strings", "local strings = require(\"strings\")\nfor i,input in pairs(inputs) do\n table.insert(outputs, strings.trim_space(input))\nend\n "],
["filter", "for i,input in pairs(inputs) do\n number = tonumber(input)\n if number % 2 == 0 then\n table.insert(outputs, input)\n end\nend\n "],
]);
var snippetDiv = document.createElement("div");
snippetDiv.classList.add("d-flex", "flex-wrap");
var _loop_1 = function (name_1, snippet) {
var link = document.createElement('a');
link.setAttribute("href", "#");
link.classList.add("btn", "btn-secondary", "btn-sm");
link.classList.add("btn", "btn-outline-secondary", "btn-sm", "xs");
link.innerHTML = name_1;
link.onclick = function () { var1Input_1.value = snippet; };
var1Div.appendChild(link);
snippetDiv.appendChild(link);
var gap = document.createElement("div");
gap.classList.add("m-1", "xs");
snippetDiv.appendChild(gap);
};
try {
// add snippets
@ -670,6 +681,7 @@ function onTypeChange(node) {
}
finally { if (e_2) throw e_2.error; }
}
var1Div.appendChild(snippetDiv);
var var2Input = document.createElement("input");
var2Input.name = "var2";
var2Input.id = "var2Input";

View file

@ -612,13 +612,13 @@ function onTypeChange(node: DiagramNode | null = null){
var1Input.rows = 10;
var1Div.appendChild(var1Input);
// dev download link
// dev copy link
let devCopyA = document.createElement('a');
let results = node == null ? [] : node.results;
let luaScript = `inputs = {"${results.join('","')}"}\noutputs = {}\n\n ${var1Input.value}`;
devCopyA.setAttribute('href', '#')
devCopyA.classList.add("btn", "btn-primary", "btn-sm");
devCopyA.innerHTML = "Copy Script";
devCopyA.innerHTML = "Copy script with inputs";
devCopyA.onclick = function() {
if (navigator.clipboard){
navigator.clipboard.writeText(luaScript);
@ -630,29 +630,134 @@ function onTypeChange(node: DiagramNode | null = null){
var1Div.appendChild(devCopyA);
let luaSnippets: Map<string, string> = new Map([
["http", `
local http = require("http")
["HTTP GET", `local http = require("http")
local client = http.client()
local request = http.request("GET", "https://api.ipify.org")
local result, err = client:do_request(request)
if err then
table.insert(logs, err)
error(err)
end
if not (result.code == 200) then
error("code")
table.insert(logs, err)
error(err)
end
table.insert(outputs, result.body)
`],
["strings", `
local strings = require("strings")
["HTTP POST", `local http = require("http")
local client = http.client()
local request = http.request("POST", "http://httpbin.org/post", "{}")
local result, err = client:do_request(request)
if err then
table.insert(logs, err)
error(err)
end
if not (result.code == 200) then
table.insert(logs, err)
error(err)
end
table.insert(outputs, result.body)
`],
["HTTP Auth", `local http = require("http")
local client = http.client()
local request = http.request("GET", "http://httpbin.org/basic-auth/gowatch/gowatch123")
request:set_basic_auth("gowatch", "gowatch123")
local result, err = client:do_request(request)
if err then
table.insert(logs, err)
error(err)
end
if not (result.code == 200) then
table.insert(logs, err)
error(err)
end
table.insert(outputs, result.body)
`],
["HTTP Bearer", `local http = require("http")
local client = http.client()
local request = http.request("GET", "http://httpbin.org/bearer")
local token = "gowatch123"
request:header_set("Authorization", "Bearer " .. token)
local result, err = client:do_request(request)
if err then
table.insert(logs, err)
error(err)
end
if not (result.code == 200) then
table.insert(logs, err)
error(err)
end
table.insert(outputs, result.body)
`],
["HTTP Proxy", `local http = require("http")
local client = http.client({ proxy = "http://login:password@hostname.com" })
local request = http.request("GET", "https://api.ipify.org")
local result, err = client:do_request(request)
if err then
table.insert(logs, err)
error(err)
end
if not (result.code == 200) then
table.insert(logs, err)
error(err)
end
table.insert(outputs, result.body)
`],
["HTTP Browserless", `local http = require("http")
local client = http.client()
# note " for keys/values
local data = '{"url": "https://api.ipify.org"}'
local request = http.request("POST", "http://browserless:3000/content", data)
request:header_set("Content-Type", "application/json")
local result, err = client:do_request(request)
if err then
table.insert(logs, err)
error(err)
end
if not (result.code == 200) then
table.insert(logs, "Response != 200 - " .. result.code)
end
table.insert(outputs, result.body)
`],
["XPath", `local xmlpath = require("xmlpath")
local query = "//td[@class='price']"
local query, err = xmlpath.compile(query)
if err then
table.insert(logs, err)
error(err)
end
for i,input in pairs(inputs) do
local node, err = xmlpath.load(input)
if err then
table.insert(logs, err)
error(err)
end
local iter = query:iter(node)
for k, v in pairs(iter) do
table.insert(outputs, v:string())
end
end
`],
["strings", `local strings = require("strings")
for i,input in pairs(inputs) do
table.insert(outputs, strings.trim_space(input))
end
`],
["filter", `
for i,input in pairs(inputs) do
["filter", `for i,input in pairs(inputs) do
number = tonumber(input)
if number % 2 == 0 then
table.insert(outputs, input)
@ -660,15 +765,22 @@ for i,input in pairs(inputs) do
end
`],
]);
let snippetDiv = document.createElement("div");
snippetDiv.classList.add("d-flex", "flex-wrap")
// add snippets
for (let [name, snippet] of luaSnippets) {
let link = document.createElement('a');
link.setAttribute("href", "#");
link.classList.add("btn", "btn-secondary", "btn-sm");
link.classList.add("btn", "btn-outline-secondary", "btn-sm", "xs");
link.innerHTML = name;
link.onclick = function() {var1Input.value = snippet;};
var1Div.appendChild(link)
snippetDiv.appendChild(link)
let gap = document.createElement("div");
gap.classList.add("m-1", "xs");
snippetDiv.appendChild(gap);
}
var1Div.appendChild(snippetDiv);
let var2Input = document.createElement("input");
var2Input.name = "var2";

View file

@ -1,9 +1,3 @@
# Todo
- comments
- lua snippets
- basic auth
- post
- browserless?
- xpath
- json
- safe escape {{ }} for pages