2016-12-24 2 views
1

Luaスクリプトを使用してブラウザで特定のテキストを開きたいとします。誰でも正しいコードが何であるか知っていますか?あなたのHTMLが(6キロバイトの下に)非常に長いされていない場合、あなたは一時ファイルを作成せずにブラウザにHTMLを直接渡すためのデータURIを使用することができ、luaでhtml文字列でブラウザを起動するには

local a ="<html><body>hi</body></html>" 
fp=os.execute("start chrome"..a ); 

答えて

0

は、私はこれを試してみました。

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) 

P.S.
申し訳ありませんが、私はクロムを使用していません。
おそらく、path\to\firefox.exepath\to\your\chrome.exeに置き換えても、クロムで動作させるには十分でしょう。

関連する問題