Skip to content

Instantly share code, notes, and snippets.

@bbielsa
Created August 11, 2022 00:52
Show Gist options
  • Save bbielsa/25a7016bf813e99a6e131e05a8cd9012 to your computer and use it in GitHub Desktop.
Save bbielsa/25a7016bf813e99a6e131e05a8cd9012 to your computer and use it in GitHub Desktop.
Wiremod EGP (Expression 2) in-game web browser
@name Browser
@inputs Graphics:wirelink User:entity
@persist RenderedNodes
@persist TotalNodes
@persist NodeRenderBatch
@persist Response:table
@persist ScrollY
@persist RequestedVisit
@persist RequestedNodes
NodeRenderBatch = 15
interval(250)
function void render() {
local Res = Response
local TextNodesCount = Res["textNodesCount", number]
local Before = RenderedNodes
for(I = RenderedNodes, RenderedNodes + NodeRenderBatch) {
local Key = "text" + I
local EgpIndex = I + 1
local Text = Res[Key + "text", string]
local Top = Res[Key + "top", number]
local Left = Res[Key + "left", number]
local Right = Res[Key + "right", number]
local Bottom = Res[Key + "bottom", number]
local FontSize = Res[Key + "fontSize", number]
local R = Res[Key + "colorR", number]
local G = Res[Key + "colorG", number]
local B = Res[Key + "colorB", number]
local Pos = vec2(Left, Top)
local Size = vec2(Right - Left, Bottom - Top)
Graphics:egpTextLayout(EgpIndex, Text, Pos, Size)
Graphics:egpFont(EgpIndex, "consolas", FontSize)
Graphics:egpColor(EgpIndex, R, G, B, 255)
RenderedNodes = RenderedNodes + 1
}
local BatchRendered = RenderedNodes - Before
print("Batch rendered " + BatchRendered)
Graphics:egpDrawTopLeft(0)
}
function void tick() {
if(RequestedVisit && !RequestedNodes) {
print("Requesting nodes")
}
if(RenderedNodes >= TotalNodes) {
# Done rendering, return
return
}
render()
}
function void request(Url:string) {
local RequestUrl = "https://9d3a-100-8-148-247.ngrok.io/visit"
+ "?id=" + 11
+ "&url=" + httpUrlEncode(Url)
httpRequest(RequestUrl)
}
function void requestNodes(Id:number, StartIndex, EndIndex) {
local RequestUrl = "http://localhost:3000/nodes"
+ "?id=" + 11
+ "&si=" + StartIndex
+ "&ei=" + EndIndex
httpRequest(RequestUrl)
}
function void handleVisit() {
local Data = httpData()
local Res = jsonDecode(Data)
local Success = Res["success", number]
print("Success? " + Success)
if(!Success) {
print("Error response, not requesting nodes")
return
}
requestNodes(11, 0, NodeRenderBatch)
}
function void handleNodes() {
print("Got Node request")
local Data = httpData()
local Res = jsonDecode(Data)
local TextNodesCount = Res["textNodesCount", number]
print("Parsed response")
local BgR = Res["bgColorR", number]
local BgG = Res["bgColorG", number]
local BgB = Res["bgColorB", number]
TotalNodes = TextNodesCount
RenderedNodes = 0
Response = Res
Graphics:egpClear()
Graphics:egpDrawTopLeft(1)
Graphics:egpBox(300, vec2(0, 0), vec2(512, 512))
Graphics:egpColor(300, BgR, BgG, BgB, 255)
}
if(first()) {
runOnHTTP(1)
runOnTick(1)
request("https://google.com/")
RenderedNodes = 0
RequestedVisit = 0
RequestedNodes = 0
}
if(httpClk()) {
VisitRequest = httpRequestUrl():find("visit")
NodesRequest = httpRequestUrl():find("nodes")
print(VisitRequest, NodesRequest)
if(VisitRequest) {
handleVisit()
RequestedVisit = 1
}
elseif(NodesRequest) {
handleNodes()
RequestedNodes = 1
}
}
if(clk()) {
tick()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment