-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.lua
More file actions
86 lines (66 loc) · 2.14 KB
/
create.lua
File metadata and controls
86 lines (66 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local displayElement = gurt.select('#del-code-display')
displayElement:hide()
local login = gurt.select("#create");
if login ~= nil then
login:on("click", function()
gurt.location.goto("/create")
end)
end
local register = gurt.select("#browse");
if register ~= nil then
register:on("click", function()
gurt.location.goto("/browse?p=1")
end)
end
local title = gurt.select("#home")
if title ~= nil then
title:on("click", function()
gurt.location.goto("/")
end)
end
local title = gurt.select("#search1")
title:on("click", function()
local input = gurt.select("#search").value
gurt.location.goto("/search?s=" .. input .. "&p=1")
end)
local title = gurt.select("#createwiki")
title:on("click", function()
local titleEl = gurt.select("#createtitle")
local contentEl = gurt.select("#createcontent")
-- grab text from textareas
local titleStr = tostring(titleEl.value or "")
local contentStr = tostring(contentEl.value or "")
function urlencode(str)
if str then
str = str:gsub("\n", "\r\n")
str = str:gsub("([^%w _%%%-%.~])", function(c)
return string.format("%%%02X", string.byte(c))
end)
str = str:gsub(" ", "+")
end
return str
end
contentStr = urlencode(contentStr)
titleStr = urlencode(titleStr)
contentStr = contentStr:gsub("/n", "|")
if titleStr:match("^%s*$") then
trace.log("Error: Title cannot be empty")
return
end
if contentStr:match("^%s*$") then
trace.log("Error: Content cannot be empty")
return
end
trace.log("Sending POST:\n" .. titleStr .. "\n" .. contentStr)
local response = fetch('/addpage?t=' .. titleStr .. '&b=' .. contentStr)
if response:ok() then
local text = response:text() -- Get as text
trace.log('Status: ' .. response.status)
trace.log('Status Text: ' .. response.statusText)
-- Access headers
local contentType = response.headers['content-type']
gurt.location.goto("/getpage?l=" .. text)
else
trace.log('Request failed with status: ' .. response.status)
end
end)