1
我想用Lua腳本在瀏覽器中打開特定的文本。有誰知道這是什麼正確的代碼?如何用lua的html字符串啓動瀏覽器
我嘗試這樣做:
local a ="<html><body>hi</body></html>"
fp=os.execute("start chrome"..a );
我想用Lua腳本在瀏覽器中打開特定的文本。有誰知道這是什麼正確的代碼?如何用lua的html字符串啓動瀏覽器
我嘗試這樣做:
local a ="<html><body>hi</body></html>"
fp=os.execute("start chrome"..a );
如果你的HTML不是很長(低於6千字節),你可以使用數據URI直接通過HTML到瀏覽器,而無需創建臨時文件。
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)
附:
對不起,我沒有使用鉻。
也許,用path\to\your\chrome.exe
代替path\to\firefox.exe
就足以使它與chrome一起工作。