From a3713075dd034fdabb5e3e9ebb81e6393d7ae49b Mon Sep 17 00:00:00 2001 From: Anas Mohamed Date: Thu, 13 May 2021 14:38:53 -0600 Subject: [PATCH] Added ability to save `about:` pages. (#236) Co-authored-by: makeworld --- display/bookmarks.go | 2 +- display/display.go | 2 +- display/download.go | 9 ++++++++- display/handlers.go | 2 +- display/private.go | 2 +- display/subscriptions.go | 2 +- display/tab.go | 10 ++++++---- display/util.go | 2 +- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/display/bookmarks.go b/display/bookmarks.go index e956fb5..e20c223 100644 --- a/display/bookmarks.go +++ b/display/bookmarks.go @@ -152,7 +152,7 @@ func addBookmark() { t := tabs[curTab] p := t.page - if !t.hasContent() { + if !t.hasContent() || t.isAnAboutPage() { // It's an about: page, or a malformed one return } diff --git a/display/display.go b/display/display.go index 4771787..0368de9 100644 --- a/display/display.go +++ b/display/display.go @@ -155,7 +155,7 @@ func Init(version, commit, builtBy string) { reset() return } - if query[0] == '.' && tabs[tab].hasContent() { + if query[0] == '.' && tabs[tab].hasContent() && !tabs[tab].isAnAboutPage() { // Relative url current, err := url.Parse(tabs[tab].page.URL) if err != nil { diff --git a/display/download.go b/display/download.go index 28fa9fa..b57a097 100644 --- a/display/download.go +++ b/display/download.go @@ -321,8 +321,14 @@ func downloadPage(p *structs.Page) (string, error) { func downloadNameFromURL(dir, u, ext string) (string, error) { var name string var err error + parsed, _ := url.Parse(u) - if parsed.Path == "" || path.Base(parsed.Path) == "/" { + if strings.HasPrefix(u, "about:") { + name, err = getSafeDownloadName(dir, parsed.Opaque+ext, true, 0) + if err != nil { + return "", err + } + } else if parsed.Path == "" || path.Base(parsed.Path) == "/" { // No file, just the root domain name, err = getSafeDownloadName(dir, parsed.Hostname()+ext, true, 0) if err != nil { @@ -340,6 +346,7 @@ func downloadNameFromURL(dir, u, ext string) (string, error) { return "", err } } + return filepath.Join(dir, name), nil } diff --git a/display/handlers.go b/display/handlers.go index 8f63370..8deaf90 100644 --- a/display/handlers.go +++ b/display/handlers.go @@ -180,7 +180,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) { t.mode = tabModeDone go func(p *structs.Page) { - if b && t.hasContent() && viper.GetBool("subscriptions.popup") { + if b && t.hasContent() && !t.isAnAboutPage() && viper.GetBool("subscriptions.popup") { // The current page might be an untracked feed, and the user wants // to be notified in such cases. diff --git a/display/private.go b/display/private.go index 90cd8bd..ecf4491 100644 --- a/display/private.go +++ b/display/private.go @@ -23,7 +23,7 @@ func followLink(t *tab, prev, next string) { return } - if t.hasContent() { + if t.hasContent() && !t.isAnAboutPage() { nextURL, err := resolveRelLink(t, prev, next) if err != nil { Error("URL Error", err.Error()) diff --git a/display/subscriptions.go b/display/subscriptions.go index b582dd9..30e76d8 100644 --- a/display/subscriptions.go +++ b/display/subscriptions.go @@ -304,7 +304,7 @@ func addSubscription() { t := tabs[curTab] p := t.page - if !t.hasContent() { + if !t.hasContent() || t.isAnAboutPage() { // It's an about: page, or a malformed one return } diff --git a/display/tab.go b/display/tab.go index cbbe593..dbe75a9 100644 --- a/display/tab.go +++ b/display/tab.go @@ -213,7 +213,7 @@ func (t *tab) pageDown() { } // hasContent returns false when the tab's page is malformed, -// has no content or URL, or if it's an 'about:' page. +// has no content or URL. func (t *tab) hasContent() bool { if t.page == nil || t.view == nil { return false @@ -221,15 +221,17 @@ func (t *tab) hasContent() bool { if t.page.URL == "" { return false } - if strings.HasPrefix(t.page.URL, "about:") { - return false - } if t.page.Content == "" { return false } return true } +// isAnAboutPage returns true when the tab's page is an about page +func (t *tab) isAnAboutPage() bool { + return strings.HasPrefix(t.page.URL, "about:") +} + // applyHorizontalScroll handles horizontal scroll logic including left margin resizing, // see #197 for details. Use applyScroll instead. // diff --git a/display/util.go b/display/util.go index e0949ab..f91f2a8 100644 --- a/display/util.go +++ b/display/util.go @@ -90,7 +90,7 @@ func textWidth() int { // It also returns an error if it could not resolve the links, which should be displayed // to the user. func resolveRelLink(t *tab, prev, next string) (string, error) { - if !t.hasContent() { + if !t.hasContent() || t.isAnAboutPage() { return next, nil }