package gui
import (
"crypto/sha256"
"encoding/base64"
"net/url"
"os"
"strings"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/plugin"
)
// A plugin's page is framed with sandbox="allow-scripts", so its origin is
// opaque and every file it loads is a cross-site request that carries no
// cookie of SameSite=Lax (Cloudflare Access's CF_Authorization is one).
// Behind a proxy that signs readers in by cookie, the page's HTML came
// (the frame's own navigation is same-site) but its stylesheet and
// scripts were redirected to the sign-in page, and the page said it
// "didn't answer" (xuanwo on X, Day card).
//
// A page served to a browser therefore comes as one document: its own
// classic scripts and stylesheets are written into the HTML, the scripts
// allowed by their hashes in the page's policy. Loads the page makes
// later (a script it adds) ask the window for the file (pagebridge.js's
// asset call), which carries the cookie.
var (
inlineScript = lazyre.New(`(?is)`)
inlineLink = lazyre.New(`(?is)]*>`)
attrRel = lazyre.New(`(?is)\brel\s*=\s*["']?\s*stylesheet\b`)
attrHref = lazyre.New(`(?is)\bhref\s*=\s*(?:"([^"]*)"|'([^']*)')`)
attrType = lazyre.New(`(?is)\btype\s*=\s*["']?\s*([^"'\s>]*)`)
attrDefer = lazyre.New(`(?is)\bdefer\b`)
bodyClose = lazyre.New(`(?i)