-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll page links into the new window
56 lines (56 loc) · 1.82 KB
/
All page links into the new window
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
javascript: (function() {
let num = 0, newWin = window.open().document, lnk = document.links,
href, text, aria, title, isAccessible, isValid,
error1 = '<b>????????href is not a valid URL</b>????????',
error2 = '<b>????????inaccessible link????????</b>';
function htmlEscape(s) {
s = s.replace(/&/g, '&').replace(/>/g, '>').replace(/</g, '<');
return s;
}
function isURL(s) {
try { return Boolean(new URL(s)); } catch (e) { return false; }
}
newWin.writeln('Links on <a href=\'' + location.href + '\'>' + location.href + '</a><br><hr>');
for (let i = 0; i < lnk.length; ++i) {
isAccessible = 1;
isValid = 1;
href = lnk[i].href;
text = lnk[i].innerText;
aria = lnk[i].getAttribute('aria-label');
title = lnk[i].getAttribute('title');
newWin.writeln(++num + '. <a href=\'');
if (!href.startsWith('javascript:')) {
newWin.writeln(htmlEscape(href) + '\'>');
} else {
newWin.writeln('javascript:\'>');
}
if (!isURL(href)) {
isValid = 0
}
if (lnk[i].innerHTML) {
if (text.length > 1) {
newWin.writeln(text.substring(0, 100));
} else if (aria) {
newWin.writeln(aria);
} else if (title) {
newWin.writeln(title);
} else {
isAccessible = 0;
}
} else {
isAccessible = 0;
}
if (!isValid) {
newWin.writeln(error1);
}
if (!isAccessible) {
if (!isValid) {
newWin.writeln('<br>');
}
newWin.writeln(error2);
}
newWin.writeln('</a><br>');
}
newWin.writeln('<hr></body></html>');
newWin.close();
})();