fixupx copy to clipboard button

add a copy to clipboard button to X posts that set it to fixupx.com instead of twitter.com

当前为 2024-05-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name fixupx copy to clipboard button
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-05-02
  5. // @description add a copy to clipboard button to X posts that set it to fixupx.com instead of twitter.com
  6. // @author Sickerine
  7. // @license MIT
  8. // @match https://twitter.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. function apply(article) {
  17. if (article.tagName == 'DIV')
  18. article = article.querySelector('article') || article.closest('article');
  19. if (article.querySelector('.customCopyButton')) return;
  20. if (!article) return;
  21. try {
  22. const SVGStyle = `
  23. width="18"
  24. height="18"
  25. fill="rgb(113, 118, 123)"
  26. `
  27. const defaultSVG = `
  28. <svg
  29. ${SVGStyle}
  30. xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M280 64h40c35.3 0 64 28.7 64 64V448c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V128C0 92.7 28.7 64 64 64h40 9.6C121 27.5 153.3 0 192 0s71 27.5 78.4 64H280zM64 112c-8.8 0-16 7.2-16 16V448c0 8.8 7.2 16 16 16H320c8.8 0 16-7.2 16-16V128c0-8.8-7.2-16-16-16H304v24c0 13.3-10.7 24-24 24H192 104c-13.3 0-24-10.7-24-24V112H64zm128-8a24 24 0 1 0 0-48 24 24 0 1 0 0 48z"/></svg>`
  31.  
  32. const clickedSVG = `
  33. <svg
  34. ${SVGStyle}
  35. xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--!Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M192 0c-41.8 0-77.4 26.7-90.5 64H64C28.7 64 0 92.7 0 128V448c0 35.3 28.7 64 64 64H320c35.3 0 64-28.7 64-64V128c0-35.3-28.7-64-64-64H282.5C269.4 26.7 233.8 0 192 0zm0 64a32 32 0 1 1 0 64 32 32 0 1 1 0-64zM305 273L177 401c-9.4 9.4-24.6 9.4-33.9 0L79 337c-9.4-9.4-9.4-24.6 0-33.9s24.6-9.4 33.9 0l47 47L271 239c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9z"/></svg>
  36.  
  37. `
  38.  
  39. const firstIcon = article.querySelector('[aria-label="More"]');
  40. const sixthParent = firstIcon?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement?.parentElement;
  41. if (!sixthParent) return;
  42. article.classList.add('marked');
  43. const secondChild = sixthParent.children[1];
  44. const newDiv = document.createElement('div');
  45. newDiv.classList.add('customCopyButton');
  46. newDiv.style.marginLeft = 'auto';
  47. newDiv.style.marginRight = '4px';
  48. newDiv.style.cursor = 'pointer';
  49. newDiv.style.display = 'flex';
  50. newDiv.innerHTML = defaultSVG;
  51. newDiv.onclick = (e) => {
  52. e.preventDefault();
  53. const href = article.querySelector('a[href*="/status/"]').href.replace("twitter.com", "fixupx.com")
  54. newDiv.innerHTML = clickedSVG;
  55. setTimeout(() => {
  56. newDiv.innerHTML = defaultSVG;
  57. }, 1000);
  58. navigator.clipboard.writeText(href).then(() => {
  59. console.log('Copied to clipboard: ' + href);
  60. }, (err) => {
  61. console.error('Failed to copy to clipboard: ' + href, err);
  62. });
  63. };
  64. sixthParent.insertBefore(newDiv, secondChild);
  65. }
  66. catch (e) {
  67. console.error(e);
  68. console.log(article)
  69. }
  70. }
  71.  
  72. const observer = new MutationObserver((mutations) => {
  73. mutations.forEach((mutation) => {
  74. mutation.addedNodes.forEach((node) => {
  75. console.log(node.tagName);
  76. if (node.tagName == 'ARTICLE' && node || node.tagName == 'DIV' && (node.querySelector('article') || node.closest('article'))) {
  77. apply(node);
  78. }
  79. });
  80. });
  81. });
  82.  
  83. observer.observe(document.body, { childList: true, subtree: true });
  84.  
  85. })();