mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
Open links from the desktop app in the default browser
AI Assisted
This commit is contained in:
1 parent
37f2735294
commit
7a9f56a452
5 files changed
+105
-2
No files matched your search
@@ -0,0 +1,41 @@
|
||||
// Plan2Code Web Console - hands an http or https link to the person's default browser, for the page inside the desktop app; defines only.
|
||||
//
|
||||
// The app's webview opens no new windows, so a link the page shows there (Open
|
||||
// pull request, a link in a doc, the feedback button) would do nothing. The
|
||||
// page posts it to /open-link instead, and this opens it. Never through a
|
||||
// shell: the link is the page's to name, so it travels as one argument to a
|
||||
// program that only opens links.
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
/** The link to open, normalised, or null when it is not a plain http or https link. */
|
||||
export function linkToOpen(raw) {
|
||||
if (typeof raw !== "string" || raw.length > 4096) return null;
|
||||
let url;
|
||||
try {
|
||||
url = new URL(raw);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return url.protocol === "http:" || url.protocol === "https:" ? url.href : null;
|
||||
}
|
||||
|
||||
/** Opens `href` (from linkToOpen) in the default browser; false when it could not start one. */
|
||||
export function openLink(href) {
|
||||
// Tests, and a person who turned the console's own browser launches off.
|
||||
if (process.env.NO_BROWSER || process.env.PLAN2CODE_NO_BROWSER) return true;
|
||||
const [cmd, args] =
|
||||
process.platform === "win32"
|
||||
? ["rundll32.exe", ["url.dll,FileProtocolHandler", href]]
|
||||
: process.platform === "darwin"
|
||||
? ["open", [href]]
|
||||
: [process.env.BROWSER || "xdg-open", [href]];
|
||||
try {
|
||||
const child = spawn(cmd, args, { detached: true, stdio: "ignore", windowsHide: true });
|
||||
child.on("error", () => {});
|
||||
child.unref();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// Plan2Code Web Console - the few messages this page sends the desktop app that frames it; defines only.
|
||||
// Plan2Code Web Console - the few messages this page sends the desktop app that frames it, and which links it must open another way there; defines only.
|
||||
//
|
||||
// Each one is a named request with no free text in it, so the app decides what
|
||||
// it does: a page can ask for "continue" to be typed, never for anything else.
|
||||
@@ -8,6 +8,21 @@ export function framed() {
|
||||
return Boolean(window.parent) && window.parent !== window;
|
||||
}
|
||||
|
||||
/**
|
||||
* True for a link that leaves the console for the web: http or https on
|
||||
* another origin. Inside the app those open through /open-link, since the
|
||||
* app's window opens no new ones; the console's own files stay put.
|
||||
*/
|
||||
export function linkLeavesPage(href, origin) {
|
||||
let url;
|
||||
try {
|
||||
url = new URL(href, origin);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
return (url.protocol === "http:" || url.protocol === "https:") && url.origin !== origin;
|
||||
}
|
||||
|
||||
/** Post `message` to the frame's parent, as `{ plan2code: kind, ...extra }`; nothing outside a frame. */
|
||||
export function tellApp(kind, extra = {}) {
|
||||
if (!framed()) return;
|
||||
|
||||
@@ -42,6 +42,7 @@ import { cleanSetting, offered } from "./public/subagents.js";
|
||||
import { ACCENTS, DEFAULT_ACCENT, DEFAULT_CARD_WIDTH, cardWidth } from "./public/palette.js";
|
||||
import { SERVABLE, TYPES, asset } from "./static.mjs";
|
||||
import { handoffsDir, isHandoffId } from "./handoffs.mjs";
|
||||
import { linkToOpen, openLink } from "./open-link.mjs";
|
||||
import {
|
||||
NotepadError,
|
||||
addImage,
|
||||
@@ -99,6 +100,7 @@ const ROUTES = [
|
||||
{ match: (p, req) => p.startsWith("/shots/") || req.url.startsWith("/shots/"), run: shot },
|
||||
{ match: (p, req) => p.startsWith("/uploads/") || req.url.startsWith("/uploads/"), run: uploads },
|
||||
{ path: "/submit", method: "POST", origin: true, run: submit },
|
||||
{ path: "/open-link", method: "POST", origin: true, run: openLinkRoute },
|
||||
{ path: "/chat", method: "POST", origin: true, run: chat },
|
||||
{ path: "/chat/decision", method: "POST", origin: true, run: chatDecision },
|
||||
{ path: "/workspace", method: "GET", run: workspaceGet },
|
||||
@@ -1025,6 +1027,18 @@ function recordSubmitted(state, actions) {
|
||||
}
|
||||
}
|
||||
|
||||
// A link the page inside the desktop app cannot open itself (open-link.mjs).
|
||||
// http and https only; the page shows the link to paste when this says no.
|
||||
function openLinkRoute(req, res) {
|
||||
return readBody(req, (err, body) => {
|
||||
if (err) return json(res, err.tooBig ? 413 : 400, { error: err.tooBig ? "too large" : "body must be JSON" });
|
||||
const href = linkToOpen(body && body.url);
|
||||
if (!href) return json(res, 400, { error: "only http and https links open" });
|
||||
if (!openLink(href)) return json(res, 500, { error: "no browser could be started" });
|
||||
return json(res, 200, { ok: true, url: href });
|
||||
});
|
||||
}
|
||||
|
||||
function submit(req, res, ctx) {
|
||||
return readBody(req, (err, body) => {
|
||||
if (err) return json(res, err.tooBig ? 413 : 400, { error: err.tooBig ? "too large" : "body must be JSON" });
|
||||
|
||||
Reference in new issue
Block a user