推特只看原创内容

在网页版推特中只查看原创内容

目前为 2023-11-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 推特只看原创内容
  3. // @namespace https://github.com/ssoda01
  4. // @version 1.1
  5. // @description 在网页版推特中只查看原创内容
  6. // @author sodakoo
  7. // @match https://twitter.com/*
  8. // @license GPL-3.0-only
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14. let stylePC = document.createElement('style');
  15. let styleMobile = document.createElement('style');
  16. let isMobile = false;
  17. if (window.screen.width <= 768) {
  18. isMobile = true
  19. }
  20. else {
  21. isMobile = false
  22. }
  23. stylePC.innerHTML = `
  24. .yellow-btn-common {
  25. position: fixed;
  26. top: 10px;
  27. right: 20px;
  28. z-index: 9999;
  29. border-radius: 20px;
  30. background: #fff;
  31. box-shadow: 5px 5px #eff3f4;
  32. width: 180px;
  33. padding: 0 8px;
  34. border: 2px solid;
  35. transition: .3s;
  36. cursor: pointer;
  37. }
  38.  
  39. .yellow-btn-common:hover {
  40. background: rgba(0,0,0,0.45);
  41. color: #fff;
  42. }
  43. .yellow-btn-common:active {
  44. background: #000;
  45. color: #fff;
  46. }
  47.  
  48. .yellow-btn-off {
  49. color: #c3c3c3;
  50. border-color: #c3c3c3;
  51. }
  52. .yellow-btn-on {
  53. color: #000;
  54. border-color: #000;
  55. }
  56. `;
  57. styleMobile.innerHTML = `
  58. .yellow-btn-common {
  59. opacity: .3;
  60. position: fixed;
  61. bottom: 42px;
  62. right: 16px;
  63. z-index: 9999;
  64. border-radius: 20px;
  65. background: #fff;
  66. box-shadow: 5px 5px #eff3f4;
  67. width: 48px;
  68. border: 2px solid;
  69. transition: .3s;
  70. }
  71.  
  72. .yellow-btn-common:hover {
  73. background: rgba(0,0,0,0.45);
  74. color: #fff;
  75. }
  76. .yellow-btn-common:active {
  77. background: #000;
  78. color: #fff;
  79. }
  80.  
  81. .yellow-btn-off {
  82. color: #c3c3c3;
  83. border-color: #c3c3c3;
  84. }
  85. .yellow-btn-on {
  86. color: #000;
  87. border-color: #000;
  88. }
  89. `;
  90. // 将样式节点添加到文档头部
  91.  
  92. document.head.appendChild(isMobile ? styleMobile : stylePC);
  93.  
  94. // 开关状态 0已关闭 1已开启
  95. let status = 0;
  96. let observer = undefined;
  97. let getBtnText = (status) => {
  98. if (status === 0) { return isMobile ? 'OFF' : '只看原创内容 OFF' }
  99. else { return isMobile ? 'ON' : '只看原创内容 ON' }
  100. }
  101. // 监听页面加载完成事件
  102. // 创建悬浮按钮
  103. let button = document.createElement('button');
  104. button.classList.add('yellow-btn-common');
  105. button.classList.add('yellow-btn-off');
  106. button.textContent = getBtnText(status);
  107. document.body.appendChild(button);
  108.  
  109. // 隐藏非原创推文的函数
  110. function hideNonOriginalTweets() {
  111. // 获取所有 article[data-testid="tweet"] 元素
  112. let tweetElements = document.querySelectorAll('article[data-testid="tweet"]');
  113.  
  114. // 遍历推文元素
  115. for (let i = 0; i < tweetElements.length; i++) {
  116. let tweetElement = tweetElements[i];
  117.  
  118. // 检查是否存在后代子元素 <span data-testid="socialContext">
  119. let socialContextElement = tweetElement.querySelector('span[data-testid="socialContext"]');
  120. if (socialContextElement) {
  121. // 隐藏包含后代子元素的 article 元素
  122. tweetElement.style.display = 'none';
  123. }
  124. }
  125. // 创建一个 MutationObserver 实例
  126. observer = new MutationObserver(function (mutationsList) {
  127. for (let i = 0; i < mutationsList.length; i++) {
  128. let mutation = mutationsList[i];
  129. if (mutation.type === 'childList') {
  130. // 检查是否还有需要删除的元素
  131. console.log("检查是否还有需要删除的元素>>")
  132. let remainingElements = document.querySelectorAll('article[data-testid="tweet"]');
  133. for (let i = 0; i < remainingElements.length; i++) {
  134. let remainingElement = remainingElements[i];
  135. // 检查是否存在后代子元素 <span data-testid="socialContext">
  136. let socialContextElement = remainingElement.querySelector('span[data-testid="socialContext"]');
  137. if (socialContextElement) {
  138. // 隐藏包含后代子元素的 article 元素
  139. remainingElement.style.display = 'none';
  140. }
  141. }
  142. // if (remainingElements.length === 0) {
  143. // // 停止观察
  144. // observer.disconnect();
  145. // }
  146. }
  147. }
  148. });
  149.  
  150. // 开始观察 document.body 的子节点变化
  151. observer.observe(document.body, { childList: true, subtree: true });
  152. }
  153.  
  154. // 按钮点击事件处理程序
  155. button.addEventListener('click', function () {
  156. status = status === 0 ? 1 : 0
  157. button.textContent = getBtnText(status);
  158. if (status === 0) {
  159. button.classList.remove('yellow-btn-on');
  160. button.classList.add('yellow-btn-off');
  161. if (observer) {
  162. observer.disconnect();
  163. }
  164. } else {
  165. button.classList.remove('yellow-btn-off');
  166. button.classList.add('yellow-btn-on');
  167. hideNonOriginalTweets();
  168. }
  169.  
  170. });
  171. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址