|
| 1 | +import userChromeCss from "./files/userChrome.css"; |
| 2 | +import userJs from "./files/user.js.txt"; |
1 | 3 | import type { TargetInfos, WebsiteType } from "~~/types"; |
2 | 4 |
|
| 5 | +function psQuote(s: string): string { |
| 6 | + return s.replace(/'/g, "''"); |
| 7 | +} |
| 8 | + |
| 9 | +function firefoxSetup(target: TargetInfos): string { |
| 10 | + if (target.bw !== "firefox") return "# Firefox setup skipped"; |
| 11 | + |
| 12 | + return `Write-Host "\nSetting up Firefox..." |
| 13 | +Start-Process -FilePath '${psQuote(target.path)}' -ArgumentList '-CreateProfile','FTWA' -Wait -NoNewWindow |
| 14 | +
|
| 15 | +$ProfileBase = Join-Path $env:APPDATA 'Mozilla\\Firefox\\Profiles' |
| 16 | +$ProfileFolder = Get-ChildItem -Path $ProfileBase -Directory -Filter '*.FTWA' | Select-Object -First 1 -ExpandProperty FullName |
| 17 | +if (-not $ProfileFolder) { throw 'FTWA Firefox profile not found.' } |
| 18 | +
|
| 19 | +$ChromeDir = Join-Path $ProfileFolder 'chrome' |
| 20 | +New-Item -ItemType Directory -Force -Path $ChromeDir | Out-Null |
| 21 | +
|
| 22 | +@' |
| 23 | +${userChromeCss} |
| 24 | +'@ | Set-Content -Path (Join-Path $ChromeDir 'userChrome.css') -Encoding UTF8 |
| 25 | +
|
| 26 | +@' |
| 27 | +${userJs} |
| 28 | +'@ | Set-Content -Path (Join-Path $ProfileFolder 'user.js') -Encoding UTF8 |
| 29 | +
|
| 30 | +Write-Host "Firefox setup done."`; |
| 31 | +} |
| 32 | + |
3 | 33 | export function windows({ |
4 | 34 | website, |
5 | 35 | target, |
6 | 36 | }: { |
7 | 37 | website: WebsiteType; |
8 | 38 | target: TargetInfos; |
9 | 39 | }) { |
10 | | - // TODO: https://github.com/nwutils/create-desktop-shortcuts/blob/main/src/windows.vbs |
| 40 | + const safeName = website.name.replace(/[\\/:*?"<>|]/g, ""); |
| 41 | + const fname = "ftwa-" + website.name.toLowerCase().replace(/[^a-z0-9]+/g, "-"); |
| 42 | + |
| 43 | + let commandArgs = ""; |
| 44 | + if (target.bw === "firefox") { |
| 45 | + commandArgs = `--no-remote -P "FTWA" "${website.url}"`; |
| 46 | + } else if (["chrome", "opera", "brave", "edge"].includes(target.bw)) { |
| 47 | + commandArgs = `--app=${website.url}`; |
| 48 | + } |
| 49 | + |
| 50 | + return `# Generated by FTWA (https://ftwa.mathix.dev) |
| 51 | +# /u/ links are only available for 24h |
| 52 | +# Check the app store for more apps: https://ftwa.mathix.dev/store |
| 53 | +
|
| 54 | +$ErrorActionPreference = 'Stop' |
| 55 | +
|
| 56 | +${firefoxSetup(target)} |
| 57 | +
|
| 58 | +Write-Host "\nCreating shortcut..." |
| 59 | +
|
| 60 | +$IconDir = Join-Path $env:LOCALAPPDATA 'FTWA\\icons' |
| 61 | +New-Item -ItemType Directory -Force -Path $IconDir | Out-Null |
| 62 | +
|
| 63 | +$PngPath = Join-Path $IconDir '${fname}.png' |
| 64 | +$IcoPath = Join-Path $IconDir '${fname}.ico' |
| 65 | +
|
| 66 | +Invoke-WebRequest -Uri '${website.logo}' -OutFile $PngPath -UseBasicParsing |
| 67 | +
|
| 68 | +# Wrap the PNG as a single-image ICO so Windows can use any size (incl. 256+) |
| 69 | +Add-Type -AssemblyName System.Drawing |
| 70 | +$Bmp = [System.Drawing.Bitmap]::FromFile($PngPath) |
| 71 | +$IcoW = if ($Bmp.Width -ge 256) { [byte]0 } else { [byte]$Bmp.Width } |
| 72 | +$IcoH = if ($Bmp.Height -ge 256) { [byte]0 } else { [byte]$Bmp.Height } |
| 73 | +$Bmp.Dispose() |
| 74 | +
|
| 75 | +$PngBytes = [System.IO.File]::ReadAllBytes($PngPath) |
| 76 | +$Ms = New-Object System.IO.MemoryStream |
| 77 | +$Bw = New-Object System.IO.BinaryWriter($Ms) |
| 78 | +$Bw.Write([uint16]0); $Bw.Write([uint16]1); $Bw.Write([uint16]1) |
| 79 | +$Bw.Write($IcoW); $Bw.Write($IcoH); $Bw.Write([byte]0); $Bw.Write([byte]0) |
| 80 | +$Bw.Write([uint16]1); $Bw.Write([uint16]32) |
| 81 | +$Bw.Write([uint32]$PngBytes.Length); $Bw.Write([uint32]22) |
| 82 | +$Bw.Write($PngBytes) |
| 83 | +$Bw.Close() |
| 84 | +[System.IO.File]::WriteAllBytes($IcoPath, $Ms.ToArray()) |
| 85 | +$Ms.Dispose() |
| 86 | +
|
| 87 | +$BrowserPath = '${psQuote(target.path)}' |
| 88 | +if (-not (Test-Path $BrowserPath)) { throw "Browser not found: $BrowserPath" } |
| 89 | +
|
| 90 | +$ShortcutDir = [Environment]::GetFolderPath('Programs') |
| 91 | +$ShortcutPath = Join-Path $ShortcutDir '${psQuote(safeName)}.lnk' |
| 92 | +
|
| 93 | +$WshShell = New-Object -ComObject WScript.Shell |
| 94 | +$Shortcut = $WshShell.CreateShortcut($ShortcutPath) |
| 95 | +$Shortcut.TargetPath = $BrowserPath |
| 96 | +$Shortcut.Arguments = '${psQuote(commandArgs)}' |
| 97 | +$Shortcut.IconLocation = $IcoPath |
| 98 | +$Shortcut.Description = 'Generated by FTWA (https://ftwa.mathix.dev)' |
| 99 | +$Shortcut.WorkingDirectory = (Split-Path $BrowserPath -Parent) |
| 100 | +$Shortcut.Save() |
11 | 101 |
|
12 | | - return ""; |
| 102 | +Write-Host "Shortcut created: $ShortcutPath" |
| 103 | +Write-Host "Search for '${psQuote(website.name)}' in the Start Menu." |
| 104 | +`; |
13 | 105 | } |
0 commit comments