Skip to content

Instantly share code, notes, and snippets.

@natereprogle
Last active April 22, 2025 22:49
Show Gist options
  • Save natereprogle/91c2a55491c9dd4e4c7fbead05193ec7 to your computer and use it in GitHub Desktop.
Save natereprogle/91c2a55491c9dd4e4c7fbead05193ec7 to your computer and use it in GitHub Desktop.
A powershell script for self-hosting a Chrome extension
Add-Type -AssemblyName System.Net.HttpListener
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://localhost:8080/")
$listener.Start()
Write-Host "Listening on http://localhost:8080/"
while ($listener.IsListening) {
$context = $listener.GetContext()
$request = $context.Request
$response = $context.Response
$path = $request.Url.AbsolutePath
switch ($path) {
'/' {
$html = @"
<!DOCTYPE html>
<html>
<head><title>Extension Download</title></head>
<body>
<h1>Download Extension</h1>
<button onclick="window.location.href='/ext.crx'">Download .crx</button>
</body>
</html>
"@
$bytes = [System.Text.Encoding]::UTF8.GetBytes($html)
$response.ContentType = "text/html"
$response.ContentEncoding = [System.Text.Encoding]::UTF8
$response.ContentLength64 = $bytes.Length
$response.OutputStream.Write($bytes, 0, $bytes.Length)
$response.OutputStream.Close()
}
'/update.xml' {
$xml = @"
<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='aaabbbcccdddeeefff'>
<updatecheck codebase='http://localhost:8080/ext.crx' version='1.0' />
</app>
</gupdate>
"@
$bytes = [System.Text.Encoding]::UTF8.GetBytes($xml)
$response.ContentType = "text/xml"
$response.ContentEncoding = [System.Text.Encoding]::UTF8
$response.ContentLength64 = $bytes.Length
$response.OutputStream.Write($bytes, 0, $bytes.Length)
$response.OutputStream.Close()
}
'/ext.crx' {
$filePath = "C:\Path\To\Your\Extension.crx"
if (Test-Path $filePath) {
$bytes = [System.IO.File]::ReadAllBytes($filePath)
$response.ContentType = "application/x-chrome-extension"
$response.ContentLength64 = $bytes.Length
$response.OutputStream.Write($bytes, 0, $bytes.Length)
} else {
$response.StatusCode = 404
$error = [System.Text.Encoding]::UTF8.GetBytes("File not found")
$response.OutputStream.Write($error, 0, $error.Length)
}
$response.OutputStream.Close()
}
default {
$response.StatusCode = 404
$msg = [System.Text.Encoding]::UTF8.GetBytes("Not found")
$response.OutputStream.Write($msg, 0, $msg.Length)
$response.OutputStream.Close()
}
}
}
$listener.Stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment