如何使用 Lua 以 HTML 字符串的形式启动浏览器

我想使用 Lua 脚本在浏览器中打开特定的文本。有人知道正确的代码是什么吗?

我尝试过这个:

local a ="<html><body>hi</body></html>"
fp=os.execute( "start chrome"..a  );
点赞
用户1847592
用户1847592

如果您的HTML不是很长(低于6KBytes),您可以使用data URI将HTML直接传递给浏览器,而无需创建临时文件。

函数 show_html(some_html)
   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
   function tobase64(str)
      result, pos = {}, 1
      while pos <= #str do
         last3 = {str:byte(pos, pos+2)}
         padded = 3 - #last3
         for j = 1, padded do
            table.insert(last3, 0)
         end
         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\firefox.exe 替换为 path\to\your\chrome.exe 就足以使其在chrome中工作了。

2016-12-24 09:37:35