Links clicked in Google Messages open in the default browser, part 3
This commit is contained in:
+88
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
// Links clicked inside Google Messages open in the Windows default browser.
|
||||
//
|
||||
// Edge opens a clicked link in a new Edge window of the Messages profile. To
|
||||
// hand those to the default browser, the Messages window is started with a
|
||||
// debugging port that listens on this PC only (127.0.0.1). A small background
|
||||
// loop looks at the list of open pages, and any page that is not Messages or
|
||||
// a Google sign-in page is opened with the default browser and then closed.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const gmDebugPort = "9341"
|
||||
|
||||
var gmLinkOnce bool
|
||||
|
||||
type gmTarget struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// gmOwnHost reports whether a page belongs to Messages itself (including the
|
||||
// Google sign-in pages used while pairing), which must stay where they are.
|
||||
func gmOwnHost(host string) bool {
|
||||
host = strings.ToLower(host)
|
||||
for _, ok := range []string{"messages.google.com", "accounts.google.com", "gstatic.com", "googleusercontent.com"} {
|
||||
if host == ok || strings.HasSuffix(host, "."+ok) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func gmStartLinkWatch() {
|
||||
if gmLinkOnce {
|
||||
return
|
||||
}
|
||||
gmLinkOnce = true
|
||||
go func() {
|
||||
defer func() { recover() }()
|
||||
runtime.LockOSThread()
|
||||
pCoInitializeEx.Call(0, 2)
|
||||
client := &http.Client{Timeout: 1500 * time.Millisecond, Transport: &http.Transport{DisableKeepAlives: true}}
|
||||
base := "http://127.0.0.1:" + gmDebugPort
|
||||
for {
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
if gmHwnd == 0 {
|
||||
continue
|
||||
}
|
||||
resp, err := client.Get(base + "/json/list")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var ts []gmTarget
|
||||
err = json.NewDecoder(resp.Body).Decode(&ts)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, t := range ts {
|
||||
if t.Type != "page" {
|
||||
continue
|
||||
}
|
||||
u, err := url.Parse(t.URL)
|
||||
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
|
||||
continue
|
||||
}
|
||||
if gmOwnHost(u.Hostname()) {
|
||||
continue
|
||||
}
|
||||
// close the Edge window first so it is on screen as briefly as possible
|
||||
if r, err := client.Get(base + "/json/close/" + url.PathEscape(t.ID)); err == nil {
|
||||
r.Body.Close()
|
||||
}
|
||||
pShellExecute.Call(0, uintptr(unsafe.Pointer(u16("open"))), uintptr(unsafe.Pointer(u16(t.URL))), 0, 0, 1)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user