Advertisement
Guest User

Akun TagChecker v3

a guest
Apr 19th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // Displayable Name of your script
  3. // @name           Akun TagChecker
  4.  
  5. // Brief description
  6. // @description    Closes html tags in chat because you can't do it yourself you buffoon
  7.  
  8. // Version Number
  9. // @version        3
  10.  
  11. // @include        https://anonkun.com/*
  12. // @include        http://anonkun.com/*
  13. // @grant          none
  14.  
  15. // ==/UserScript==
  16.  
  17. /* User Options */
  18. 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
  19. var options = {
  20.     "close tags":true, // Automatically close opened tags
  21.     "tag links":true   // Automatically wrap links in tags
  22. }
  23. /* Rest of script, best not to touch */
  24.  
  25. $(document).ready(function() {
  26.     var timer = setInterval(function () {
  27.         if ($('textarea.chatInput').length){
  28.             clearInterval(timer);
  29.             $('textarea.chatInput').on('input', chatParse);
  30.             newEntryEvent();
  31.         }
  32.     }, 500);
  33. });
  34.  
  35. function chatParse() {
  36.     if (options["close tags"]){
  37.         var startFullTags = $(this).val().match(/<[A-z][^<>]*>/g);
  38.         var endTags = $(this).val().match(/<\/[^<>]*>/g);
  39.         var pattIgnoreTags = new RegExp("("+ignoreTags+")",'i');
  40.         if (endTags == null){
  41.             endTags = [""]; // Make sure the array is populated to prevent it throwing errors
  42.         }
  43.  
  44.         var startFilteredTags = [];
  45.         if (startFullTags !== null && startFullTags.length > 0){
  46.             var startTags = $.map(startFullTags, function(a){
  47.                 return "</"+ a.replace(/[ ]+[^>]*/g,"").substr(1);
  48.             });
  49.             $.each(endTags, function(endIndex, endValue){ // Remove start tags that are closed
  50.                 $.each(startTags, function(startIndex, startValue){
  51.                     if (startValue == endValue){
  52.                         startTags.splice(startIndex, 1);
  53.                         return false; // After removing first match break loop
  54.                     }
  55.                 });
  56.             });
  57.             $.each(startTags, function(startIndex, startValue){ // Only push tags that need closing
  58.                 if (! pattIgnoreTags.test(startValue)){
  59.                     startFilteredTags.push(startValue);
  60.                 }
  61.             });
  62.         }
  63.     }
  64.     if (options["tag links"]){
  65.         var pattEmb = new RegExp("(=\"|<\/a>)");
  66.         var untaggedLinks = $(this).val().match(/(=\")?http[s]?:\/\/[^><\s]+(<\/a>)?/g);
  67.         var untaggedFilteredLinks = [];
  68.         if (untaggedLinks !== null && untaggedLinks.length > 0){
  69.             $.each(untaggedLinks, function(index, value){
  70.                 if (! pattEmb.test(value)){
  71.                     untaggedFilteredLinks.push(value);
  72.                 }
  73.             });
  74.         }
  75.     }
  76.     var input = this;
  77.     var startPos = input.selectionStart;
  78.     var endPos = input.selectionEnd;
  79.     var originalText = input.value;
  80.     if (options["close tags"] && input.selectionStart != undefined && startFilteredTags !== undefined && startFilteredTags.length > 0)
  81.     {
  82.         var startText = originalText.substring(0, startPos);
  83.         var endText = originalText.substring(endPos);
  84.         var newText = startText +startFilteredTags.join("")+ endText;
  85.         input.value = originalText.replace(originalText,newText);
  86.         $(this).selectRange(startPos);
  87.     }
  88.     if (options["tag links"] && input.selectionStart != undefined && untaggedFilteredLinks !== undefined && untaggedFilteredLinks.length > 0)
  89.     {
  90.         $.each(untaggedFilteredLinks, function(index, value){
  91.             var imageFiletypes = new RegExp("\.(jpg|png|gif)$");
  92.             var videoFiletypes = new RegExp("\.(webm|gifv|mp4)$");
  93.             var newText;
  94.             if (imageFiletypes.test(value)){
  95.                 //console.log("image");
  96.                 newText = '<img src="'+value+'">';
  97.                 startPos = newText.length;
  98.             }else if (videoFiletypes.test(value)){
  99.                 //console.log("video");
  100.                 var valueParsed = value.replace(/\.gifv/g, "\.webm"); // Only tested to work with Imgur
  101.                 newText = '<video controls name="media"><source src="'+valueParsed+'" type="video/webm"></video>';
  102.                 startPos = newText.length;
  103.             }else{
  104.                 //console.log("link");
  105.                 newText = '<a href="'+value+'">'+value+'</a>';
  106.             }
  107.             var patt = new RegExp(value+"(?!\")",'g'); // Test adding a ,g
  108.             originalText = originalText.replace(patt,newText);
  109.         });
  110.         input.value = originalText;
  111.         var pattPos = new RegExp(untaggedFilteredLinks[0]+"<");
  112.         var match = pattPos.exec(originalText);
  113.         if (match != null){ // Regular link
  114.             startPos = match.index;
  115.             endPos = startPos+match[0].length-1;
  116.             console.log(startPos+","+ endPos);
  117.             $(this).selectRange(startPos, endPos);
  118.         }else{ // Image embed
  119.             $(this).selectRange(startPos);
  120.         }
  121.     }
  122. }
  123.  
  124. function newEntryEvent(){
  125.     window.MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  126.     var target =  $('body')["0"],
  127.         observer = new MutationObserver(function(mutation) {
  128.             //console.log("New entryfield Event Triggered");
  129.             $('textarea.chatInput').off('input', chatParse);
  130.             $('textarea.chatInput').on('input', chatParse);
  131.         }),
  132.         config = {
  133.             childList: true
  134.         };
  135.     observer.observe(target, config);
  136. }
  137.  
  138. $.fn.selectRange = function(start, end) { // Set caret position function
  139.     if(!end) end = start;
  140.     return this.each(function() {
  141.         if (this.setSelectionRange) {
  142.             this.focus();
  143.             this.setSelectionRange(start, end);
  144.         } else if (this.createTextRange) {
  145.             var range = this.createTextRange();
  146.             range.collapse(true);
  147.             range.moveEnd('character', end);
  148.             range.moveStart('character', start);
  149.             range.select();
  150.         }
  151.     });
  152. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement