Insert zero-width spaces into your Tweet

Insert zero-width characters in your tweets

  1. // ==UserScript==
  2. // @name Insert zero-width spaces into your Tweet
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Insert zero-width characters in your tweets
  6. // @author eggplants
  7. // @homepage https://github.com/eggplants
  8. // @match https://twitter.com/*/*
  9. // @match https://twitter.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /*jshint esversion: 8 */
  14.  
  15. function rafAsync() {
  16. return new Promise((resolve) => {
  17. requestAnimationFrame(resolve);
  18. });
  19. }
  20.  
  21. async function checkElement(selector) {
  22. let q = null;
  23. while (q === null) {
  24. await rafAsync();
  25. q = document.querySelector(selector);
  26. }
  27. return q;
  28. }
  29.  
  30. function main() {
  31. "use strict";
  32. let btn = document.createElement("button");
  33. btn.setAttribute("id", "zerow");
  34. btn.innerHTML = "Insert 0-width space";
  35. btn.onclick = function () {
  36. let elms = Array.from(
  37. document.querySelectorAll(
  38. `div[style="max-height: 720px; min-height: 96px;"]>div>div>div>div>div>div>span>*`
  39. )
  40. );
  41. let elms_ns = elms.map((x) => x.tagName);
  42. let uniq_elms_ns = [...new Set(elms_ns.flat(1))];
  43. if (
  44. elms.length === 0 ||
  45. (uniq_elms_ns.length === 1 && uniq_elms_ns[0] === "BR")
  46. ) {
  47. console.log("do nothing");
  48. } else if (
  49. elms_ns.includes("SPAN") &&
  50. elms[elms.length - 1].tagName !== "BR"
  51. ) {
  52. alert("Put a new line at the end of your tweet!");
  53. } else {
  54. elms.forEach((e) => {
  55. e.innerText = e.innerText
  56. .replaceAll('\u{200B}', "")
  57. .replaceAll(/(.)/g, "$1\u{200B}");
  58. });
  59. console.log("inserted!");
  60. }
  61. };
  62. const qs =
  63. "div.css-1dbjc4n.r-1iusvr4.r-16y2uox.r-1777fci" +
  64. ".r-1h8ys4a.r-1bylmt5.r-13tjlyg.r-7qyjyx.r-1ftll1t" +
  65. "> div:nth-child(3) > div > div > div:nth-child(1)";
  66. checkElement(qs).then((e) => {
  67. console.info(e);
  68. e.appendChild(btn);
  69. });
  70. console.log("loaded!");
  71. }
  72.  
  73. function waitForCompose() {
  74. if (
  75. window.location.href === "https://twitter.com/compose/tweet" &&
  76. document.readyState === "complete" &&
  77. document.getElementById("zerow") === null
  78. ) {
  79. main();
  80. // clearInterval(t);
  81. } else {
  82. // console.log("timer");
  83. }
  84. }
  85.  
  86. var t = setInterval(waitForCompose, 500);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址