推特只看原创内容

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

目前为 2024-05-17 提交的版本。查看 最新版本

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

QingJ © 2025

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