2016-12-24 26 views

回答

0

如果你的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一起工作。