subscription_bookmarklet.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/view"
  12. )
  13. // Best effort url extraction regexp
  14. var urlRe = regexp.MustCompile(`(?i)(?:https?://)?[0-9a-z.]+[.][a-z]+(?::[0-9]+)?(?:/[^ ]+|/)?`)
  15. func (h *handler) bookmarklet(w http.ResponseWriter, r *http.Request) {
  16. user, err := h.store.UserByID(request.UserID(r))
  17. if err != nil {
  18. response.HTMLServerError(w, r, err)
  19. return
  20. }
  21. categories, err := h.store.Categories(user.ID)
  22. if err != nil {
  23. response.HTMLServerError(w, r, err)
  24. return
  25. }
  26. bookmarkletURL := request.QueryStringParam(r, "uri", "")
  27. // Extract URL from text supplied by Web Share Target API.
  28. //
  29. // This is because Android intents have no concept of URL, so apps
  30. // just shove a URL directly into the EXTRA_TEXT intent field.
  31. //
  32. // See https://bugs.chromium.org/p/chromium/issues/detail?id=789379.
  33. text := request.QueryStringParam(r, "text", "")
  34. if text != "" && bookmarkletURL == "" {
  35. bookmarkletURL = urlRe.FindString(text)
  36. }
  37. view := view.New(h.tpl, r)
  38. view.Set("form", form.SubscriptionForm{URL: bookmarkletURL})
  39. view.Set("categories", categories)
  40. view.Set("menu", "feeds")
  41. view.Set("user", user)
  42. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  43. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  44. view.Set("defaultUserAgent", config.Opts.HTTPClientUserAgent())
  45. view.Set("hasProxyConfigured", config.Opts.HasHTTPClientProxyURLConfigured())
  46. response.HTML(w, r, view.Render("add_subscription"))
  47. }