Skip to content

Commit

Permalink
Open external links in new browser window
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzen committed Sep 25, 2023
1 parent 584f037 commit dbba1bf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
26 changes: 26 additions & 0 deletions client/cmd/dexc-desktop/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,31 @@ func closeAllWindows() {
}
}

// bindJSFunctions exports functions callable in the frontend
func bindJSFunctions(w webview.WebView) {
w.Bind("isWebview", func() bool {
return true
})

w.Bind("openUrl", func(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
break
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
break
case "darwin":
err = exec.Command("open", url).Start()
break
}
if err != nil {
log.Errorf(err.Error())
}
})
}

func runWebview(url string) {
w := webview.New(true)
defer w.Destroy()
Expand All @@ -389,6 +414,7 @@ func runWebview(url string) {

w.SetSize(width, height, webview.HintNone)
w.Navigate(url)
bindJSFunctions(w)
w.Run()
}

Expand Down
4 changes: 2 additions & 2 deletions client/webserver/site/src/html/wallets.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@
<td data-tmpl="status"></td>
<td data-tmpl="detailsLink" class="pointer hoverbg">
<span class="mono">
<span data-tmpl="hashStart"></span>…<span data-tmpl="hashEnd"></span>
<a href="" data-tmpl="detailsLinkUrl" target="_blank"><span data-tmpl="hashStart"></span>…<span data-tmpl="hashEnd"></span></a>
</span>
<span class="ico-open ms-1"></span>
</td>
Expand Down Expand Up @@ -812,7 +812,7 @@
<div class="flex-center flex-grow-1 pe-3">
<div data-tmpl="value" class="flex-center fs16 p-2"></div>
<div data-tmpl="hash" class="hashwrap fs14 p-1"></div>
<div data-tmpl="explorerLink" class="p-2 hoverbg pointer"><span class="fs16 ico-open"></span></div>
<a href="" target="_blank" data-tmpl="explorerLink" class="p-2 hoverbg pointer"><span class="fs16 ico-open"></span></a>
</div>
<div class="d-flex align-items-stretch">
<div class="flex-center flex-column pe-2">
Expand Down
22 changes: 22 additions & 0 deletions client/webserver/site/src/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default class Application {
popupNotes: HTMLElement
popupTmpl: HTMLElement
noteReceivers: Record<string, (n: CoreNote) => void>[]
isWebview: boolean

constructor () {
this.notes = []
Expand Down Expand Up @@ -151,6 +152,9 @@ export default class Application {

// use user current locale set by backend
intl.setLocale()

const w = window as any
this.isWebview = (w.isWebview !== undefined)
}

/**
Expand Down Expand Up @@ -276,6 +280,11 @@ export default class Application {

// Bind the tooltips.
this.bindTooltips(this.main)

if (this.isWebview) {
// Bind webview URL handlers
this.bindUrlHandlers(this.main)
}
}

bindTooltips (ancestor: HTMLElement) {
Expand All @@ -297,6 +306,19 @@ export default class Application {
})
}

bindUrlHandlers (ancestor: HTMLElement) {
ancestor.addEventListener('click', function (event: MouseEvent) {
if (event === null || event.target === null) return

const target = event.target as HTMLElement
if (target.matches('a[target=_blank]')) {
const url = target.getAttribute('href')
const w = window as any
w.openUrl(url)
}
}, false)
}

/* attachHeader attaches the header element, which unlike the main element,
* isn't replaced during page navigation.
*/
Expand Down
4 changes: 2 additions & 2 deletions client/webserver/site/src/js/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ export default class WalletsPage extends BasePage {
tmpl.status.textContent = intl.prep(ticketStatusTranslationKeys[status])
tmpl.hashStart.textContent = tx.hash.slice(0, 6)
tmpl.hashEnd.textContent = tx.hash.slice(-6)
Doc.bind(tmpl.detailsLink, 'click', () => window.open(coinLink(tx.hash), '_blank'))
tmpl.detailsLinkUrl.setAttribute('href', coinLink(tx.hash))
}
}

Expand Down Expand Up @@ -1186,7 +1186,7 @@ export default class WalletsPage extends BasePage {
if (tspend.value > 0) tmpl.value.textContent = Doc.formatFourSigFigs(tspend.value / ui.conventional.conversionFactor)
else Doc.hide(tmpl.value)
tmpl.hash.textContent = tspend.hash
Doc.bind(tmpl.explorerLink, 'click', () => window.open(coinLink(tspend.hash), '_blank'))
tmpl.explorerLink.setAttribute('href', coinLink(tspend.hash))
}

const setTKeyPolicy = async (key: string, policy: string) => {
Expand Down

0 comments on commit dbba1bf

Please sign in to comment.