The owner: Magpie Browser "老是会代替掉用户正在浏览的网页". A call that
named no tab, from a session with none, took the tab the user had in
front and navigated it away under them. Now:
- such a call opens a new tab of the session's (about:blank), behind the
shown one, in its Magpie tab group, in the window used last, or an
unfocused window of its own when none is open (open() in background.js);
- browser_tabs new / tab_new open behind too; front: true shows the tab,
for when the user asked to see it (background is still taken, and is
now the default);
- a screenshot of a tab behind shows it for the picture, then gives the
window back the tab the user had in front;
- a link with target=_blank clicked in a tab behind: Chrome shows the new
tab and names the user's shown tab as its opener. It is still followed,
grouped in the session's group, and the user's tab is put back in front.
- the tool descriptions say so; the user's tabs are acted in only when a
call names one.
Extension 0.1.4. When an older one connects, the gateway writes this
magpie's over its folders (browserext.Refresh; only folders already
written, never a newer one's: browserext.Older), so the browser picks it
up when it starts again or on the extension's reload in
chrome://extensions. Before, only opening Library's Browser page did. The
dir helpers moved from gui to browserext for it.
Tried and dropped: telling the extension to chrome.runtime.reload()
itself. In Chrome for Testing 1234 on mote an unpacked extension that
calls it never comes back (headless and headful, --load-extension and
CDP Extensions.loadUnpacked, with an onInstalled listener), which would
leave the user with none until a restart.
Run on mote at 1e1068c86 + this:
- MAGPIE_BROWSER_CHROME=Chrome for Testing 1234, go test -run Browser -v
./internal/gateway: ok, 16 passed, including the new
TestBrowserLeavesTheUsersTabAlone (navigate, link to a new tab, tab new,
screenshot, tab_new: the user's about:blank tab stays + and unchanged,
the agent's in [Magpie]) and TestBrowserOlderExtensionsFolderWritten.
- go test ./internal/browserext (TestOlder, TestRefreshWritesOnlyWrittenFolders): ok.
- go test ./internal/gui: one red, TestMainWindowCorners/macOS_26_SDK,
filed as #3949 (macOS 27 on mote), not touched here.
- gofmt -l; GOOS=darwin/linux/windows go vet of the three packages: clean.
Broken by hand: TestBrowserLeavesTheUsersTabAlone with origin/main's
background.js is red ("navigated the user's tab"). The link case was
found by TestBrowserToolsInARealBrowser going red on the first version.
Not tried: the user's own Chrome (headful Chrome with real sign-ins),
Edge and Arc; Windows and Linux browsers.
109 lines
3.5 KiB
Go
109 lines
3.5 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"
|
|
)
|
|
|
|
// 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.
|
|
|
|
// the folders it is written to (browserext.Dir, and OldDir while a
|
|
// browser still loads it from there)
|
|
func browserExtDir() string { return browserext.Dir() }
|
|
func browserExtOldDir() string { return browserext.OldDir() }
|
|
|
|
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)
|
|
}
|