4chan Text Functions

Allows usage of [spoiler]Spoilers[/spoiler] and >greentext inside of picarto chats

  1. // ==UserScript==
  2. // @name 4chan Text Functions
  3. // @namespace Wolvan_PicartoTV_4chan_Chat_Functions
  4. // @version 1.3
  5. // @description Allows usage of [spoiler]Spoilers[/spoiler] and >greentext inside of picarto chats
  6. // @author Wolvan
  7. // @match *://*.picarto.tv/*
  8. // @run-at document-end
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. // Get Picarto's jQuery instance, no need to polute it with our own
  15. //var $ = window.jQuery;
  16.  
  17. // A function to inject CSS into the site
  18. function addCSS(css) {
  19. var head, style;
  20. head = document.getElementsByTagName('head')[0];
  21. if (!head) { return; }
  22. style = document.createElement('style');
  23. style.type = 'text/css';
  24. style.innerHTML = css;
  25. head.appendChild(style);
  26. }
  27.  
  28. // A function that lets me retrieve the text the user has selected
  29. function getSelectionText() {
  30. var text = "";
  31. if (window.getSelection) {
  32. text = window.getSelection().toString();
  33. } else if (document.selection && document.selection.type != "Control") {
  34. text = document.selection.createRange().text;
  35. }
  36. return text;
  37. }
  38.  
  39. // On appending a new message to the messages container we replace the new message's control codes ([spoiler]/[/spoiler]) with properly css-formatted <s> tags
  40. // We also bind the mouseover and mouseout events to hide the spoilers again when you remove the mousecursor
  41. // Lastly we check if the first char of the message is > and turn the message green
  42. var observer = new MutationObserver(function(mutations) {
  43. observer.disconnect();
  44. var selection = $("#msgs > li:last > div > div > span:not(.timestamp, .my_timestamp):last");
  45. if (typeof(selection) !== "undefined") {
  46. if ($("#spoilers").is(":checked")) {
  47. var incoming = selection.html();
  48. if (typeof(incoming) !== "undefined") {
  49. var processed = incoming.replace(/\[spoiler\]/gi, "<s>").replace(/\[\/spoiler\]/gi, "</s>");
  50. var countS = (processed.match(/<s>/g) || []).length;
  51. var countSE = (processed.match(/<\/s>/g) || []).length;
  52. var countDiff = countS - countSE;
  53. if (countDiff > 0) {
  54. for(i = 0; i <= countDiff; i++) {
  55. processed = processed + "</s>";
  56. }
  57. }
  58. selection.html(processed).find("s").mouseover(function() {
  59. $(this).css("color", "white");
  60. }).mouseout(function() {
  61. $(this).css("color", "black");
  62. });
  63. }
  64. }
  65. }
  66. if ($("#greentext").is(":checked")) {
  67. if (selection.text().startsWith(">")) {
  68. selection.css("color", "#8ba446");
  69. }
  70. }
  71. observer.observe(document.querySelector('#msgs'), {
  72. childList: true,
  73. subtree: true
  74. });
  75. });
  76.  
  77. observer.observe(document.querySelector('#msgs'), {
  78. childList: true,
  79. subtree: true
  80. });
  81.  
  82. // Add the CSS to have the spoilers be black boxes without a strikethrough
  83. addCSS(' \
  84. s { \
  85. background-color: black; \
  86. color: black; \
  87. text-decoration: none; \
  88. }\
  89. ');
  90.  
  91. // Allow Ctrl+S to use as hotkey for spoiler tags
  92. $("#msg").bind('keydown', function(event) {
  93. if ($("#kb_shortcuts").is(":checked")) {
  94. if (event.ctrlKey || event.metaKey) {
  95. if (String.fromCharCode(event.which).toLowerCase() === "s") {
  96. event.preventDefault();
  97. if (getSelectionText() !== "") {
  98. var fullmsg = $("#msg").val();
  99. $("#msg").val(fullmsg.replace(getSelectionText(), "[spoiler]" + getSelectionText() + "[/spoiler]"))
  100. } else {
  101. var caretPos = document.getElementById("msg").selectionStart;
  102. var textAreaTxt = jQuery("#msg").val();
  103. var txtToAdd = "[spoiler][/spoiler]";
  104. jQuery("#msg").val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
  105. document.getElementById("msg").selectionStart = caretPos + 9;
  106. document.getElementById("msg").selectionEnd = caretPos + 9;
  107. }
  108. }
  109. }
  110. }
  111. });
  112.  
  113. // Append Settings to the settings menu
  114. var spoilers = GM_getValue("enableSpoiler", true) ? "checked" : "";
  115. var greentext = GM_getValue("enableGreentext", true) ? "checked" : "";
  116. var shortcuts = GM_getValue("enableShortcuts", true) ? "checked" : "";
  117.  
  118. $("#functions > table > tbody > tr:nth-child(4)").after(' \
  119. <tr> \
  120. <td class="functionDesc"><b>4chan Text Functions:</b></td> \
  121. </tr> \
  122. <tr> \
  123. <td class="functionDesc">Spoilers</td> \
  124. <td class="functionButton"> \
  125. <label class="switch switch-green"> \
  126. <input type="checkbox" class="switch-input" id="spoilers" ' + spoilers + '> \
  127. <span class="switch-label" data-on="On" data-off="Off"></span> \
  128. <span class="switch-handle"></span> \
  129. </label> \
  130. </td> \
  131. </tr> \
  132. <tr> \
  133. <td class="functionDesc">Greentext</td> \
  134. <td class="functionButton"> \
  135. <label class="switch switch-green"> \
  136. <input type="checkbox" class="switch-input" id="greentext" ' + greentext + '> \
  137. <span class="switch-label" data-on="On" data-off="Off"></span> \
  138. <span class="switch-handle"></span> \
  139. </label> \
  140. </td> \
  141. </tr> \
  142. <tr> \
  143. <td class="functionDesc">Keyboard Shortcuts</td> \
  144. <td class="functionButton"> \
  145. <label class="switch switch-green"> \
  146. <input type="checkbox" class="switch-input" id="kb_shortcuts" ' + shortcuts + '> \
  147. <span class="switch-label" data-on="On" data-off="Off"></span> \
  148. <span class="switch-handle"></span> \
  149. </label> \
  150. </td> \
  151. </tr> \
  152. ');
  153.  
  154. $("#spoilers").change(function() {
  155. GM_setValue("enableSpoiler", $(this).is(":checked"));
  156. });
  157. $("#greentext").change(function() {
  158. GM_setValue("enableGreentext", $(this).is(":checked"));
  159. });
  160. $("#kb_shortcuts").change(function() {
  161. GM_setValue("enableShortcuts", $(this).is(":checked"));
  162. });

QingJ © 2025

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