Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Added loadURL and setProxy functionality #307

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion astilectron.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Versions
const (
DefaultAcceptTCPTimeout = 30 * time.Second
DefaultVersionAstilectron = "0.44.0"
DefaultVersionAstilectron = "0.46.0"
DefaultVersionElectron = "11.1.0"
)

Expand Down
4 changes: 4 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type Event struct {
Username string `json:"username,omitempty"`
WindowID string `json:"windowId,omitempty"`
WindowOptions *WindowOptions `json:"windowOptions,omitempty"`
Proxy *WindowProxyOptions `json:"proxy,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this field upwards so that fields are sorted alphabetically?

Scheme string `json:"scheme,omitempty"`
MimeType string `json:"mimeType,omitempty"`
Data string `json:"data,omitempty"`
}

// EventAuthInfo represents an event auth info
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"

"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
"github.com/true-zero/go-astilectron"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/asticode/go-astilectron
module github.com/true-zero/go-astilectron

go 1.13

Expand Down
2 changes: 1 addition & 1 deletion paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (p *Paths) initDataDirectory(dataDirectoryPath, appName string) (err error)

// AstilectronDownloadSrc returns the download URL of the (currently platform-independent) astilectron zip file
func AstilectronDownloadSrc(versionAstilectron string) string {
return fmt.Sprintf("https://github.com/asticode/astilectron/archive/v%s.zip", versionAstilectron)
return "https://codeload.github.com/true-zero/astilectron/zip/v" + versionAstilectron
}

// ElectronDownloadSrc returns the download URL of the platform-dependant electron zipfile
Expand Down
147 changes: 103 additions & 44 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,60 @@ import (

// Window event names
const (
EventNameWebContentsEventLogin = "web.contents.event.login"
EventNameWebContentsEventLoginCallback = "web.contents.event.login.callback"
EventNameWindowCmdBlur = "window.cmd.blur"
EventNameWindowCmdCenter = "window.cmd.center"
EventNameWindowCmdClose = "window.cmd.close"
EventNameWindowCmdCreate = "window.cmd.create"
EventNameWindowCmdDestroy = "window.cmd.destroy"
EventNameWindowCmdFocus = "window.cmd.focus"
EventNameWindowCmdHide = "window.cmd.hide"
EventNameWindowCmdLog = "window.cmd.log"
EventNameWindowCmdMaximize = "window.cmd.maximize"
eventNameWindowCmdMessage = "window.cmd.message"
eventNameWindowCmdMessageCallback = "window.cmd.message.callback"
EventNameWindowCmdMinimize = "window.cmd.minimize"
EventNameWindowCmdMove = "window.cmd.move"
EventNameWindowCmdResize = "window.cmd.resize"
EventNameWindowCmdSetBounds = "window.cmd.set.bounds"
EventNameWindowCmdRestore = "window.cmd.restore"
EventNameWindowCmdShow = "window.cmd.show"
EventNameWindowCmdUnmaximize = "window.cmd.unmaximize"
EventNameWindowCmdUpdateCustomOptions = "window.cmd.update.custom.options"
EventNameWindowCmdWebContentsCloseDevTools = "window.cmd.web.contents.close.dev.tools"
EventNameWindowCmdWebContentsOpenDevTools = "window.cmd.web.contents.open.dev.tools"
EventNameWindowCmdWebContentsExecuteJavaScript = "window.cmd.web.contents.execute.javascript"
EventNameWindowEventBlur = "window.event.blur"
EventNameWindowEventClosed = "window.event.closed"
EventNameWindowEventDidFinishLoad = "window.event.did.finish.load"
EventNameWindowEventFocus = "window.event.focus"
EventNameWindowEventHide = "window.event.hide"
EventNameWindowEventMaximize = "window.event.maximize"
eventNameWindowEventMessage = "window.event.message"
eventNameWindowEventMessageCallback = "window.event.message.callback"
EventNameWindowEventMinimize = "window.event.minimize"
EventNameWindowEventMove = "window.event.move"
EventNameWindowEventReadyToShow = "window.event.ready.to.show"
EventNameWindowEventResize = "window.event.resize"
EventNameWindowEventRestore = "window.event.restore"
EventNameWindowEventShow = "window.event.show"
EventNameWindowEventUnmaximize = "window.event.unmaximize"
EventNameWindowEventUnresponsive = "window.event.unresponsive"
EventNameWindowEventDidGetRedirectRequest = "window.event.did.get.redirect.request"
EventNameWindowEventWebContentsExecutedJavaScript = "window.event.web.contents.executed.javascript"
EventNameWindowEventWillNavigate = "window.event.will.navigate"
EventNameWindowEventUpdatedCustomOptions = "window.event.updated.custom.options"
EventNameWebContentsEventLogin = "web.contents.event.login"
EventNameWebContentsEventLoginCallback = "web.contents.event.login.callback"
EventNameWebContentsEventInterceptStringProtocol = "web.contents.event.intercept.string.protocol"
EventNameWebContentsEventInterceptStringProtocolCallback = "web.contents.event.intercept.string.protocol.callback"
EventNameWindowCmdBlur = "window.cmd.blur"
EventNameWindowCmdCenter = "window.cmd.center"
EventNameWindowCmdClose = "window.cmd.close"
EventNameWindowCmdCreate = "window.cmd.create"
EventNameWindowCmdDestroy = "window.cmd.destroy"
EventNameWindowCmdFocus = "window.cmd.focus"
EventNameWindowCmdHide = "window.cmd.hide"
EventNameWindowCmdLog = "window.cmd.log"
EventNameWindowCmdMaximize = "window.cmd.maximize"
eventNameWindowCmdMessage = "window.cmd.message"
eventNameWindowCmdMessageCallback = "window.cmd.message.callback"
EventNameWindowCmdMinimize = "window.cmd.minimize"
EventNameWindowCmdMove = "window.cmd.move"
EventNameWindowCmdResize = "window.cmd.resize"
EventNameWindowCmdSetBounds = "window.cmd.set.bounds"
EventNameWindowCmdRestore = "window.cmd.restore"
EventNameWindowCmdShow = "window.cmd.show"
EventNameWindowCmdUnmaximize = "window.cmd.unmaximize"
EventNameWindowCmdUpdateCustomOptions = "window.cmd.update.custom.options"
EventNameWindowCmdWebContentsCloseDevTools = "window.cmd.web.contents.close.dev.tools"
EventNameWindowCmdWebContentsOpenDevTools = "window.cmd.web.contents.open.dev.tools"
EventNameWindowCmdWebContentsExecuteJavaScript = "window.cmd.web.contents.execute.javascript"
EventNameWindowCmdWebContentsSetProxy = "window.cmd.web.contents.set.proxy"
EventNameWindowCmdWebContentsInterceptStringProtocol = "window.cmd.web.contents.intercept.string.protocol"
EventNameWindowCmdGetUrl = "window.cmd.get.url"
EventNameWindowCmdLoadURL = "window.cmd.load.url"
EventNameWindowEventBlur = "window.event.blur"
EventNameWindowEventClosed = "window.event.closed"
EventNameWindowEventDidFinishLoad = "window.event.did.finish.load"
EventNameWindowEventFocus = "window.event.focus"
EventNameWindowEventHide = "window.event.hide"
EventNameWindowEventMaximize = "window.event.maximize"
eventNameWindowEventMessage = "window.event.message"
eventNameWindowEventMessageCallback = "window.event.message.callback"
EventNameWindowEventMinimize = "window.event.minimize"
EventNameWindowEventMove = "window.event.move"
EventNameWindowEventReadyToShow = "window.event.ready.to.show"
EventNameWindowEventResize = "window.event.resize"
EventNameWindowEventRestore = "window.event.restore"
EventNameWindowEventShow = "window.event.show"
EventNameWindowEventUnmaximize = "window.event.unmaximize"
EventNameWindowEventUnresponsive = "window.event.unresponsive"
EventNameWindowEventDidGetRedirectRequest = "window.event.did.get.redirect.request"
EventNameWindowEventWebContentsExecutedJavaScript = "window.event.web.contents.executed.javascript"
EventNameWindowEventWebContentsSetProxy = "window.event.web.contents.set.proxy"
EventNameWindowEventWebContentsInterceptStringProtocol = "window.event.web.contents.intercept.string.protocol"
EventNameWindowEventWillNavigate = "window.event.will.navigate"
EventNameWindowEventUpdatedCustomOptions = "window.event.updated.custom.options"
EventNameWindowLoadedURL = "window.event.loaded.url"
EventNameWindowGetUrl = "window.event.get.url"
)

// Title bar styles
Expand Down Expand Up @@ -423,6 +433,28 @@ func (w *Window) OnLogin(fn func(i Event) (username, password string, err error)
})
}

func (w *Window) OnInterceptStringProtocol(scheme string, fn func(i Event) (string, string, bool)) (err error) {
// Setup the event to handle the callback
w.On(EventNameWebContentsEventInterceptStringProtocol, func(i Event) (deleteListener bool) {
// Get mime type, data and whether the listener should be deleted.
mimeType, data, deleteListener := fn(i)

// Send message back
if err = w.w.write(Event{CallbackID: i.CallbackID, Name: EventNameWebContentsEventInterceptStringProtocolCallback, TargetID: w.id, MimeType: mimeType, Data: data}); err != nil {
w.l.Error(fmt.Errorf("writing intercept string protocol callback message failed: %w", err))
return
}

return
})

if err = w.ctx.Err(); err != nil {
return
}
_, err = synchronousEvent(w.ctx, w, w.w, Event{Name: EventNameWindowCmdWebContentsInterceptStringProtocol, TargetID: w.id, Scheme: scheme}, EventNameWindowEventWebContentsInterceptStringProtocol)
return
}

// ListenerMessage represents a message listener executed when receiving a message from the JS
type ListenerMessage func(m *EventMessage) (v interface{})

Expand Down Expand Up @@ -534,6 +566,24 @@ func (w *Window) Unmaximize() (err error) {
return
}

// Loads the url
func (w *Window) LoadURL(url string) (err error) {
if err = w.ctx.Err(); err != nil {
return
}
_, err = synchronousEvent(w.ctx, w, w.w, Event{Name: EventNameWindowCmdLoadURL, TargetID: w.id, URL: url}, EventNameWindowLoadedURL)
return
}

// Sets the proxy
func (w *Window) SetProxy(proxy *WindowProxyOptions) (err error) {
if err = w.ctx.Err(); err != nil {
return
}
_, err = synchronousEvent(w.ctx, w, w.w, Event{Name: EventNameWindowCmdWebContentsSetProxy, TargetID: w.id, Proxy: proxy}, EventNameWindowEventWebContentsSetProxy)
return
}

// UpdateCustomOptions updates the window custom options
func (w *Window) UpdateCustomOptions(o WindowCustomOptions) (err error) {
if err = w.ctx.Err(); err != nil {
Expand All @@ -545,3 +595,12 @@ func (w *Window) UpdateCustomOptions(o WindowCustomOptions) (err error) {
_, err = synchronousEvent(w.ctx, w, w.w, Event{WindowOptions: w.o, Name: EventNameWindowCmdUpdateCustomOptions, TargetID: w.id}, EventNameWindowEventUpdatedCustomOptions)
return
}

// Loads the url
func (w *Window) GetUrl() (e Event, err error) {
if err = w.ctx.Err(); err != nil {
return
}
e, err = synchronousEvent(w.ctx, w, w.w, Event{Name: EventNameWindowCmdGetUrl, TargetID: w.id}, EventNameWindowGetUrl)
return
}