Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(chrome-extension): add Cap web page recorder panel bridge

Richie McIlroya70f588aa6dd
diff --git a/apps/chrome-extension/src/background/service-worker.ts b/apps/chrome-extension/src/background/service-worker.ts
index 2e811d99a..43eccb06c 100644
--- a/apps/chrome-extension/src/background/service-worker.ts
+++ b/apps/chrome-extension/src/background/service-worker.ts
@@ -1682,6 +1682,11 @@ const handleRequest = async (
};
}
+ if (message.type === "open-recorder-panel") {
+ await openRecorderPanel(sender.tab);
+ return { ok: true };
+ }
+
if (
message.type === "pause-recording" ||
message.type === "resume-recording"
diff --git a/apps/chrome-extension/src/content/bootstrap.ts b/apps/chrome-extension/src/content/bootstrap.ts
index 7369689b3..60cf019e1 100644
--- a/apps/chrome-extension/src/content/bootstrap.ts
+++ b/apps/chrome-extension/src/content/bootstrap.ts
@@ -27,6 +27,22 @@ type OverlayModule = {
const UI_PHASES = new Set(["creating", "recording", "paused"]);
const BOOTSTRAP_FLAG = "__capExtensionContentBootstrap";
+const INSTALLED_ATTRIBUTE = "data-cap-chrome-extension-installed";
+const READY_EVENT = "cap-chrome-extension-ready";
+const OPEN_EVENT = "cap-chrome-extension-open";
+
+const isCapWebOrigin = () => {
+ const { hostname, protocol } = window.location;
+ if (protocol !== "http:" && protocol !== "https:") return false;
+
+ return (
+ hostname === "cap.so" ||
+ hostname.endsWith(".cap.so") ||
+ hostname === "localhost" ||
+ hostname === "127.0.0.1" ||
+ hostname === "::1"
+ );
+};
const readPhase = (value: unknown): string | null => {
if (!value || typeof value !== "object") return null;
@@ -128,6 +144,19 @@ const bootstrap = () => {
chrome.runtime.onMessage.addListener(handleRuntimeMessage);
chrome.storage.onChanged.addListener(handleStorageChange);
+ if (isCapWebOrigin()) {
+ document.documentElement.setAttribute(INSTALLED_ATTRIBUTE, "true");
+ window.dispatchEvent(new CustomEvent(READY_EVENT));
+ window.addEventListener(OPEN_EVENT, () => {
+ chrome.runtime.sendMessage(
+ { target: "service-worker", type: "open-recorder-panel" },
+ () => {
+ void chrome.runtime.lastError;
+ },
+ );
+ });
+ }
+
// One cheap session-storage read decides whether this page needs UI right
// away: a recording in progress or the recorder panel open (the panel
// follows the user across tabs).
diff --git a/apps/chrome-extension/src/shared/types.ts b/apps/chrome-extension/src/shared/types.ts
index 4c126edbd..68331e9be 100644
--- a/apps/chrome-extension/src/shared/types.ts
+++ b/apps/chrome-extension/src/shared/types.ts
@@ -369,6 +369,10 @@ export type ServiceWorkerRequest =
target: "service-worker";
type: "get-recording-status";
}
+ | {
+ target: "service-worker";
+ type: "open-recorder-panel";
+ }
| {
target: "service-worker";
type: "open-options";