x-twitter-add-to-list-button

1-click "add user to [xyz] list" button next to usernames while scrolling your x (twitter) feed (be sure to edit the variable "lists")

目前为 2024-05-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name x-twitter-add-to-list-button
  3. // @name:ja x-twitter-add-to-list-button
  4. // @namespace x-twitter
  5. // @version 0.2.1
  6. // @description 1-click "add user to [xyz] list" button next to usernames while scrolling your x (twitter) feed (be sure to edit the variable "lists")
  7. // @description:ja リストにワンクリックで追加するボタンを表示します(変数"lists"を必ず編集してください)
  8. // @author fuwawascoco
  9. // @match https://twitter.com/*
  10. // @match https://mobile.twitter.com/*
  11. // @match https://x.com/*
  12. // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
  13. // @grant none
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. const lists = ['waitlist', 'illustrators', 'animators']; // be sure to change to the NAME of your lists (not IDs)
  21. const checkInterval = 512; // ms
  22. const tryInterval = 64; // ms
  23.  
  24. function retryUntilSuccess(newTab, selector, innerText, callback) {
  25. const intervalID = setInterval(() => {
  26. const element = newTab.document.querySelector(selector);
  27. if (innerText != ''){
  28. const elements = newTab.document.querySelectorAll(selector);
  29. element = Array.from(elements).find(el => el.textContent.trim() === innerText);
  30.  
  31. }
  32. if (element && element.offsetParent != null) { // Check if element is visible
  33. clearInterval(intervalID);
  34. callback(element);
  35. }
  36. }, tryInterval);
  37. }
  38.  
  39. function onClick(userProfile, list) {
  40. const newTab = open(userProfile);
  41. newTab.addEventListener('beforeunload', () => clearInterval(intervalID));
  42.  
  43. retryUntilSuccess(newTab, 'button[aria-label="More"][data-testid="userActions"]', '', moreButton => {
  44. moreButton.click();
  45. retryUntilSuccess(newTab, 'a[href="/i/lists/add_member"][role="menuitem"]', '', listButton => {
  46. listButton.click();
  47. retryUntilSuccess(newTab, '[aria-modal="true"]', '', modal => {
  48. const listSpan = Array.from(modal.getElementsByTagName('span')).find(span => span.textContent === list);
  49. if (!listSpan) {
  50. newTab.alert(`"${list}" was not found, please edit the script to update the variable "lists" with your own names.`);
  51. newTab.close();
  52. return;
  53. }
  54. const checkbox = listSpan.closest('[role="checkbox"]');
  55. if (checkbox && checkbox.getAttribute('aria-checked') === 'false') {
  56. checkbox.click();
  57. }
  58. });
  59. });
  60. });
  61. }
  62.  
  63. function createButton(userProfile, list) {
  64. const button = document.createElement('button');
  65. button.style.fontSize = '90%';
  66. button.style.margin = '0 0.25em';
  67. button.textContent = list;
  68. button.addEventListener('click', () => onClick(userProfile, list));
  69. return button;
  70. }
  71.  
  72. function createButtonContainer(userProfile) {
  73. const container = document.createElement('div');
  74. container.style.position = 'relative';
  75. container.style.left = '2%';
  76. container.style.opacity = 0.5;
  77. lists.forEach(list => container.appendChild(createButton(userProfile, list)));
  78. container.classList.add('listButtons');
  79. return container;
  80. }
  81.  
  82. function addButtons() {
  83. const nodes = document.querySelectorAll('[data-testid="User-Name"]:not(:has(.listButtons))');
  84. nodes.forEach(node => {
  85. const userProfile = node.querySelector('a')?.href || '';
  86. if (userProfile) {
  87. node.appendChild(createButtonContainer(userProfile));
  88. }
  89. });
  90. }
  91.  
  92. setInterval(addButtons, checkInterval);
  93.  
  94. })();