4chan Text Functions

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

当前为 2015-08-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 4chan Text Functions
  3. // @namespace Wolvan_PicartoTV_4chan_Chat_Functions
  4. // @version 1.1
  5. // @description Allows usage of [spoiler]Spoilers[/spoiler] and >greentext inside of picarto chats
  6. // @author Wolvan
  7. // @match *://*.picarto.tv/live/channel.php?*watch=*
  8. // @match *://*.picarto.tv/live/multistream.php?*watch=*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // Get Picarto's jQuery instance, no need to polute it with our own
  13. var $ = window.jQuery;
  14.  
  15. // A function to inject CSS into the site
  16. function addCSS(css) {
  17. var head, style;
  18. head = document.getElementsByTagName('head')[0];
  19. if (!head) { return; }
  20. style = document.createElement('style');
  21. style.type = 'text/css';
  22. style.innerHTML = css;
  23. head.appendChild(style);
  24. }
  25.  
  26. // A function that lets me retrieve the text the user has selected
  27. function getSelectionText() {
  28. var text = "";
  29. if (window.getSelection) {
  30. text = window.getSelection().toString();
  31. } else if (document.selection && document.selection.type != "Control") {
  32. text = document.selection.createRange().text;
  33. }
  34. return text;
  35. }
  36.  
  37. // 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
  38. // We also bind the mouseover and mouseout events to hide the spoilers again when you remove the mousecursor
  39. $("#msgs").on("append", function(){
  40. var incoming = $(".messageli:last").html();
  41. if (typeof(incoming) !== "undefined") {
  42. var processed = incoming.replace(/\[spoiler\]/gi, "<s>").replace(/\[\/spoiler\]/gi, "</s>");
  43. var countS = (processed.match(/<s>/g) || []).length;
  44. var countSE = (processed.match(/<\/s>/g) || []).length;
  45. var countDiff = countS - countSE;
  46. if (countDiff > 0) {
  47. for(i = 0; i <= countDiff; i++) {
  48. processed = processed + "</s>";
  49. }
  50. }
  51. $(".messageli:last").html(processed).find("s").mouseover(function() {
  52. $(this).css("color", "white");
  53. }).mouseout(function() {
  54. $(this).css("color", "black");
  55. });
  56. }
  57. });
  58.  
  59. // Add the CSS to have the spoilers be black boxes without a strikethrough
  60. addCSS(' \
  61. s { \
  62. background-color: black; \
  63. color: black; \
  64. text-decoration: none; \
  65. }\
  66. ');
  67.  
  68. // Allow Ctrl+S to use as hotkey for spoiler tags
  69. $("#msg").bind('keydown', function(event) {
  70. if (event.ctrlKey || event.metaKey) {
  71. if (String.fromCharCode(event.which).toLowerCase() === "s") {
  72. event.preventDefault();
  73. if (getSelectionText() !== "") {
  74. var fullmsg = $("#msg").val();
  75. $("#msg").val(fullmsg.replace(getSelectionText(), "[spoiler]" + getSelectionText() + "[/spoiler]"))
  76. } else {
  77. var caretPos = document.getElementById("msg").selectionStart;
  78. var textAreaTxt = jQuery("#msg").val();
  79. var txtToAdd = "[spoiler][/spoiler]";
  80. jQuery("#msg").val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
  81. }
  82. }
  83. }
  84. });
  85.  
  86. // On appending a new message to the message container we check if the first char of the message is > and turn the message green
  87. $("#msgs").on("append", function(){
  88. var msg = $(".messageli:last").find(".msgContent");
  89. if (msg.text().startsWith(">")) {
  90. msg.css("color", "#8ba446");
  91. }
  92. });