Skip to content

Commit 12a3b44

Browse files
committed
refactor: Rename asset URL functions and enhance Tampermonkey route functionality
Renamed `buildAbsoluteAssetUrl` to `buildShareAssetUrl` and introduced `buildFetchAssetUrl` for improved clarity in asset URL handling. Updated the Tampermonkey route to utilize the new functions, enhancing the user experience with a new download button for assets and updated instructions for using the Tampermonkey extension.
1 parent 35059b7 commit 12a3b44

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

src/routes/tampermonkey.tsx

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Link, createFileRoute } from '@tanstack/react-router'
33
import {
44
ChevronLeft,
55
Copy,
6+
Download,
67
ExternalLink,
78
FileText,
89
ShieldCheck,
@@ -100,7 +101,7 @@ function writeCachedSource(urlPath: string, text: string) {
100101
}
101102
}
102103

103-
function buildAbsoluteAssetUrl(relativePath: string) {
104+
function buildShareAssetUrl(relativePath: string) {
104105
const basePath = relativePath.startsWith('/') ? relativePath : `/${relativePath}`
105106
if (typeof window === 'undefined') {
106107
return new URL(basePath, PRODUCTION_ORIGIN).toString()
@@ -114,6 +115,15 @@ function buildAbsoluteAssetUrl(relativePath: string) {
114115
return new URL(basePath, origin).toString()
115116
}
116117

118+
function buildFetchAssetUrl(relativePath: string) {
119+
const basePath = relativePath.startsWith('/') ? relativePath : `/${relativePath}`
120+
if (typeof window === 'undefined') {
121+
return new URL(basePath, PRODUCTION_ORIGIN).toString()
122+
}
123+
124+
return new URL(basePath, window.location.origin).toString()
125+
}
126+
117127
function TampermonkeyRoute() {
118128
const tmFolder =
119129
folderGroups.find((folder) => folder.id === 'tampermonkey') ?? null
@@ -168,7 +178,7 @@ function TampermonkeyRoute() {
168178
setSourceError(null)
169179

170180
try {
171-
const absoluteUrl = buildAbsoluteAssetUrl(url)
181+
const absoluteUrl = buildFetchAssetUrl(url)
172182
const response = await fetch(absoluteUrl, {
173183
signal: controller.signal,
174184
cache: 'no-store',
@@ -215,7 +225,7 @@ function TampermonkeyRoute() {
215225

216226
const absoluteAssetUrl = useMemo(() => {
217227
if (!activeAsset) return null
218-
return buildAbsoluteAssetUrl(activeAsset.urlPath)
228+
return buildShareAssetUrl(activeAsset.urlPath)
219229
}, [activeAsset])
220230

221231
const handleCopyUrl = async () => {
@@ -296,10 +306,10 @@ function TampermonkeyRoute() {
296306
<div className="rounded-xl border bg-card p-5 shadow-sm">
297307
<h3 className="flex items-center gap-2 text-sm font-semibold">
298308
<div className="size-1.5 rounded-full bg-emerald-500" />
299-
How to install
309+
How to use
300310
</h3>
301311
<p className="mt-3 text-sm leading-relaxed text-muted-foreground">
302-
Install the Tampermonkey browser extension, then open a script and click Install.
312+
Install the Tampermonkey browser extension, then open a script to view it and copy the raw URL.
303313
</p>
304314
</div>
305315
<div className="rounded-xl border bg-card p-5 shadow-sm">
@@ -357,17 +367,6 @@ function TampermonkeyRoute() {
357367
</div>
358368
</div>
359369
<div className="flex items-center gap-2">
360-
<Button
361-
asChild
362-
variant="default"
363-
size="sm"
364-
className="h-8 gap-1.5 text-xs"
365-
>
366-
<a href={absoluteAssetUrl ?? activeAsset?.urlPath} target="_blank" rel="noreferrer">
367-
<ShieldCheck className="size-3.5" />
368-
Install
369-
</a>
370-
</Button>
371370
<Button
372371
variant="outline"
373372
size="sm"
@@ -399,6 +398,17 @@ function TampermonkeyRoute() {
399398
Raw
400399
</a>
401400
</Button>
401+
<Button
402+
asChild
403+
variant="outline"
404+
size="sm"
405+
className="h-8 gap-1.5 text-xs"
406+
>
407+
<a href={activeAsset?.urlPath} download>
408+
<Download className="size-3.5" />
409+
<span className="hidden sm:inline">Download</span>
410+
</a>
411+
</Button>
402412
</div>
403413
</div>
404414

0 commit comments

Comments
 (0)