Kawiesh

LinkToolz

Nov 3rd, 2021 (edited)
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let create= (x)=> document.createElement(x),
  2. select= (x,y=document)=> y.querySelector(x),
  3. selectAll= (x,y=document)=> y.querySelectorAll(x);
  4.  
  5.  
  6. /*
  7.  
  8. td, td>a{
  9. ocerflow: auto;
  10. white-space: nowrap;
  11. text-decoration:none;
  12. }
  13.  
  14. */
  15.  
  16. /*__________________________________________________________________________________________*/
  17. let obu = false;
  18. window.onunload = window.onbeforeunload= function(){
  19. if(!obu){
  20. obu= true;
  21. sessionStorage.removeItem("urltext");
  22. }};
  23.  
  24.  
  25. function linkify(){
  26. let a= selectAll("a");
  27. let regex= /(https?\:\/\/|javascript\:).+/;
  28. let b= [...a].map(i=>i.innerHTML);
  29.  
  30. if(!sessionStorage.urltext) sessionStorage.urltext= JSON.stringify(b);
  31.  
  32. a.forEach((i,x)=>{
  33. if(!regex.test(i.text)){
  34. i.text= i.href;
  35. }
  36. else{
  37. let c= JSON.parse(sessionStorage.urltext);
  38. i.innerHTML= c[x];
  39. }
  40. });
  41.  
  42. }
  43.  
  44. /*__________________________________________________________________________________________*/
  45. let selected= "lol";
  46. document.onselectionchange= function() {
  47. let a= window.getSelection();
  48. selected= (a.toString().length > 0) ? a.getRangeAt(0) : "lol";
  49. };
  50.  
  51.  
  52. function selectedLinks(){
  53. let a= selectAll("a");
  54. let c= [];
  55.  
  56. if(a.length<=0){
  57. alert("No links found on page");
  58. }
  59. else{
  60. if(selected!=="lol"){
  61. let b= window.getSelection();
  62. b.removeAllRanges(); b.addRange(selected);
  63. a.forEach(i=> {if(b.containsNode(i, true)) c.push(i)});
  64.  
  65. if(c.length>0){
  66. let d= confirm(`${c.length} links selected. Open all in new tabs?`);
  67.        if(d) c.forEach(i=> window.open(i.href,"_blank"));
  68. }
  69. else alert("No links found in selection range!");
  70.  
  71. }//Selected is lol
  72. else{
  73. let e= confirm(`No selection was made. Open ${a.length} links found on this page in new tabs?`);
  74.        if(e) a.forEach(i=> window.open(i.href,"_blank"));
  75. }
  76. }
  77.  
  78.  
  79. window.getSelection().removeAllRanges();
  80. selected= "lol";
  81. }
  82.  
  83. /*__________________________________________________________________________________________*/
  84. function listLinks(){
  85. let a= selectAll("a");
  86. let b= create("table");
  87. let c= create("style");
  88. let d= create("div");
  89. d.id= "title";
  90. let f= prompt("Find links containing this keyword (RegEx allowed). Leave blank to list all links");
  91. let g= window.open();
  92. g.document.body.append(c,d,b);
  93.  
  94.  
  95. c.innerHTML=`
  96. .red{
  97. color: red;
  98. }
  99.  
  100. table{
  101. border:1.5px solid black;      
  102. border-collapse: collapse;
  103. width: 90vw;
  104. margin-left: 5vw;
  105. table-layout: fixed;
  106. }
  107.  
  108. #title{
  109. padding: 5px;      
  110. width: 90vw;
  111. margin: 10px 5vw;
  112. background: pink;
  113. }
  114.  
  115.  
  116. td{
  117. padding: 2px;
  118. border:1px solid black;
  119. height: calc(1em + 5px);
  120. width: calc((100% - 3ch) / 2);
  121. text-align: left;
  122. vertical-align: center;
  123. overflow: auto;
  124. }
  125.  
  126. td *{
  127. max-height: calc(1em + 5px);
  128. }
  129.  
  130. tr>td:nth-child(1){
  131. color: brown;
  132. font-weight: bold;
  133. text-align: right;
  134. width: 3ch;
  135. }`;
  136.  
  137.  
  138. let e=[];
  139.  
  140. function findLinks(regex){
  141. a.forEach(i=>{
  142. if(regex.test(i.href)){
  143. let a= `<tr>
  144. <td>${i.innerHTML||'<span class="red">Empty name</span>'}</td>
  145. <td><a href="${i.href}">${i.href}</a></td>
  146. </tr>`;
  147. e.push(a);
  148. }
  149. });
  150. }
  151.  
  152.  
  153. if(f){
  154. let regexp= /^\/(.*?)\/([gimuy]*)$/;
  155. let match= f.match(regexp);
  156. let regex= (match) ? new RegExp(match[1],match[2]) : new RegExp(f);
  157. findLinks(regex);
  158. }
  159. else{
  160. let regex= /.+/;
  161. findLinks(regex);
  162. }
  163.  
  164. b.innerHTML= e.join("\n");
  165.  
  166. let rows= selectAll("tr",g.document);
  167. rows.forEach((i,x)=>{
  168. let a= create("td");
  169. a.innerHTML= x+1;
  170. i.prepend(a);
  171. });
  172.  
  173. let title= select("#title",g.document);
  174.  
  175. if(a.length>0){
  176. if(f){
  177. title.innerHTML= (e.length>0) ?
  178. `Showing ${rows.length} out of ${a.length} links, matching <span class="red">${f}</span>`:
  179. `No links found matching <span class="red">${f}</span>`;
  180. }
  181. else{
  182. title.innerHTML= (e.length>0) ?
  183. `Showing all links found on <span class="red">${location.href}</span>`:
  184. `No links found matching <span class="red">${f}</span>`;
  185. }
  186.  
  187. }
  188. else title.innerHTML= `No links found on <span class="red">${location.href}</span>`;
  189.  
  190. }
  191.  
  192.  
Add Comment
Please, Sign In to add comment