e6afa91e09
kage renders every page in headless Chrome, snapshots the final DOM, strips all JavaScript, and localises CSS, images, and fonts so a site can be browsed offline as a plain folder of files. The engine is split into small packages: urlx deterministic URL to local-path mapping and scope rules sanitize remove scripts, on* handlers, and javascript: URLs asset rewrite HTML and CSS references, download assets browser headless Chrome pool over the DevTools protocol robots robots.txt matcher clone the orchestrator: a polite resumable breadth-first crawl The cli package wires a cobra and fang command surface with two commands, clone and serve. Every pure package has table tests; the browser and clone packages add Chrome-driven end-to-end tests that skip when no browser is present or under -short. CI runs gofmt, vet, build, race tests, golangci-lint, govulncheck, and a tidy check on Linux and macOS. A goreleaser config fans one tag out to archives, deb/rpm/apk, a Chromium-bundled GHCR image, and the package managers. A tago docs site builds to Pages and Cloudflare.
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package browser
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLookChromeReadsEnv(t *testing.T) {
|
|
t.Setenv("KAGE_CHROME", "/custom/chrome")
|
|
bin, ok := LookChrome()
|
|
if !ok || bin != "/custom/chrome" {
|
|
t.Fatalf("LookChrome() = %q, %v; want /custom/chrome, true", bin, ok)
|
|
}
|
|
}
|
|
|
|
func TestRenderCapturesFinalDOM(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("render test drives Chrome; skipped under -short")
|
|
}
|
|
if _, ok := LookChrome(); !ok {
|
|
t.Skip("no Chrome/Chromium found; skipping render test")
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
// A page whose visible content is built by JavaScript: only a real
|
|
// browser render captures the injected node.
|
|
_, _ = w.Write([]byte(`<!doctype html><html><body>
|
|
<div id="app"></div>
|
|
<script>document.getElementById("app").textContent = "rendered-by-js";</script>
|
|
</body></html>`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := New(Options{Headless: true, Workers: 1, Settle: 300 * time.Millisecond, RenderTimeout: 20 * time.Second})
|
|
defer func() { _ = p.Close() }()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
res, err := p.Render(ctx, srv.URL)
|
|
if err != nil {
|
|
t.Fatalf("render: %v", err)
|
|
}
|
|
if !strings.Contains(res.HTML, "rendered-by-js") {
|
|
t.Errorf("render did not capture the JS-built DOM:\n%s", res.HTML)
|
|
}
|
|
}
|