2

I want to open a particular text in browser using Lua script. Does anyone know what the correct code is for that ?

I tried this:

local a ="<html><body>hi</body></html>" 
fp=os.execute( "start chrome"..a  );
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
Dharani
  • 31
  • 4

1 Answers1

0

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

Usage:

local html = [[
<html>
  <body>
    <h3>Hi</h3>
    <script>alert('Hello, World!')</script>
  </body>
</html>
]]
show_html(html)

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.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64