Skip to content

Instantly share code, notes, and snippets.

@alaingilbert
Last active March 15, 2023 09:29
Show Gist options
  • Save alaingilbert/f5b6777f59e0bce66bf17f8b98fb3ee6 to your computer and use it in GitHub Desktop.
Save alaingilbert/f5b6777f59e0bce66bf17f8b98fb3ee6 to your computer and use it in GitHub Desktop.
chrome webstore downloader
package main
import (
"encoding/binary"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
)
var webstoreRgx = regexp.MustCompile(`https://chrome.google.com/webstore/detail/([^/]+)/([^/]+)`)
func main() {
if len(os.Args) == 1 {
fmt.Println("webstore url missing")
return
}
chromeWebstoreURL := os.Args[1] // https://chrome.google.com/webstore/detail/extension-name/extension-id
m := webstoreRgx.FindStringSubmatch(chromeWebstoreURL)
if len(m) != 3 {
fmt.Println("invalid webstore url")
return
}
extensionName, extensionID := m[1], m[2]
if err := downloadExt(extensionName, extensionID); err != nil {
fmt.Println(err)
return
}
}
func downloadExt(extensionName, extensionID string) error {
downloadLink := buildDownloadLink(extensionID)
resp, err := http.Get(downloadLink)
if err != nil {
return err
}
defer resp.Body.Close()
parse(resp.Body)
out, err := os.Create(extensionName + `.zip`)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func buildDownloadLink(extensionID string) string {
v := url.Values{
"response": {"redirect"},
"prodversion": {"49.0"},
"acceptformat": {"crx3"},
"x": {"id=" + extensionID + "&installsource=ondemand&uc"},
}
return "https://clients2.google.com/service/update2/crx?" + v.Encode()
}
func parse(reader io.Reader) {
var magic, version, headerLength uint32
_ = binary.Read(reader, binary.BigEndian, &magic)
_ = binary.Read(reader, binary.BigEndian, &version)
_ = binary.Read(reader, binary.LittleEndian, &headerLength)
if magic != 0x43723234 { // Cr24
return // Magic is broken
}
buf := make([]byte, headerLength)
_, _ = reader.Read(buf)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment