-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump-links.user.js
More file actions
126 lines (98 loc) · 2.9 KB
/
Copy pathdump-links.user.js
File metadata and controls
126 lines (98 loc) · 2.9 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ==UserScript==
// @name Dump links
// @namespace Azazar's Scripts
// @version 1.2.0
// @author -
// @description Dump all external links and images
// @grant GM_registerMenuCommand
// @grant GM_setClipboard
// @updateURL https://raw.githubusercontent.com/azazar/userscripts/main/dump-links.user.js
// @downloadURL https://raw.githubusercontent.com/azazar/userscripts/main/dump-links.user.js
// @license MIT
// ==/UserScript==
function sanitizeHost(host) {
if (host.substring(0, 4) === 'www.') {
host = host.substring(4);
}
return host;
}
function endsWith(str, wth) {
return str.length >= wth.length && str.substring(str.length - wth.length) === wth;
}
function isExternal(url) {
let host = sanitizeHost(location.host);
let a = document.createElement('A');
a.href = url;
let link_host = sanitizeHost(a.host);
if (host === link_host || endsWith(host, '.' + link_host) || endsWith(link_host, '.' + host)) {
return false;
}
return true;
}
function getLinks() {
let res = {};
let links = [];
Array.from(document.querySelectorAll('a[href]')).forEach(a => {
if (isExternal(a.href)) {
links.push(a.href);
}
});
res['anchors'] = links;
let ae = document.createElement('A');
links = [];
Array.from(document.querySelectorAll('img[src]')).forEach(a => {
ae.href = a.src;
if (isExternal(ae.href)) {
links.push(ae.href);
}
});
res['images'] = links;
links = [];
Array.from(document.querySelectorAll('*')).forEach(element => {
let backgroundImage = getComputedStyle(element).backgroundImage;
Array.from(backgroundImage.matchAll(/url\(["']?(.+?)["']?\)/g)).forEach(match => {
ae.href = match[1];
if (isExternal(ae.href) && !links.includes(ae.href)) {
links.push(ae.href);
}
});
});
res['cssImages'] = links;
links = [];
Array.from(document.querySelectorAll('video[src]')).forEach(v => {
ae.href = v.src;
if (isExternal(ae.href)) {
links.push(ae.href);
}
});
res['videos'] = links;
return res;
}
function buildSection(title, values) {
if (values.length === 0) {
return '';
}
return '# ' + title + ":\n\n - " + values.join("\n - ") + "\n\n";
}
function getLinksStr() {
let links = getLinks();
let message =
"Page URL: " + location.href + "\n\n" +
buildSection("Anchors", links['anchors']) +
buildSection("Images", links['images']) +
buildSection("CSS Images", links['cssImages']) +
buildSection("Videos", links['videos']);
return message;
}
function dumpLinks() {
console.log(getLinksStr());
}
function copyLinks() {
GM_setClipboard(getLinksStr());
}
let suffix = '';
if (window.top != window.self) {
suffix = ' (' + location.host + location.pathname + ')';
}
GM_registerMenuCommand("Dump External Links to Console" + suffix, dumpLinks);
GM_registerMenuCommand("Copy External Links to Clipboard" + suffix, copyLinks);