我有一个生成一些HTML页面的脚本,完成后,我打开生成的页面的索引文件。为此,我有以下代码:
if exist "generated_pages/index.html" start "" "generated_pages/index.html"
现在,该页面在.html文件的默认文本编辑器中打开,如何确保它在用户的默认浏览器中打开?我不想为特定的浏览器使用命令,因为我不知道用户的默认浏览器将是什么。
答案 0 :(得分:1)
不幸的是,无法用start
命令指定您要启动哪种程序。它将基于文件的扩展名启动默认的关联程序,而您对于用户对.html文件的文件关联的可疑选择感到不满。如果要确保仅通过Web浏览器而不是通过文本编辑器打开文件,那么将URL传递到start
最好比文件系统位置打开。使用http地址作为start
的参数应确保打开位置的设备将是Web浏览器。
可以通过http服务.html文件,而无需依赖第三方的二进制文件。使用.Net方法创建基本的Web服务器并通过localhost返回网页并不难。这样,您可以start "" "http://localhost:port/"
,并且如果用户搞砸了他们的文件关联,您将有更大的机会避免在文本编辑器中打开文件。
将以下魔术保存为.bat脚本,根据需要调整html文件名和位置,然后尝试一下。
<# : httptest.bat -- https://stackoverflow.com/a/53689025/1683264
@echo off & setlocal
if exist test.html call :display test.html
goto :EOF
:display <htmlfile>
setlocal
set "infile=%~f1"
powershell -noprofile "iex (${%~f0} | out-string)"
endlocal & exit /b
: end Batch / begin PowerShell polyglot code #>
$tcpClient = new-object Net.Sockets.TcpClient
while ($port = get-random -min 1024 -max 65535) {
try {$tcpClient.Connect("localhost", $port)}
catch {$tcpClient.Dispose(); break}
}
$endpoint = new-object Net.IPEndPoint([Net.IPAddress]::Any, $port)
$listener = new-object Net.Sockets.TcpListener $endpoint
$listener.start()
cmd /c start "" "http://localhost:$($port)/"
$client = $listener.AcceptTcpClient()
$stream = $client.GetStream()
if ($stream.CanRead) {
[void]$stream.read((new-object byte[] 1024), 0, 1024);
}
if ($stream.CanWrite) {
$content = "HTTP/1.1 200 OK`n`n$(gc $env:infile)"
$out = [text.encoding]::UTF8.GetBytes($content)
$stream.write($out, 0, $out.length)
}
$stream.close()
$stream.dispose()
$listener.stop()
作为附带的好处,通过http提供html可以帮助您避免某些浏览器的安全受到限制,从而禁止JavaScript从file:/// URL执行。
如果要包括其他引用的文件,例如图像,css文件,源JavaScript文件等,那确实会有些棘手。这是一个更彻底的示例,它侦听最初的http请求最多60秒钟,然后在浏览器请求它们时继续提供相对路径源文件,直到5秒钟没有收到请求为止。它应该正确声明图像和其他源文件的mime类型。如果需要更长的超时时间,请更改底部附近的serve-content 5
行。
<# : httptest2.bat -- https://stackoverflow.com/a/53689025/1683264
@echo off & setlocal
if exist "%~1" (call :display "%~1") else goto usage
goto :EOF
:usage
echo Usage: %~nx0 htmlfile
exit /b
:display <htmlfile>
setlocal
set "infile=%~f1"
powershell -noprofile "iex (${%~f0} | out-string)"
endlocal & exit /b
: end Batch / begin PowerShell polyglot code #>
Add-Type -as System.Web
$rootpath = (get-item $env:infile).DirectoryName
$filename = (get-item $env:infile).Name
$webname = [Web.HttpUtility]::UrlEncode($filename)
$tcpClient = new-object Net.Sockets.TcpClient
while ($port = get-random -min 1024 -max 65535) {
try {$tcpClient.Connect("localhost", $port)}
catch {$tcpClient.Dispose(); break}
}
cmd /c start "" "http://localhost:$($port)/$webname"
function log($polarity, $txt) {
$color = (("red","darkgray"),("green","white"))[$polarity]
write-host -nonewline "[" -f $color[1]
write-host -nonewline "*" -f $color[0]
write-host "] $txt" -f $color[1]
}
function serve-content($seconds) {
$timer = (get-date).AddSeconds($seconds)
while (!$listener.Pending()) {
start-sleep -milliseconds 10
if ((get-date) -ge $timer) { return $false }
}
$client = $listener.AcceptTcpClient()
$stream = $client.GetStream()
if ($stream.CanRead) {
$request = new-object byte[] 1024
$size = $stream.read($request, 0, $request.length)
$headers = [text.encoding]::UTF8.GetString($request, 0, $size)
if ($stream.CanWrite) {
$loc = $headers.split("`r?`n")[0] -replace "^\S+\s+|\s+HTTP/\d.+$"
$loc = $loc -replace "^/", "$rootpath/" -replace "/", "\"
$loc = [Web.HttpUtility]::UrlDecode($loc)
if ($loc) {
if (!(test-path $loc -type leaf)) {
$loc = [Web.HttpUtility]::UrlDecode($loc)
}
if (test-path $loc -type leaf) {
$response = ,"HTTP/1.1 200 OK"
$mime = [Web.MimeMapping]::GetMimeMapping($loc)
$response += ,"Content-Type: $mime"
$response += ,"Content-Length: $((gi $loc).length)","",""
$out = [text.encoding]::UTF8.GetBytes(($response -join "`n"))
[byte[]]$body = gc $loc -enc byte
$out += $body
$stream.write($out, 0, $out.length)
log $true $loc
}
else {
$response = "HTTP/1.1 404 Not Found","",@"
<html lang="en">
<head>
<title>Error 404</title>
</head>
<body>
<h3>Not Found</h3>
<p>The requested resource could not be located.</p>
</body>
</html>
"@
$out = [text.encoding]::UTF8.GetBytes(($response -join "`n"))
$stream.write($out, 0, $out.length)
log $false $loc
}
}
}
}
$stream.close()
$stream.dispose()
$client.close()
return $true
}
$endpoint = new-object Net.IPEndPoint([Net.IPAddress]::Any, $port)
$listener = new-object Net.Sockets.TcpListener $endpoint
$listener.start()
[void](serve-content 60)
while ((serve-content 5)) {}
$listener.stop()
答案 1 :(得分:0)
我不知道这是否可以在其他浏览器上使用,但是对于Chrome来说,就可以了。您可以使用html
打开chrome.exe
文件,如下所示:
if exist "generated_pages/index.html" start "" "full\path\to\chrome.exe" file:///C:/example/generated_pages/index.html
另一种方法是更改Windows帐户中html
文件的默认处理程序。 ({right-click to file
=> Open With...
=> select your browser
和option "Open always html files..."
)。