Twitter - Hide Retweet

Support 2019 new UI Twitter only.

当前为 2019-08-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter - Hide Retweet
  3. // @version 1.0.8
  4. // @description Support 2019 new UI Twitter only.
  5. // @author Hayao-Gai
  6. // @namespace https://github.com/HayaoGai
  7. // @icon https://i.imgur.com/M9oO8K9.png
  8. // @include https://twitter.com/*
  9. // @require https://code.jquery.com/jquery-3.4.1.min.js
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const $ = window.jQuery;
  17.  
  18. $(document).ready(() => {
  19. observeSystem();
  20. detectUrl();
  21. });
  22.  
  23. // 觀察文件是否產生變化
  24. function observeSystem() {
  25. setTimeout(() => {
  26. const h1 = $("section").find("H1");
  27. const title = $("title");
  28. if (h1.length === 0 || title.length === 0) observeSystem();
  29. else {
  30. // 獲取目標
  31. const target1 = [...h1[0].parentElement.childNodes].filter(child => child.tagName !== "H1")[0].childNodes[0].childNodes[0];
  32. // 目標錯誤
  33. if (target1.className == "css-1dbjc4n r-1adg3ll") observeSystem();
  34. else {
  35. const target2 = title[0];
  36. // 先執行一次
  37. hideRetweet();
  38. // 建立觀察者,文件有變化就執行下列函式
  39. const mutation = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; //前綴
  40. const observer = new mutation(hideRetweet);
  41. // 設定觀察選項
  42. const config = { attributes: true, childList: true, characterData: true };
  43. // 開始觀察
  44. observer.observe(target1, config); //時間軸
  45. observer.observe(target2, config); //標籤頁
  46. }
  47. }
  48. }, 500);
  49. }
  50.  
  51. // 監聽網址改變
  52. function detectUrl() {
  53. window.addEventListener('locationchange', observeSystem);
  54.  
  55. history.pushState = ( f => function pushState(){
  56. var ret = f.apply(this, arguments);
  57. window.dispatchEvent(new Event('pushState'));
  58. window.dispatchEvent(new Event('locationchange'));
  59. return ret;
  60. })(history.pushState);
  61.  
  62. history.replaceState = ( f => function replaceState(){
  63. var ret = f.apply(this, arguments);
  64. window.dispatchEvent(new Event('replaceState'));
  65. window.dispatchEvent(new Event('locationchange'));
  66. return ret;
  67. })(history.replaceState);
  68.  
  69. window.addEventListener('popstate', () => {
  70. window.dispatchEvent(new Event('locationchange'))
  71. });
  72. }
  73.  
  74. function hideRetweet() {
  75. // 已轉推
  76. const retweet = $('path[d="M23.615 15.477c-.47-.47-1.23-.47-1.697 0l-1.326 1.326V7.4c0-2.178-1.772-3.95-3.95-3.95h-5.2c-.663 0-1.2.538-1.2 1.2s.537 1.2 1.2 1.2h5.2c.854 0 1.55.695 1.55 1.55v9.403l-1.326-1.326c-.47-.47-1.23-.47-1.697 0s-.47 1.23 0 1.697l3.374 3.375c.234.233.542.35.85.35s.613-.116.848-.35l3.375-3.376c.467-.47.467-1.23-.002-1.697zM12.562 18.5h-5.2c-.854 0-1.55-.695-1.55-1.55V7.547l1.326 1.326c.234.235.542.352.848.352s.614-.117.85-.352c.468-.47.468-1.23 0-1.697L5.46 3.8c-.47-.468-1.23-.468-1.697 0L.388 7.177c-.47.47-.47 1.23 0 1.697s1.23.47 1.697 0L3.41 7.547v9.403c0 2.178 1.773 3.95 3.95 3.95h5.2c.664 0 1.2-.538 1.2-1.2s-.535-1.2-1.198-1.2z"]');
  77. retweet.each(function() {
  78. if ($(this).parent().parent().attr("class") === "r-111h2gw r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1xzupcd") {
  79. $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().hide();
  80. }
  81. });
  82. // 已喜歡
  83. const like = $('path[d="M12 21.638h-.014C9.403 21.59 1.95 14.856 1.95 8.478c0-3.064 2.525-5.754 5.403-5.754 2.29 0 3.83 1.58 4.646 2.73.814-1.148 2.354-2.73 4.645-2.73 2.88 0 5.404 2.69 5.404 5.755 0 6.376-7.454 13.11-10.037 13.157H12z"]');
  84. like.each(function() {
  85. if ($(this).parent().parent().attr("class") === "r-111h2gw r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1xzupcd") {
  86. $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().hide();
  87. }
  88. });
  89. // 已回覆
  90. const reply = $('path[d="M14.046 2.242l-4.148-.01h-.002c-4.374 0-7.8 3.427-7.8 7.802 0 4.098 3.186 7.206 7.465 7.37v3.828c0 .108.044.286.12.403.142.225.384.347.632.347.138 0 .277-.038.402-.118.264-.168 6.473-4.14 8.088-5.506 1.902-1.61 3.04-3.97 3.043-6.312v-.017c-.006-4.367-3.43-7.787-7.8-7.788z"]');
  91. reply.each(function() {
  92. if ($(this).parent().parent().attr("class") === "r-111h2gw r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1xzupcd") {
  93. $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().hide();
  94. }
  95. });
  96. // 已跟隨
  97. const follow = $('path[d="M12.225 12.165c-1.356 0-2.872-.15-3.84-1.256-.814-.93-1.077-2.368-.805-4.392.38-2.826 2.116-4.513 4.646-4.513s4.267 1.687 4.646 4.513c.272 2.024.008 3.46-.806 4.392-.97 1.106-2.485 1.255-3.84 1.255zm5.849 9.85H6.376c-.663 0-1.25-.28-1.65-.786-.422-.534-.576-1.27-.41-1.968.834-3.53 4.086-5.997 7.908-5.997s7.074 2.466 7.91 5.997c.164.698.01 1.434-.412 1.967-.4.505-.985.785-1.648.785z"]');
  98. follow.each(function() {
  99. if ($(this).parent().parent().attr("class") === "r-111h2gw r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1xzupcd") {
  100. $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().hide();
  101. }
  102. });
  103. }
  104. })();