ipv4info.user.js

This script export ipv4info tables to CSV

  1. // ==UserScript==
  2. // @namespace https://greasyfork.org/es/users/161767
  3. // @name ipv4info.user.js
  4. // @description This script export ipv4info tables to CSV
  5. // @license MIT; https://opensource.org/licenses/MIT
  6. // @version 1.0.3
  7. // @match *://ipv4info.com/*
  8. // @match *://*.ipv4info.com/*
  9. // @exclude http://ipv4info.com/tools/*
  10. // @exclude http://ipv4info.com/store/*
  11. // @exclude http://ipv4info.com/stat/*
  12. // @grant none
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. // Modified from https://codepen.io/kostas-krevatas/pen/mJyBwp
  17.  
  18. window.xport = {
  19. _fallbacktoCSV: true,
  20. toXLS: function(tableId, filename, n) {
  21. var n = (typeof n === 'undefined') ? 0 : n;
  22. this._filename = (typeof filename == 'undefined') ? tableId : filename;
  23. //var ieVersion = this._getMsieVersion();
  24. //Fallback to CSV for IE & Edge
  25. if ((this._getMsieVersion() || this._isFirefox()) && this._fallbacktoCSV) {
  26. return this.toCSV(tableId);
  27. } else if (this._getMsieVersion() || this._isFirefox()) {
  28. alert("Not supported browser");
  29. }
  30.  
  31. //Other Browser can download xls
  32. var table = document.getElementById(tableId);
  33. table = (table == null) ? document.getElementsByClassName(tableId)[n] : table;
  34. if(typeof table == "undefined"){
  35. return false;
  36. }
  37.  
  38. var html = table.outerHTML;
  39.  
  40. this._downloadAnchor("data:application/vnd.ms-excel" + encodeURIComponent(html), 'xls');
  41. },
  42. toCSV: function(tableId, filename, n) {
  43. var n = (typeof n === 'undefined') ? 0 : n;
  44. this._filename = (typeof filename === 'undefined') ? tableId : filename;
  45.  
  46. var table = document.getElementById(tableId);
  47. table = (table == null) ? document.getElementsByClassName(tableId)[n] : table;
  48. console.log(table, typeof table);
  49. if(typeof table == "undefined"){
  50. return false;
  51. }
  52.  
  53. // Generate our CSV string from out HTML Table
  54. var csv = this._tableToCSV(table);
  55. // Create a CSV Blob
  56. var blob = new Blob([csv], { type: "text/csv" });
  57.  
  58. // Determine which approach to take for the download
  59. if (navigator.msSaveOrOpenBlob) {
  60. // Works for Internet Explorer and Microsoft Edge
  61. navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
  62. } else {
  63. this._downloadAnchor(URL.createObjectURL(blob), 'csv');
  64. }
  65. },
  66. _getMsieVersion: function() {
  67. var ua = window.navigator.userAgent;
  68.  
  69. var msie = ua.indexOf("MSIE ");
  70. if (msie > 0) {
  71. // IE 10 or older => return version number
  72. return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)), 10);
  73. }
  74.  
  75. var trident = ua.indexOf("Trident/");
  76. if (trident > 0) {
  77. // IE 11 => return version number
  78. var rv = ua.indexOf("rv:");
  79. return parseInt(ua.substring(rv + 3, ua.indexOf(".", rv)), 10);
  80. }
  81.  
  82. var edge = ua.indexOf("Edge/");
  83. if (edge > 0) {
  84. // Edge (IE 12+) => return version number
  85. return parseInt(ua.substring(edge + 5, ua.indexOf(".", edge)), 10);
  86. }
  87.  
  88. // other browser
  89. return false;
  90. },
  91. _isFirefox: function(){
  92. if (navigator.userAgent.indexOf("Firefox") > 0) {
  93. return 1;
  94. }
  95. return 0;
  96. },
  97. _downloadAnchor: function(content, ext) {
  98. var anchor = document.createElement("a");
  99. anchor.style = "display:none !important";
  100. anchor.id = "downloadanchor";
  101. document.body.appendChild(anchor);
  102.  
  103. // If the [download] attribute is supported, try to use it
  104. if ("download" in anchor) {
  105. anchor.download = this._filename + "." + ext;
  106. }
  107. anchor.href = content;
  108. anchor.click();
  109. anchor.remove();
  110. },
  111. create_link_btn: function(obj_id, n){
  112. var n = (typeof n === 'undefined') ? 0 : n;
  113. var table = document.getElementById(obj_id);
  114. table = (table == null) ? document.getElementsByClassName(obj_id)[n] : table;
  115. console.log(table, typeof table);
  116. if(typeof table == "undefined"){
  117. return false;
  118. }
  119. var parent_div = table.parentNode;
  120. var link = document.createElement("a");
  121. link.id = "download_btn";
  122. link.href = "#download";
  123. link.onclick = function(e){
  124. e.preventDefault();
  125. console.log(e, obj_id);
  126. var file_name = window.location.pathname.split("/").pop().replace(".html", "")
  127. window.xport.toCSV(obj_id,file_name,n);
  128. };
  129. link.innerText = "Export to CSV";
  130. link.style = "position:absolute;z-index:1000;border:1px solid #f0f0f0;padding:10px;color:#fff;background-color:#333;";
  131. parent_div.insertBefore(link, table);
  132. },
  133. _tableToCSV: function(table) {
  134. var slice = Array.prototype.slice;
  135.  
  136. return slice.call(table.rows).map(function(row){
  137. return slice.call(row.cells).map(function(cell){
  138. return cell.innerText.trim();
  139. }).join(",");
  140. }).join("\r\n");
  141. }
  142. };
  143.  
  144. document.onreadystatechange = function () {
  145. if (document.readyState == "complete") {
  146. if(!xport.create_link_btn('main_table')){
  147. if(!xport.create_link_btn('TB2')){
  148. xport.create_link_btn('TB2_90pr', 3); // Select third class element
  149. }
  150. }
  151. }
  152. }