subscription_bookmarklet.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "net/http"
  6. "regexp"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/http/request"
  9. "miniflux.app/v2/internal/http/response"
  10. "miniflux.app/v2/internal/ui/form"
  11. "miniflux.app/v2/internal/ui/session"
  12. "miniflux.app/v2/internal/ui/view"
  13. )
  14. // Best effort url extraction regexp
  15. var urlRe = regexp.MustCompile(`(?i)(?:https?://)?[0-9a-z.]+[.][a-z]+(?::[0-9]+)?(?:/[^ ]+|/)?`)
  16. func (h *handler) bookmarklet(w http.ResponseWriter, r *http.Request) {
  17. user, err := h.store.UserByID(request.UserID(r))
  18. if err != nil {
  19. response.HTMLServerError(w, r, err)
  20. return
  21. }
  22. categories, err := h.store.Categories(user.ID)
  23. if err != nil {
  24. response.HTMLServerError(w, r, err)
  25. return
  26. }
  27. bookmarkletURL := request.QueryStringParam(r, "uri", "")
  28. // Extract URL from text supplied by Web Share Target API.
  29. //
  30. // This is because Android intents have no concept of URL, so apps
  31. // just shove a URL directly into the EXTRA_TEXT intent field.
  32. //
  33. // See https://bugs.chromium.org/p/chromium/issues/detail?id=789379.
  34. text := request.QueryStringParam(r, "text", "")
  35. if text != "" && bookmarkletURL == "" {
  36. bookmarkletURL = urlRe.FindString(text)
  37. }
  38. sess := session.New(h.store, request.SessionID(r))
  39. view := view.New(h.tpl, r, sess)
  40. view.Set("form", form.SubscriptionForm{URL: bookmarkletURL})
  41. view.Set("categories", categories)
  42. view.Set("menu", "feeds")
  43. view.Set("user", user)
  44. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  45. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  46. view.Set("defaultUserAgent", config.Opts.HTTPClientUserAgent())
  47. view.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyURLConfigured())
  48. response.HTML(w, r, view.Render("add_subscription"))
  49. }