Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

  1. #!/bin/bash
  2. # surf_linkselect.sh:
  3. # Usage: curl somesite.com | surf_linkselect [SURFWINDOWID] [PROMPT]
  4. # Deps: xmllint, dmenu
  5. # Info:
  6. # Designed to be used w/ surf externalpipe patch. Enables keyboard-only
  7. # link selection via dmenu. Given HTML stdin, extracts links one per line
  8. # Selected link is normalized based on current URI and printed to STDOUT.
  9. # Pipe the result to a new surf or xprop _SURF_URI accordingly.
  10. SURF_WINDOW="${1:-$(xprop -root | sed -n '/^_NET_ACTIVE_WINDOW/ s/.* //p')}"
  11. DMENU_PROMPT="${2:-Link}"
  12. function dump_links_with_titles() {
  13. awk '{
  14. input = $0;
  15. $0 = input;
  16. gsub("<[^>]*>", "");
  17. gsub(/[ ]+/, " ");
  18. gsub("&amp;", "\\&");
  19. gsub("&lt;", "<");
  20. gsub("&gt;", ">");
  21. $1 = $1;
  22. title = ($0 == "" ? "None" : $0);
  23. $0 = input;
  24. match($0, /\<[ ]*[aA][^>]* [hH][rR][eE][fF]=["]([^"]+)["]/, linkextract);
  25. $0 = linkextract[1];
  26. gsub(/^[ \t]+/,"");
  27. gsub(/[ \t]+$/,"");
  28. gsub("[ ]", "%20");
  29. link = $0;
  30. if (link != "") {
  31. print title ": " link;
  32. }
  33. }'
  34. }
  35. function link_normalize() {
  36. URI=$1
  37. awk -v uri=$URI '{
  38. gsub("&amp;", "\\&");
  39. if ($0 ~ /^https?:\/\// || $0 ~ /^\/\/.+$/) {
  40. print $0;
  41. } else if ($0 ~/^#/) {
  42. gsub(/[#?][^#?]+/, "", uri);
  43. print uri $0;
  44. } else if ($0 ~/^\//) {
  45. split(uri, uri_parts, "/");
  46. print uri_parts[3] $0;
  47. } else {
  48. gsub(/[#][^#]+/, "", uri);
  49. uri_parts_size = split(uri, uri_parts, "/");
  50. delete uri_parts[uri_parts_size];
  51. for (v in uri_parts) {
  52. uri_pagestripped = uri_pagestripped uri_parts[v] "/"
  53. }
  54. print uri_pagestripped $0;
  55. }
  56. }'
  57. }
  58. function link_select() {
  59. tr '\n\r' ' ' |
  60. xmllint --html --xpath "//a" - |
  61. dump_links_with_titles |
  62. awk '!x[$0]++' |
  63. # sort | uniq
  64. dmenu -p "$DMENU_PROMPT" -l 10 -i -w $SURF_WINDOW |
  65. awk -F' ' '{print $NF}' |
  66. link_normalize $(xprop -id $SURF_WINDOW _SURF_URI | cut -d '"' -f 2)
  67. }
  68. link_select