-
Notifications
You must be signed in to change notification settings - Fork 0
/
linktools.js
268 lines (222 loc) · 5.26 KB
/
linktools.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
let create= (x)=> document.createElement(x),
select= (x,y=document)=> y.querySelector(x),
selectAll= (x,y=document)=> y.querySelectorAll(x);
/*__________________________________________________________________________________________*/
let obu = false;
window.onunload = window.onbeforeunload= function(){
if(!obu){
obu= true;
sessionStorage.removeItem("urltext");
}};
function linkify(){
let a= selectAll("a");
let regex= /(https?\:\/\/|javascript\:).+/;
if(a.length>0){
let b= [...a].map(i=>i.innerHTML);
if(!sessionStorage.urltext) sessionStorage.urltext= JSON.stringify(b);
a.forEach((i,x)=>{
if(!regex.test(i.text)){
i.text= i.href;
}
else{
let c= JSON.parse(sessionStorage.urltext);
i.innerHTML= c[x];
}
});
}
}
/*__________________________________________________________________________________________*/
let selected= "lol";
document.onselectionchange= function() {
let a= window.getSelection();
selected= (a.toString().length > 0) ? a.getRangeAt(0) : "lol";
};
function selectedLinks(){
let a= selectAll("a");
let c= [];
if(a.length<=0){
alert("No links found on page");
}
else{
if(selected!=="lol"){
let b= window.getSelection();
b.removeAllRanges(); b.addRange(selected);
a.forEach(i=> {if(b.containsNode(i, true)) c.push(i)});
if(c.length>0){
let d= confirm(`${c.length} links selected. Open all in new tabs?`);
if(d) c.forEach(i=> window.open(i.href,"_blank"));
}
else alert("No links found in selection range!");
}//Selected is lol
else{
let e= confirm(`No selection was made. Open ${a.length} links found on this page in new tabs?`);
if(e) a.forEach(i=> window.open(i.href,"_blank"));
}
}
window.getSelection().removeAllRanges();
selected= "lol";
}
/*__________________________________________________________________________________________*/
function listLinks(){
let a= selectAll("a");
let b= create("table");
let c= create("style");
let d= create("div");
d.id= "title";
let f= prompt("Find links containing this keyword (RegEx allowed). Leave blank to list all links");
let g= window.open();
g.document.body.append(c,d,b);
c.innerHTML=`
.red{
color: red;
}
table{
border:1.5px solid black;
border-collapse: collapse;
width: 90vw;
margin-left: 5vw;
table-layout: fixed;
}
#title{
padding: 5px;
width: 90vw;
margin: 10px 5vw;
background: pink;
}
td{
padding: 2px;
border:1px solid black;
height: calc(1em + 5px);
width: calc((100% - 3ch) / 2);
text-align: left;
vertical-align: center;
overflow: auto;
}
td *{
max-height: calc(1em + 5px);
}
tr>td:nth-child(1){
color: brown;
font-weight: bold;
text-align: right;
width: 3ch;
}`;
let e=[];
function findLinks(regex){
a.forEach(i=>{
if(regex.test(i.href)){
let a= `<tr>
<td>${i.innerHTML||'<span class="red">Empty name</span>'}</td>
<td><a href="${i.href}">${i.href}</a></td>
</tr>`;
e.push(a);
}
});
}
if(f){
let regexp= /^\/(.*?)\/([gimuy]*)$/;
let match= f.match(regexp);
let regex= (match) ? new RegExp(match[1],match[2]) : new RegExp(f);
findLinks(regex);
}
else{
let regex= /.+/;
findLinks(regex);
}
b.innerHTML= e.join("\n");
let rows= selectAll("tr",g.document);
rows.forEach((i,x)=>{
let a= create("td");
a.innerHTML= x+1;
i.prepend(a);
});
let title= select("#title",g.document);
if(a.length>0){
if(f){
title.innerHTML= (e.length>0) ?
`Showing ${rows.length} out of ${a.length} links, matching <span class="red">${f}</span>`:
`No links found matching <span class="red">${f}</span>`;
}
else{
title.innerHTML= (e.length>0) ?
`Showing all links found on <span class="red">${location.href}</span>`:
`No links found matching <span class="red">${f}</span>`;
}
}
else title.innerHTML= `No links found on <span class="red">${location.href}</span>`;
}
/*__________________________________________________________________________________________*/
/*__________________________________________________________________________________________*/
let titles= ["Unveil Links","Open selected links","List all links"],
functions= [linkify,selectedLinks,listLinks];
let div= create("div");
div.id= "kdbmlistdiv";
titles.forEach((i,x)=>{
let a= create("span");
a.onclick= functions[x];
a.textContent= i;
div.append(a);
a.className= "kdbmlista";
});
let list= select("#kdbmlistdiv");
if(list){
list.remove();
document.body.append(div);
}
else{
document.body.append(div);
}
let close= create("span");
close.className= "kdbmlistclose";
close.innerHTML= `<svg viewBox="0 0 24 24">
<path d="M 2 2 L 22 22 M 2 22 L22 2" /></svg>`;
div.prepend(close);
function minmax(b,c){
selectAll(".kdbmlista").forEach(i=>{
i.style.opacity= b;
i.style.pointerEvents= c;
});
}
let closed= false;
close.onclick=()=>{
if(closed){
minmax("1","auto");
closed=false;
}
else{
minmax("0","none");
closed= true;
}
};
let style= create("style");
document.body.append(style);
style.innerHTML=`
#kdbmlistdiv{ z-index:99999;
position: fixed; left:1px; top:2px;
display:flex; flex-direction: column;
}
.kdbmlista{
box-sizing: border-box;
height:1.5em;
padding: 2px 5px 2px 5px;
background: white; color: black;
display: flex;
align-items: center;
text-decoration: none;
border: 1px solid black;
transition: .3s linear;
margin-bottom: -1px;
font: 15px "Arial";
}
.kdbmlista:hover{
background:yellow;
}.kdbmlistclose{
height: 2.5ch; width: 2.5ch;
display:block;
}
.kdbmlistclose svg{
width: 100%; height: 100%;
stroke: red; stroke-width: 2;
border: 1px solid red;
stroke-linecap: round;
}`;