Fix Twitter Share Link

Replace `twitter` to `vxtwitter` when sharing links

当前为 2023-07-01 提交的版本,查看 最新版本

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