Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // Displayable Name of your script
- // @name Akun TagChecker
- // Brief description
- // @description Closes html tags in chat because you can't do it yourself you buffoon
- // Version Number
- // @version 3
- // @include https://anonkun.com/*
- // @include http://anonkun.com/*
- // @grant none
- // ==/UserScript==
- /* User Options */
- var ignoreTags = "area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr"; // HTML tags that don't get closed
- var options = {
- "close tags":true, // Automatically close opened tags
- "tag links":true // Automatically wrap links in tags
- }
- /* Rest of script, best not to touch */
- $(document).ready(function() {
- var timer = setInterval(function () {
- if ($('textarea.chatInput').length){
- clearInterval(timer);
- $('textarea.chatInput').on('input', chatParse);
- newEntryEvent();
- }
- }, 500);
- });
- function chatParse() {
- if (options["close tags"]){
- var startFullTags = $(this).val().match(/<[A-z][^<>]*>/g);
- var endTags = $(this).val().match(/<\/[^<>]*>/g);
- var pattIgnoreTags = new RegExp("("+ignoreTags+")",'i');
- if (endTags == null){
- endTags = [""]; // Make sure the array is populated to prevent it throwing errors
- }
- var startFilteredTags = [];
- if (startFullTags !== null && startFullTags.length > 0){
- var startTags = $.map(startFullTags, function(a){
- return "</"+ a.replace(/[ ]+[^>]*/g,"").substr(1);
- });
- $.each(endTags, function(endIndex, endValue){ // Remove start tags that are closed
- $.each(startTags, function(startIndex, startValue){
- if (startValue == endValue){
- startTags.splice(startIndex, 1);
- return false; // After removing first match break loop
- }
- });
- });
- $.each(startTags, function(startIndex, startValue){ // Only push tags that need closing
- if (! pattIgnoreTags.test(startValue)){
- startFilteredTags.push(startValue);
- }
- });
- }
- }
- if (options["tag links"]){
- var pattEmb = new RegExp("(=\"|<\/a>)");
- var untaggedLinks = $(this).val().match(/(=\")?http[s]?:\/\/[^><\s]+(<\/a>)?/g);
- var untaggedFilteredLinks = [];
- if (untaggedLinks !== null && untaggedLinks.length > 0){
- $.each(untaggedLinks, function(index, value){
- if (! pattEmb.test(value)){
- untaggedFilteredLinks.push(value);
- }
- });
- }
- }
- var input = this;
- var startPos = input.selectionStart;
- var endPos = input.selectionEnd;
- var originalText = input.value;
- if (options["close tags"] && input.selectionStart != undefined && startFilteredTags !== undefined && startFilteredTags.length > 0)
- {
- var startText = originalText.substring(0, startPos);
- var endText = originalText.substring(endPos);
- var newText = startText +startFilteredTags.join("")+ endText;
- input.value = originalText.replace(originalText,newText);
- $(this).selectRange(startPos);
- }
- if (options["tag links"] && input.selectionStart != undefined && untaggedFilteredLinks !== undefined && untaggedFilteredLinks.length > 0)
- {
- $.each(untaggedFilteredLinks, function(index, value){
- var imageFiletypes = new RegExp("\.(jpg|png|gif)$");
- var videoFiletypes = new RegExp("\.(webm|gifv|mp4)$");
- var newText;
- if (imageFiletypes.test(value)){
- //console.log("image");
- newText = '<img src="'+value+'">';
- startPos = newText.length;
- }else if (videoFiletypes.test(value)){
- //console.log("video");
- var valueParsed = value.replace(/\.gifv/g, "\.webm"); // Only tested to work with Imgur
- newText = '<video controls name="media"><source src="'+valueParsed+'" type="video/webm"></video>';
- startPos = newText.length;
- }else{
- //console.log("link");
- newText = '<a href="'+value+'">'+value+'</a>';
- }
- var patt = new RegExp(value+"(?!\")",'g'); // Test adding a ,g
- originalText = originalText.replace(patt,newText);
- });
- input.value = originalText;
- var pattPos = new RegExp(untaggedFilteredLinks[0]+"<");
- var match = pattPos.exec(originalText);
- if (match != null){ // Regular link
- startPos = match.index;
- endPos = startPos+match[0].length-1;
- console.log(startPos+","+ endPos);
- $(this).selectRange(startPos, endPos);
- }else{ // Image embed
- $(this).selectRange(startPos);
- }
- }
- }
- function newEntryEvent(){
- window.MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
- var target = $('body')["0"],
- observer = new MutationObserver(function(mutation) {
- //console.log("New entryfield Event Triggered");
- $('textarea.chatInput').off('input', chatParse);
- $('textarea.chatInput').on('input', chatParse);
- }),
- config = {
- childList: true
- };
- observer.observe(target, config);
- }
- $.fn.selectRange = function(start, end) { // Set caret position function
- if(!end) end = start;
- return this.each(function() {
- if (this.setSelectionRange) {
- this.focus();
- this.setSelectionRange(start, end);
- } else if (this.createTextRange) {
- var range = this.createTextRange();
- range.collapse(true);
- range.moveEnd('character', end);
- range.moveStart('character', start);
- range.select();
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement