Files
magpie/internal/gui/browser.go
T
yetone e42ee3b816 library: Magpie Browser's extension sits in ~/Magpie Browser Extension, dragged from the sheet onto chrome://extensions
The owner, on the v0.1.2442 sheet: the extension was written to
~/.config/magpie/browser-extension, a hidden folder no one can find to
choose in Load unpacked.

- Set up writes it to "Magpie Browser Extension" in the home folder
  (browserExtDir), where Finder and Explorer show it, and shows it
  selected in its parent (open -R on the Mac, explorer /select, on
  Windows, the home folder on Linux) rather than opened, to drag from.
- On the Mac the sheet has the folder itself: a press on it that moves
  hands the folder to the system's drag (POST /api/library/browser/drag,
  filedrag_darwin.go: beginDraggingSession from the key window with a
  file URL and the folder's icon, only while the button is still down),
  so it drops onto chrome://extensions as from Finder. Chromium's
  extensions page loads a dropped folder as unpacked
  (drag_and_drop_handler.ts: entry.isDirectory -> loadUnpackedFromDrag),
  with Developer mode on, which step 2 asks for.
- Elsewhere step 3 says to drag the folder from the window that opened,
  or Load unpacked and choose it.
- Old installs: a browser that loaded the hidden folder still does, and
  that folder is kept up to date as long as it has a manifest; the sheet
  offers set up again, into the new folder.

Run at ace58117a on mote: go vet ./internal/gui (darwin), GOOS=linux -tags
nogui and GOOS=windows vet; library-browser-setup and i18n-keys GUI
tests, 46 of 46 (5 languages, Chromium and WebKit, 1100 and 420 wide,
with and without the drag: a click posts no drag, a press that moves
posts one); internal/cli parity tests ok. At df0320156: go test
./internal/gui ./internal/cli, cli ok, gui red only in
TestMainWindowCorners, which is #3949 and red the same at df0320156
without this change.
Not tried: the native drag by hand into a real Chrome (no way to drive
a mouse drag here), and Explorer's /select on the Windows box.
2026-11-01 17:28:15 +08:00

120 lines
4.0 KiB
Go

package gui
import (
"context"
"encoding/json"
"net/http"
"os"
"path/filepath"
"time"
"github.com/yetone/magpie/internal/browserext"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
)
// Magpie Browser's extension, as the Library page sets it up: written to a
// folder the user can see, in their home folder, which they drop on their
// own browser's chrome://extensions (or choose with Load unpacked), and
// connected or not as the gateway says.
// browserExtDir is where the extension is written: in the home folder,
// where Finder and Explorer show it, to drag from or choose.
func browserExtDir() string {
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(settings.Dir(), "browser-extension")
}
return filepath.Join(home, "Magpie Browser Extension")
}
// browserExtOldDir is where v0.1.2442 wrote it, a hidden folder no one
// could find to load; a browser that loaded it from there still does, so
// it is kept up to date too while it is there.
func browserExtOldDir() string { return filepath.Join(settings.Dir(), "browser-extension") }
type browserJSON struct {
Dir string `json:"dir"`
Written bool `json:"written"`
Version string `json:"version"`
Connected bool `json:"connected"`
Extension string `json:"extension,omitempty"` // the connected one's version
Browser string `json:"browser,omitempty"`
// Drag: the page may drag the folder out of the window itself
Drag bool `json:"drag,omitempty"`
}
func browserState(ctx context.Context) browserJSON {
out := browserJSON{Dir: browserExtDir(), Version: browserext.Version(), Drag: canDragFile}
for _, dir := range []string{out.Dir, browserExtOldDir()} {
if _, err := os.Stat(filepath.Join(dir, "manifest.json")); err == nil {
out.Written = out.Written || dir == out.Dir
// kept as this magpie has it, and pointed at its gateway: the
// browser reads the folder again when it starts
browserext.Write(dir, gateway.URL())
}
}
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, gateway.URL()+gateway.BrowserStatusPath, nil)
if err != nil {
return out
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return out
}
defer resp.Body.Close()
var st struct {
Connected bool `json:"connected"`
Extension string `json:"extension"`
Browser string `json:"browser"`
}
if resp.StatusCode == http.StatusOK && json.NewDecoder(resp.Body).Decode(&st) == nil {
out.Connected, out.Extension, out.Browser = st.Connected, st.Extension, st.Browser
}
return out
}
func browserRoutes(mux *http.ServeMux, w Windows) {
mux.HandleFunc("GET /api/library/browser", func(rw http.ResponseWriter, r *http.Request) {
writeJSON(rw, browserState(r.Context()))
})
// Set up: the extension written, and its folder shown selected, to
// drag onto the browser's extensions page or choose with Load unpacked
mux.HandleFunc("POST /api/library/browser/setup", func(rw http.ResponseWriter, r *http.Request) {
dir := browserExtDir()
if err := browserext.Write(dir, gateway.URL()); err != nil {
fail(rw, err)
return
}
st := browserState(r.Context())
var reveal string
if err := revealIn(w, dir); err != nil {
reveal = err.Error()
}
writeJSON(rw, struct {
browserJSON
RevealError string `json:"revealError,omitempty"`
}{st, reveal})
})
// the folder dragged out of the window, as the page's mouse drags it
mux.HandleFunc("POST /api/library/browser/drag", func(rw http.ResponseWriter, r *http.Request) {
dir := browserExtDir()
if err := browserext.Write(dir, gateway.URL()); err != nil {
fail(rw, err)
return
}
writeJSON(rw, map[string]bool{"dragging": dragFile(dir)})
})
}
// revealIn shows path selected in its parent folder where the host can,
// and opens it where it can't.
func revealIn(w Windows, path string) error {
if r, ok := w.(interface{ RevealFolder(string) error }); ok {
return r.RevealFolder(path)
}
return w.OpenFolder(path)
}