mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-30 17:22:27 +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;
|
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) {
|
static int LuaServeAsset(lua_State *L) {
|
||||||
size_t pathlen;
|
size_t pathlen;
|
||||||
struct Asset *a;
|
struct Asset *a;
|
||||||
|
@ -1915,6 +1933,11 @@ static int LuaGetZipPaths(lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int LuaLaunchBrowser(lua_State *L) {
|
||||||
|
LaunchBrowser();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static void LuaRun(const char *path) {
|
static void LuaRun(const char *path) {
|
||||||
struct Asset *a;
|
struct Asset *a;
|
||||||
const char *code;
|
const char *code;
|
||||||
|
@ -1954,6 +1977,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"GetUri", LuaGetUri}, //
|
{"GetUri", LuaGetUri}, //
|
||||||
{"GetVersion", LuaGetVersion}, //
|
{"GetVersion", LuaGetVersion}, //
|
||||||
{"GetZipPaths", LuaGetZipPaths}, //
|
{"GetZipPaths", LuaGetZipPaths}, //
|
||||||
|
{"LaunchBrowser", LuaLaunchBrowser}, //
|
||||||
{"HasParam", LuaHasParam}, //
|
{"HasParam", LuaHasParam}, //
|
||||||
{"HidePath", LuaHidePath}, //
|
{"HidePath", LuaHidePath}, //
|
||||||
{"LoadAsset", LuaLoadAsset}, //
|
{"LoadAsset", LuaLoadAsset}, //
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue