4chan Text Functions

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

29.08.2015 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         4chan Text Functions
// @namespace    Wolvan_PicartoTV_4chan_Chat_Functions
// @version      1.1
// @description  Allows usage of [spoiler]Spoilers[/spoiler] and >greentext inside of picarto chats
// @author       Wolvan
// @match        *://*.picarto.tv/live/channel.php?*watch=*
// @match        *://*.picarto.tv/live/multistream.php?*watch=*
// @grant        none
// ==/UserScript==

// Get Picarto's jQuery instance, no need to polute it with our own
var $ = window.jQuery;

// A function to inject CSS into the site
function addCSS(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

// A function that lets me retrieve the text the user has selected
function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

// 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
// We also bind the mouseover and mouseout events to hide the spoilers again when you remove the mousecursor
$("#msgs").on("append", function(){
	var incoming = $(".messageli:last").html();
	if (typeof(incoming) !== "undefined") {
		var processed = incoming.replace(/\[spoiler\]/gi, "<s>").replace(/\[\/spoiler\]/gi, "</s>");
		var countS = (processed.match(/<s>/g) || []).length;
		var countSE = (processed.match(/<\/s>/g) || []).length;
		var countDiff = countS - countSE;
		if (countDiff > 0) {
			for(i = 0; i <= countDiff; i++) {
				processed = processed + "</s>";
			}
		}
		$(".messageli:last").html(processed).find("s").mouseover(function() {
			$(this).css("color", "white");
		}).mouseout(function() {
			$(this).css("color", "black");
		});
	}
});

// Add the CSS to have the spoilers be black boxes without a strikethrough
addCSS(' \
	s { \
		background-color: black; \
		color: black; \
		text-decoration: none; \
	}\
');

// Allow Ctrl+S to use as hotkey for spoiler tags
$("#msg").bind('keydown', function(event) {
	if (event.ctrlKey || event.metaKey) {
		if (String.fromCharCode(event.which).toLowerCase() === "s") {
			event.preventDefault();
			if (getSelectionText() !== "") {
				var fullmsg = $("#msg").val();
				$("#msg").val(fullmsg.replace(getSelectionText(), "[spoiler]" + getSelectionText() + "[/spoiler]"))
			} else {
				var caretPos = document.getElementById("msg").selectionStart;
				var textAreaTxt = jQuery("#msg").val();
				var txtToAdd = "[spoiler][/spoiler]";
				jQuery("#msg").val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
			}
		}
	}
});

// On appending a new message to the message container we check if the first char of the message is > and turn the message green
$("#msgs").on("append", function(){
	var msg = $(".messageli:last").find(".msgContent");
	if (msg.text().startsWith(">")) {
		msg.css("color", "#8ba446");
	}
});