toutiao fans

下载头条粉丝信息表格

  1. // ==UserScript==
  2. // @name toutiao fans
  3. // @namespace http://www.yysheng.com/
  4. // @version 1.3
  5. // @description 下载头条粉丝信息表格
  6. // @author yysheng
  7. // @match https://www.toutiao.com/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. console.log("开始打印")
  15. console.log(document.getElementsByClassName('list-wrapper'))
  16. var span = document.createElement('button'); //1、创建元素
  17. span.style.width = '150px'
  18. span.style.height = '30px'
  19. span.style.position = 'fixed'
  20. span.style.bottom = '0'
  21. span.style.right = '160px'
  22. span.style.backgroundColor = 'orange'
  23. span.style.color = 'black'
  24. span.style.textAlign = 'center'
  25. span.style.lineHeight = '30px'
  26. span.style.zIndex = '20000'
  27. span.innerHTML='开始提取';
  28. document.body.appendChild(span)
  29. var nameList = []
  30. var excel = '<table>';
  31. var row = "<tr>";
  32. var timer = null;
  33. function getList(){
  34. if(document.getElementsByClassName('body')){
  35. let list = document.getElementsByClassName('body')[0]
  36. list.scrollTo(list.scrollLeft,list.scrollHeight)
  37. }
  38. nameList = []
  39. let arr = document.getElementsByClassName('name')
  40. for (let i = 0; i < arr.length; i++) {
  41. let name = arr[i].innerText
  42. let phone = name.match(/\d+/g)
  43. if(!phone)continue;
  44. phone = phone.join('')
  45. let myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
  46. if (myreg.test(phone)) {
  47. nameList.push({name:name,phone:phone})
  48. }
  49. }
  50. span.innerHTML='已提取'+nameList.length+'个';
  51. console.log(nameList)
  52. if(document.getElementsByClassName("load-more-btn") && document.getElementsByClassName("load-more-btn")[0]){
  53. document.getElementsByClassName("load-more-btn")[0].click();
  54. }else{
  55. alert("没有更多了")
  56. clearInterval(timer)
  57. timer = null
  58. }
  59. }
  60. span.onclick = function(){
  61. console.log("开始提取")
  62. if(timer){
  63. alert("正在自动提取中...")
  64. return
  65. }
  66. timer = setInterval(getList,500)
  67. }
  68. var span2 = document.createElement('button'); //1、创建元素
  69. span2.style.width = '150px'
  70. span2.style.height = '30px'
  71. span2.style.position = 'fixed'
  72. span2.style.bottom = '0'
  73. span2.style.right = '0'
  74. span2.style.backgroundColor = 'orange'
  75. span2.style.color = 'black'
  76. span2.style.textAlign = 'center'
  77. span2.style.lineHeight = '30px'
  78. span2.style.zIndex = '20000'
  79. span2.innerHTML='下载';
  80. document.body.appendChild(span2)
  81. span2.onclick = function(){
  82. console.log("点击了下载")
  83. if(timer){
  84. alert("正在自动提取中...")
  85. return
  86. }
  87. if(nameList.length < 1){
  88. alert("没有可下载的粉丝信息")
  89. return
  90. }
  91. //先转化json
  92. var arrData = typeof nameList != 'object' ? JSON.parse(nameList) : nameList;
  93. var row = "<tr>";
  94. //设置表头
  95. var keys = Object.keys(nameList[0]);
  96. keys.forEach(function (item) {
  97. row += "<td>" + item + '</td>';
  98. });
  99. //换行
  100. excel += row + "</tr>";
  101. //设置数据
  102. for (var i = 0; i < arrData.length; i++) {
  103. row = "<tr>";
  104. for (var index in arrData[i]) {
  105. row += '<td>' + arrData[i][index] + '</td>';
  106. }
  107. excel += row + "</tr>";
  108. }
  109. excel += "</table>";
  110. var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
  111. excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
  112. excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
  113. excelFile += '; charset=UTF-8">';
  114. excelFile += "<head>";
  115. excelFile += "<!--[if gte mso 9]>";
  116. excelFile += "<xml>";
  117. excelFile += "<x:ExcelWorkbook>";
  118. excelFile += "<x:ExcelWorksheets>";
  119. excelFile += "<x:ExcelWorksheet>";
  120. excelFile += "<x:Name>";
  121. excelFile += "{worksheet}";
  122. excelFile += "</x:Name>";
  123. excelFile += "<x:WorksheetOptions>";
  124. excelFile += "<x:DisplayGridlines/>";
  125. excelFile += "</x:WorksheetOptions>";
  126. excelFile += "</x:ExcelWorksheet>";
  127. excelFile += "</x:ExcelWorksheets>";
  128. excelFile += "</x:ExcelWorkbook>";
  129. excelFile += "</xml>";
  130. excelFile += "<![endif]-->";
  131. excelFile += "</head>";
  132. excelFile += "<body>";
  133. excelFile += excel;
  134. excelFile += "</body>";
  135. excelFile += "</html>";
  136. var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
  137. var link = document.createElement("a");
  138. link.href = uri;
  139. link.style = "visibility:hidden";
  140. link.download = "头条粉丝信息.xls";
  141. document.body.appendChild(link);
  142. link.click();
  143. document.body.removeChild(link);
  144. }
  145. // Your code here...
  146. })();