~mil/sxmo-utils

ref: dbc3b1ab509aab5f1db72c5cece2ee2a6eb521e8 sxmo-utils/scripts/core/sxmo_surf_linkselect.sh -rwxr-xr-x 1.9 KiB
dbc3b1abStacy Harper Allow the sendnewtextmenu to be used with a number as arg 1 year, 4 months ago
                                                                                
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
77
78
79
#!/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}"

dump_links_with_titles() {
	awk '{
		input = $0;

		$0 = input;
		gsub("<[^>]*>", "");
		gsub(/[ ]+/, " ");
		gsub("&amp;", "\\&");
		gsub("&lt;", "<");
		gsub("&gt;", ">");
		$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;
		}
	}'
}

link_normalize() {
	URI=$1
	awk -v uri="$URI" '{
		gsub("&amp;", "\\&");

		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;
		}
	}'
}

link_select() {
	tr '\n\r' ' ' |
		xmllint --html --xpath "//a" - |
		dump_links_with_titles |
		awk '!x[$0]++' |
		# sort | uniq
		dmenu -p "$DMENU_PROMPT" -l 10 -i -c |
		awk -F' ' '{print $NF}' |
		link_normalize "$(xprop -id "$SURF_WINDOW" _SURF_URI | cut -d '"' -f 2)"
}

sxmo_keyboard.sh open
VAL="$(link_select)"
sxmo_keyboard.sh close
echo "$VAL"