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