mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 07:29:23 +00:00
Added LaunchBrowser API (#153)
You can now add LaunchBrowser() to .init.lua which will ask redbean to open your website in the local desktop browser when the server is launched.
This commit is contained in:
parent
7ed524ca31
commit
69c508729e
1 changed files with 24 additions and 0 deletions
|
@ -1461,6 +1461,24 @@ static bool IsHiddenPath(const char *s) {
|
|||
return false;
|
||||
}
|
||||
|
||||
static void LaunchBrowser() {
|
||||
char openbrowsercommand[255];
|
||||
char *prog;
|
||||
if (IsWindows()) {
|
||||
prog = "explorer";
|
||||
} else if (IsXnu()) {
|
||||
prog = "open";
|
||||
} else {
|
||||
prog = "xdg-open";
|
||||
}
|
||||
struct in_addr addr = serveraddr.sin_addr;
|
||||
if (addr.s_addr == INADDR_ANY) addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
snprintf(openbrowsercommand, sizeof(openbrowsercommand), "%s http://%s:%d",
|
||||
prog, inet_ntoa(addr), ntohs(serveraddr.sin_port));
|
||||
DEBUGF("Opening browser with command %s\n", openbrowsercommand);
|
||||
system(openbrowsercommand);
|
||||
}
|
||||
|
||||
static int LuaServeAsset(lua_State *L) {
|
||||
size_t pathlen;
|
||||
struct Asset *a;
|
||||
|
@ -1915,6 +1933,11 @@ static int LuaGetZipPaths(lua_State *L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int LuaLaunchBrowser(lua_State *L) {
|
||||
LaunchBrowser();
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void LuaRun(const char *path) {
|
||||
struct Asset *a;
|
||||
const char *code;
|
||||
|
@ -1954,6 +1977,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
|||
{"GetUri", LuaGetUri}, //
|
||||
{"GetVersion", LuaGetVersion}, //
|
||||
{"GetZipPaths", LuaGetZipPaths}, //
|
||||
{"LaunchBrowser", LuaLaunchBrowser}, //
|
||||
{"HasParam", LuaHasParam}, //
|
||||
{"HidePath", LuaHidePath}, //
|
||||
{"LoadAsset", LuaLoadAsset}, //
|
||||
|
|
Loading…
Add table
Reference in a new issue