1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
const hutchSupportedHosts = new Set([
"git.sr.ht",
"hg.sr.ht",
"todo.sr.ht",
"builds.sr.ht",
"lists.sr.ht",
"meta.sr.ht",
"sr.ht",
]);
const hutchBrowserAPI = globalThis.browser ?? globalThis.chrome;
const bannerStorageKey = "showOpenInHutchBanner";
const dismissedStorageKey = `dismissedOpenInHutchBanner:${location.hostname}`;
function hutchIsOwnerRootPath(path) {
const components = path.split("/").filter(Boolean);
return components.length === 1 && components[0].startsWith("~");
}
function hutchNormalizedPath(pathname) {
return pathname.split("/").filter(Boolean).join("/");
}
function hutchDeepLinkPath(hostname, path) {
const components = path.split("/").filter(Boolean);
if (
hostname === "sr.ht" &&
components.length === 2 &&
components[0] === "projects" &&
components[1].startsWith("~")
) {
return components[1];
}
return path;
}
function hutchDeepLinkService(hostname, path) {
if (hutchIsOwnerRootPath(path)) {
return "lookup";
}
switch (hostname) {
case "git.sr.ht":
return "git";
case "hg.sr.ht":
return "hg";
case "todo.sr.ht":
return "todo";
case "builds.sr.ht":
return "builds";
case "lists.sr.ht":
return "lists";
default:
return "lookup";
}
}
function hutchDeepLinkForLocation() {
if (location.protocol !== "https:" || !hutchSupportedHosts.has(location.hostname)) {
return null;
}
const path = hutchDeepLinkPath(location.hostname, hutchNormalizedPath(location.pathname));
const service = hutchDeepLinkService(location.hostname, path);
return `hutch://${service}${path ? `/${path}` : ""}${location.search}${location.hash}`;
}
function storageGet(defaults) {
return new Promise((resolve) => {
hutchBrowserAPI.storage.local.get(defaults, resolve);
});
}
function storageSet(values) {
return new Promise((resolve) => {
hutchBrowserAPI.storage.local.set(values, resolve);
});
}
function showNotice(message) {
const existing = document.getElementById("hutch-open-notice");
existing?.remove();
const notice = document.createElement("div");
notice.id = "hutch-open-notice";
notice.textContent = message;
notice.style.cssText = [
"position:fixed",
"left:50%",
"bottom:18px",
"z-index:2147483647",
"transform:translateX(-50%)",
"background:#1f2937",
"color:white",
"font:13px -apple-system,BlinkMacSystemFont,Segoe UI,sans-serif",
"padding:8px 12px",
"border-radius:8px",
"box-shadow:0 8px 24px rgba(0,0,0,.24)",
].join(";");
document.documentElement.appendChild(notice);
setTimeout(() => notice.remove(), 3500);
}
async function injectBannerIfEnabled() {
const hutchURL = hutchDeepLinkForLocation();
if (!hutchURL || document.getElementById("hutch-open-banner")) {
return;
}
const settings = await storageGet({
[bannerStorageKey]: false,
[dismissedStorageKey]: false,
});
if (!settings[bannerStorageKey] || settings[dismissedStorageKey]) {
return;
}
const banner = document.createElement("div");
banner.id = "hutch-open-banner";
banner.style.cssText = [
"position:fixed",
"right:14px",
"bottom:14px",
"z-index:2147483647",
"display:flex",
"align-items:center",
"gap:8px",
"background:#111827",
"color:white",
"font:13px -apple-system,BlinkMacSystemFont,Segoe UI,sans-serif",
"padding:8px 10px",
"border-radius:8px",
"box-shadow:0 8px 24px rgba(0,0,0,.22)",
].join(";");
const button = document.createElement("button");
button.type = "button";
button.textContent = "Open in Hutch";
button.style.cssText = [
"appearance:none",
"border:0",
"background:transparent",
"color:inherit",
"font:inherit",
"font-weight:600",
"padding:0",
].join(";");
button.addEventListener("click", () => {
console.debug("[Hutch] Banner opening deep link", { sourceURL: location.href, hutchURL });
location.href = hutchURL;
});
const dismiss = document.createElement("button");
dismiss.type = "button";
dismiss.textContent = "Close";
dismiss.setAttribute("aria-label", "Dismiss Open in Hutch banner");
dismiss.style.cssText = [
"appearance:none",
"border:0",
"background:rgba(255,255,255,.14)",
"color:inherit",
"font:12px -apple-system,BlinkMacSystemFont,Segoe UI,sans-serif",
"padding:3px 6px",
"border-radius:6px",
].join(";");
dismiss.addEventListener("click", async () => {
await storageSet({ [dismissedStorageKey]: true });
banner.remove();
});
banner.append(button, dismiss);
document.documentElement.appendChild(banner);
}
hutchBrowserAPI.runtime.onMessage.addListener((message) => {
if (message?.type === "hutchUnsupportedPage") {
showNotice(message.message);
}
});
injectBannerIfEnabled();
|