-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.gui_script
75 lines (64 loc) · 2.07 KB
/
example.gui_script
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
local console = require "defcon.console"
local example_module = require "example.example_module"
local function get_ip()
for i, a in ipairs(sys.get_ifaddrs()) do
if a.up and a.address then
return a.address
end
end
return nil
end
function init(self)
msg.post(".", "acquire_input_focus")
local my_ip = get_ip()
gui.set_text(gui.get_node("instructions"), "IP: " .. (my_ip and my_ip or "?") .. " - Click to open localhost browser connection")
console.register_command("bark", "Bark like a dog!", function() return "Woof!" end)
console.register_module(math, "math")
console.register_module(example_module, "example")
console.server.router.get("^/imgur/(.*)$", function(matches)
local path = matches[1]
local co = coroutine.running()
local headers = {
Authorization = "Client-ID 0166969aba5f28b"
}
http.request("https://api.imgur.com/3/gallery/search/viral?q=" .. path .."&q_type=png&q_size_px=small", "GET", function(self, id, response)
local decoded_response = json.decode(response.response)
local html = ""
for _,image_or_album in pairs(decoded_response.data or {}) do
if not image_or_album.is_album then
html = html .. "<a href='" .. image_or_album.link .. "'>" .. image_or_album.title .. "</a></br>"
end
end
coroutine.resume(co, console.server.html(html))
end, headers)
return coroutine.yield()
end)
end
function final(self)
msg.post(".", "release_input_focus")
end
local count = 0
function update(self, dt)
if math.random(1,20) == 1 then
count = count + 1
print("This is a print from the running app", os.time(), count)
end
if math.random(1,40) == 1 then
count = count + 1
pprint({ 1, 2, 4, 6, "foobar", key = "value", time = os.time(), count = count })
end
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end
function on_input(self, action_id, action)
if action.released then
sys.open_url("http://localhost:8098")
return
end
end
function on_reload(self)
-- Add input-handling code here
-- Remove this function if not needed
end