推文扒取

扒取推文的插件

  1. // ==UserScript==
  2. // @name 推文扒取
  3. // @namespace https://github.com/Cierra-Runis/getTweets
  4. // @version 1.2
  5. // @description 扒取推文的插件
  6. // @connect raw.githubusercontent.com
  7. // @connect github.com
  8. // @connect cdn.jsdelivr.net
  9. // @author https://github.com/Cierra-Runis
  10. // @match https://twitter.com/*
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. window.onload = (function () {
  16. 'use strict';
  17.  
  18. var list = [];
  19. var times = 0;
  20. setInterval(addButton, 1000);
  21.  
  22. function addButton() {
  23. var div = document.createElement('div');
  24. div.style.cssText = "width: 25px;height: 25px;color: #ffffff;text-align: center;font-size: small;line-height: 25px;margin: 5px;cursor: pointer;display: flow-root;z-index: 1";
  25. div.innerText = "下载";
  26. div.onclick = function () {
  27. if (confirm("要下载推文吗?")) {
  28. setInterval(getTweets, 5000);
  29. }
  30. }
  31. if (document.querySelector('#react-root').lastChild.innerText == '下载') {
  32. // console.log('button has existed');
  33. } else {
  34. document.querySelector('#react-root').appendChild(div);
  35. }
  36.  
  37. }
  38.  
  39. function getTweets() {
  40. var url = window.location.href;
  41. var before = list.length;
  42.  
  43. try {
  44.  
  45. var blocks = document.evaluate('//*[@id="react-root"]/div[1]/div/div[2]/main/div/div/div/div[1]/div/div[3]/div/div/section/div/div', document).iterateNext().childNodes
  46.  
  47. for (var i = 0; i < blocks.length; i++) {
  48. var div = blocks[i].querySelector('div>div>div>article>div>div');
  49. if (div == null) {
  50. // console.log('block is null');
  51. } else {
  52. try {
  53. var block = div.childNodes[1];
  54. if (block.childNodes[1].childElementCount != 3) {
  55. console.log('This is a block has not text');
  56. continue;
  57. } else {
  58. console.log('Has text!');
  59. }
  60. var href = block.getElementsByTagName("a")[0].href;
  61. if (href != url) {
  62. // console.log(href + "!=" + url);
  63. continue;
  64. } else {
  65. var textContent = block.childNodes[1].childNodes[1].innerText;
  66. var isExited = false;
  67. for (var j = 0; j < list.length; j++) {
  68. if (list[j].content == textContent) {
  69. // console.log('text (' + list[j].content + ') has existed!');
  70. isExited = true;
  71. }
  72. }
  73. if (!isExited) {
  74. console.log('text hasn\'t existed, adding to list.');
  75. list.push({ content: textContent });
  76. }
  77. }
  78. } catch (error) {
  79. // console.log('href isn\'t exist');
  80. }
  81. }
  82. // console.log(i + ". end");
  83. }
  84. // console.log('END');
  85. var after = list.length;
  86. if (after == before) {
  87. // console.log('No new text was add!!!');
  88. times++;
  89. } else {
  90. // console.log('New text was Add.');
  91. times = 0;
  92. }
  93. if (times > 3) {
  94. var jsonStr = JSON.stringify(list);
  95. // console.log(jsonStr);
  96. createAndDownloadFile(url.substring(20) + ".json", jsonStr);
  97. window.location.href = "about:blank";
  98. window.close();
  99. }
  100. window.scrollTo(0, document.body.scrollHeight);
  101. } catch (error) {
  102. console.log('error');
  103. }
  104.  
  105. }
  106.  
  107. function createAndDownloadFile(fileName, content) {
  108. var aTag = document.createElement('a');
  109. var blob = new Blob([content]);
  110. aTag.download = fileName;
  111. aTag.href = URL.createObjectURL(blob);
  112. aTag.click();
  113. URL.revokeObjectURL(blob);
  114. }
  115.  
  116. })();