User:BennyTheSpaceshipGuy/Chat.js

Note: After saving, you have to bypass your browser's cache to see the changes. /* 	Codes on this page will affect Special:Chat. For stylesheets, use MediaWiki:Chat.css */ /* chat &lt;title& gt; modification */ $("head title").html("Wiki Chat - Club Penguin Wiki"); /* allow further interwiki links */ // define Widget Widget = typeof Widget === "undefined" ? {}  : Widget; // define interwiki links feature Widget.interwikiLinks = {}; // function with parameters for custom interwiki links Widget.interwikiLinks.wikiSubDomain = $('link[rel="canonical"]').attr("href").split(".wikia.com/")[0].substr(7); Widget.interwikiLinks.setInterwikiLinks = function(interwiki,url) { if ( $('a[href^="http://' + Widget.interwikiLinks.wikiSubDomain + '.wikia.com/wiki/' + interwiki + ':"]').length > 0 ) { $('a[href^="http://' + Widget.interwikiLinks.wikiSubDomain + '.wikia.com/wiki/' + interwiki + ':"]').each(function { 			$(this).attr("href",url + "/wiki/" + $(this).attr("href").split("http://" + Widget.interwikiLinks.wikiSubDomain + ".wikia.com/wiki/" + interwiki + ":")[1]); 		}); } } // apply when message inserted $("body").on("DOMNodeInserted", "section.WikiaPage .Chat li:not(.inline-alert)", function { 	Widget.interwikiLinks.setInterwikiLinks("mw","http://www.mediawiki.org"); 	Widget.interwikiLinks.setInterwikiLinks("mediawikiwiki","http://www.mediawiki.org"); 	Widget.interwikiLinks.setInterwikiLinks("commons","http://commons.wikimedia.org"); 	Widget.interwikiLinks.setInterwikiLinks("wikimedia","http://wikimediafoundation.org"); 	Widget.interwikiLinks.setInterwikiLinks("wikipedia","http://en.wikipedia.org"); }); /* 	auto refresh emoticons also load emoticons from MediaWiki:Emoticons/DataURI (DataURI loaded once per request size - might be changed at some point) */ $(function { 	var dataURI = ""; 	setInterval(function { $.getJSON("/api.php?action=query&format=json&prop=revisions&titles=MediaWiki:Emoticons&rvprop=content&cb=" + new Date.getTime, function(info) { 			a = info.query.pages; 			for (var pageid in a) { 				var a = a[pageid].revisions[0]["*"].replace(/\\/g,"\\\\").replace(//g,"\x3e").replace(/\"/g,"\\\"").replace(/\'/g,"\\\'"); 				mw.config.set("EMOTICONS", a + "\n\n" + dataURI); 			} 		}); },30000); // loop every 30 seconds 	function getDataURIEmoticons(d, n, w) { 		/* 			d: current attempt (starting at 0) 			n: top number of attempts (highest attempt is n-1) 			w: timeout if all attempts fail (ms) 		*/ 		if (d < n) { 			$.getJSON("/api.php?action=query&format=json&prop=revisions&titles=MediaWiki:Emoticons/DataURI&rvprop=content&cb=" + new Date.getTime, function(data) { var a = data.query.pages; for (var pageid in a) { var b = a[pageid].revisions[0]["*"]; mw.config.set( 						"EMOTICONS", 						mw.config.get("EMOTICONS") + "\n\n" + b 					); dataURI = b; 				} }).fail(function { console.error("Attempt number #" + (d + 1) + " for getting Data URI emoticons resource failed"); getDataURIEmoticons(d+1, n, w); }); 		} else { 			console.error("All attempts for getting Data URI emoticons failed. Please reload chat in order to try again, or wait another " + (w / 1000) + " seconds for another automatic try"); 			setTimeout( function { getDataURIEmoticons(0, n, w)}, w ); 		} 	} 	getDataURIEmoticons(0, 5, 120000); 	$('textarea[name="message"]').keydown(function(e) { if (e.keyCode == 13 && $(this).val.toLowerCase.search(/^\u0021(datauri|data uri|uri)$/i) == 0) { $(this).val(""); $("div.Chat:first ul").append('Attempting to reload DataURI-based emoticons'); $("div.Chat:first").scrollTop($("div.Chat:first ul").height); getDataURIEmoticons(0, 5, 120000); } 	}); }); /* resize emoticons */ importStylesheet("MediaWiki:ChatResizeEmoticons.css"); /* import scripts */ importScriptPage("MediaWiki:Chat.js/pin.js"); importScriptPage("MediaWiki:Common.js/crestriction.js"); /* cookie setting */ /* script from the W3School */ // set cookie function setCookie(c_name,value,exdays) { var exdate = new Date; exdate.setDate(exdate.getDate + exdays); var c_value = escape(value) + ((exdays == null) ? ""  : "; expires=" + exdate.toUTCString); document.cookie = c_name + "=" + c_value; } // get cookie function getCookie(c_name) { var c_value = document.cookie; var c_start = c_value.indexOf(" " + c_name + "="); if (c_start == -1) { c_start = c_value.indexOf(c_name + "="); } 	if (c_start == -1) { c_value = null; } else { c_start = c_value.indexOf("=", c_start) + 1; var c_end = c_value.indexOf(";", c_start); if (c_end == -1) { c_end = c_value.length; } 		c_value = unescape(c_value.substring(c_start,c_end)); } 	return c_value; } /* chat entrance policy agreement */ /* 	alerts a message that asks the user to confirm that he \/ she have read the wiki's policy and agree to follow the rules. reset every 2 months or when the user is kicked \/ banned */ // set \/ check Widget Widget = typeof Widget === "undefined" ? {} : Widget; // create object for the warning widget Widget.policyWarning = {}; // markup Widget.policyWarning.markup = ' \n\ \n\ \t<div style=\"width: 370px; margin: auto; padding: 3px 7px; background: #f6f6f6; background: -moz-linear-gradient(top, #ffffff 0%, #f6f6f6 47%, #ededed 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(47%,#f6f6f6), color-stop(100%,#ededed)); background: -webkit-linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); background: -o-linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); background: -ms-linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); background: linear-gradient(to bottom, #ffffff 0%,#f6f6f6 47%,#ededed 100%); border: 1px solid #cccccc; border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px;\">\n\ \t\tPlease read carefully before proceeding! \n\ \t\t\n\ \t\t\tBy entering chat, you agree to follow the Club Penguin Wiki\'s policy and its chat policy . \n\ \t\t\t\n\ \t\t \n\ \t \n\ '; // do this when pressing "agree" Widget.policyWarning.agree = function { if (confirm("I've read the policy and promise to follow the wiki's rules") === true) { // agree - close notice and disable message for the next 60 days setCookie("chatPolicy", "agree", 60); $('#policy-notice-blackout, #policy-notice').remove; Widget.policyWarning.clear; } else { // disagree - close window and try again later window.close; } } // trigger if (typeof getCookie("chatPolicy") !== "string") { $("body").append(Widget.policyWarning.markup); } // reset if the user is kicked\/banned Widget.policyWarning.clear = function { $("body").on("DOMNodeInserted", "section.WikiaPage .Chat li", function(ev) { 		if ( $(ev.target).hasClass("inline-alert") && typeof getCookie("chatPolicy") === "string" && ( 				$(ev.target).html.indexOf("You have been kicked by ") > -1 || 				$(ev.target).html.indexOf("You have been banned by ") > -1 			) ) { 			document.cookie = "chatPolicy" + "=; expires=Thu, 01-Jan-70 00:00:01 GMT;"; 		} 	}); } if (typeof getCookie("chatPolicy") === "string") { Widget.policyWarning.clear; } /* 	the following script blocks certain works in certain conditions regex reduction suggested by Callofduty4 http://community.wikia.com/wiki/Thread:628738#7 */ ChatStringsBlocker = {"count": 0}; $('textarea[name="message"]').on("keypress", function(e) { 	if (e.keyCode == 13) { 		var a = " " + $('textarea[name="message"]').val.toLowerCase + " ", // the padded spaces are to make sure that matches won't be located in the beginning or ending of the message 			b = [ 				"ass", 				"asses", 				"bitch", 				"bitches", 				"bitchy", 				"boob", 				"boobs", 				"cunt", 				"dick", 				"dumbass", 				"fuck", 				"fucker", 				"fucking", 				"motherfucker", 				"nigga", 				"niggas", 				"nigger", 				"niggers", 				"penis", 				"penises", 				"piss", 				"pussy", 				"shit", 				"shitty", 				"tits", 				"sex", 				"whore", 				"whores" 			], 			escape = "[\\n\\d \\?\\!\\.\\,]+(" + b.join("\n").replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&").replace(/\n/g,"|") + ")[\\n\\d \\?\\!\\.\\,]+"; // decoder from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex#answer-6969486 if (a.split(new RegExp(escape)).length > 1) { // 1 or more matches (no need to globally search) $('textarea[name="message"]').val(""); ChatStringsBlocker.count++; if (ChatStringsBlocker.count < 2) { alert("Warning! You were caught using inappropriate language and your message has been blocked."); } else if (ChatStringsBlocker.count === 2) { alert("LAST WARNING!!!\nIt's the second time you were caught using inappropriate language. A third time would automatically kick you from the chat room!"); } else if (ChatStringsBlocker.count === 3) { window.close; // close on 3rd offense } 		} 	} }); /* shorten long external url links */ $("body").on("DOMNodeInserted", function(el) { $(el.target).find(".message a").each(function { 		if ($(this).text.length > 150 && $(this).attr("href").indexOf(location.origin) != 0) { 			// external link with over 150 characters 			var a = $(this).text; 			$(this).text(a.substr(0,50) + "\u2026" + a.substr(a.length - 51, 50)) 		} 	}); }); /* bot command blocker */ $(function { var restrictedPhrases = [ "!tell Polo_Field " ], 		regexString = []; for (var i in restrictedPhrases) { regexString.push("^" + restrictedPhrases[i].replace(/[\\\/\.\,\?\!\(\|\)\:\{\}\[\]\+\*\=\^\$]/g, "\\$&")); } 	var regex = new RegExp(regexString.join("|")); NodeChatDiscussion.prototype.getTextInput.keydown(function(e) { 		if (e.keyCode == 13 && !e.shiftKey) { 			if (this.value.search(regex) == 0) { 				e.preventDefault; 			} 		} 	}); }); /* sockpuppet detection for block-evading accounts - against the wiki rules - http://clubpenguin.wikia.com/wiki/Club_Penguin_Wiki:Policy/Accounts */ /* a note: this feature does NOT collect data or ip addresses about users - only to enforce our policy */ if (true) { 	$.getJSON("/api.php?action=query&format=json&meta=userinfo&uiprop=rights&cb=" + new Date.getTime, function(data) { var a = data.query.userinfo.rights, isBypassBlock = false; for (var i in a) { if (a[i] === "ipblock-exempt") { isBypassBlock = true; } 		} 		if (!isBypassBlock) { // user is not in group that allows evading ip blocks (e.g. not sysop, staff or vstf), check if ip is blocked $.ajax({ 				url: "/api.php?action=query&format=json&meta=userinfo&uiprop=blockinfo&callback=cb", 				dataType: "jsonp", 				jsonpCallback: "cb", 				success: function(data) { 					if (typeof data.query.userinfo.blockedby !== "undefined") { 						// ip is blocked 						mainRoom.socket.send( new models.ChatEntry({ 								roomId: mainRoom.roomId, 								name: mw.config.get("wgUserName"), 								text: "Error #sppt_id" + mainRoom.roomId + " at unix" + new Date.getTime + ": please notify an admin and include the username of this message's poster and the message's content" 							}).xport ); 						localStorage.chatIpBlockedDetected = new Date.getTime; 					} 				} 			}); } 	}); }
 * Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
 * Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
 * Opera users have to clear their caches through Tools→Preferences
 * Konqueror and Safari users can just click the Reload button.
 * Chrome: press Ctrl+F5 or Shift+F5