Fix Twitter Share Link

Replace `twitter` to `vxtwitter` when sharing links

  1. // ==UserScript==
  2. // @name Fix Twitter Share Link
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Replace `twitter` to `vxtwitter` when sharing links
  6. // @author You
  7. // @match https://twitter.com/*
  8. // @match https://mobile.twitter.com/*
  9. // @match https://tweetdeck.twitter.com/*
  10. // @match https://x.com/*
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
  12. // @grant none
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. const originalExecCommand = document.execCommand;
  19. const hijackedExecCommand = (...attrs) => {
  20. if (attrs[0] == 'copy') {
  21. const selected = window.getSelection().toString();
  22. if (selected.match(/^https+:\/\/((.+)\.)?x\.com\/(.+)\/status\/(\d+)(\?.+)?$/)) {
  23. const newUrl = selected.replace(/^https+:\/\/((.+)\.)?x\.com\/(.+)\/status\/(\d+)(\?.+)?$/, 'https://vxtwitter.com/$3/status/$4');
  24. copyTextToClipboard(newUrl);
  25. return;
  26. }
  27. }
  28. callExecCommand(...attrs);
  29. }
  30. const callExecCommand = (...attrs) => {
  31. document.execCommand = originalExecCommand;
  32. document.execCommand(...attrs);
  33. document.execCommand = hijackedExecCommand;
  34. }
  35. const copyTextToClipboard = (text) => {
  36. const textarea = document.createElement('textarea');
  37. textarea.style.position = 'fixed';
  38. textarea.style.top = '0';
  39. textarea.style.left = '0';
  40. textarea.style.opacity = '0';
  41. textarea.style.pointerEvents = 'none';
  42. textarea.value = text;
  43. document.body.appendChild(textarea);
  44. textarea.select();
  45. callExecCommand('copy', true);
  46. document.body.removeChild(textarea);
  47. }
  48.  
  49. document.execCommand = hijackedExecCommand;
  50. })();