如何使用 lua 启动带有 html 字符串的浏览器 [英] How to start browser with html string with lua
本文介绍了如何使用 lua 启动带有 html 字符串的浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Lua 脚本在浏览器中打开特定文本.有谁知道正确的代码是什么?
I want to open a particular text in browser using Lua script. Does anyone know what the correct code is for that ?
我试过了:
local a ="<html><body>hi</body></html>"
fp=os.execute( "start chrome"..a );
推荐答案
如果您的 html 不是很长(6 KB 以下),您可以使用数据 URI 将 html 直接传递给浏览器,而无需创建临时文件.
If your html is not very long (below 6 KBytes), you can use data URI for passing html directly to browser without creating temporary file.
local function show_html(some_html)
local encoder_table = {}
for _, chars in ipairs{'==', 'AZ', 'az', '09', '++', '//'} do
for ascii = chars:byte(), chars:byte(2) do
table.insert(encoder_table, string.char(ascii))
end
end
local function tobase64(str)
local result, pos = {}, 1
while pos <= #str do
local last3 = {str:byte(pos, pos+2)}
local padded = 3 - #last3
for j = 1, padded do
table.insert(last3, 0)
end
local codes = {
math.floor(last3[1] / 4),
last3[1] % 4 * 16 + math.floor(last3[2] / 16),
last3[2] % 16 * 4 + math.floor(last3[3] / 64),
last3[3] % 64
}
for j = 1, 4 do
codes[j] = encoder_table[j+padded > 4 and 1 or 2+codes[j]]
end
pos = pos + 3
table.insert(result, table.concat(codes))
end
return table.concat(result)
end
os.execute([[start "" "C:\Program Files\Mozilla Firefox\firefox.exe" ]]
..'"data:text/html;charset=utf-8;base64,'..tobase64(some_html)..'"'
)
end
用法:
local html = [[
<html>
<body>
<h3>Hi</h3>
<script>alert('Hello, World!')</script>
</body>
</html>
]]
show_html(html)
附注
抱歉,我没有使用 chrome.
可能,用 path\to\your\chrome.exe
替换 path\to\firefox.exe
就足以使它与 chrome 一起工作.
P.S.
Sorry, I'm not using chrome.
Probably, replacing path\to\firefox.exe
with path\to\your\chrome.exe
would be enough to make it work with chrome.
这篇关于如何使用 lua 启动带有 html 字符串的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文