Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 14th, 2012  |  syntax: JavaScript  |  size: 5.32 KB  |  hits: 61  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // ==UserScript==
  2. // @name           4chan Linkify
  3. // @namespace      csimi
  4. // @author         csimi
  5. // @description    Linkification of text links.
  6. // @homepage       http://userscripts.org/users/156405/scripts
  7. // @version        1.4.5
  8. // @updateURL      http://userscripts.org/scripts/source/87750.meta.js
  9. // @icon           http://i.imgur.com/JHVzK.png
  10. // @include        http://boards.4chan.org/*
  11. // @include        https://boards.4chan.org/*
  12. // ==/UserScript==
  13.  
  14. (function () {
  15.  
  16. ///////////////////////////////////
  17.  
  18. var config = {
  19.         'multiLineLinks': true,
  20.         'targetBlank': false
  21. }
  22.  
  23. ///////////////////////////////////
  24.  
  25. function $ (a, b) { if (a && document.querySelector) return (b || document).querySelector(a); }
  26. function $$ (a, b) { if (a && document.querySelectorAll) return (b || document).querySelectorAll(a); }
  27. function foreach (a, f) { if (typeof(a) == 'object' && a.length && typeof(f) == 'function') { for (var i = 0; i < a.length; i++) { a[i] = f(a[i], i) || a[i]; } } }
  28. function ce (a, b) { if (typeof(a) != 'string') return; var c = document.createElement(a); foreach(b, function (d) { if (d.nodeType == 1) { c.appendChild(d); } else if (typeof(d) == 'object' && !d.length) { for (var k in d) { c.setAttribute(k, d[k]); } } else if (typeof(d) == 'string') { c.appendChild(document.createTextNode(d)); } }); return c; }
  29.  
  30. function checkLinks (a) {
  31.         if (!a) return;
  32.         if (a.nodeType == 3) {
  33.                 var b = a.nodeValue.match(/[a-zA-Z][a-zA-Z0-9+-.]+:\/\/[^\s]+/);
  34.                 if (!b) var b = a.nodeValue.match(/mailto:[^\s]+/);
  35.                 if (!b) var b = a.nodeValue.match(/magnet:[^\s]+/);
  36.                 if (!b) var b = a.nodeValue.match(/news:[^\s]+/);
  37.                 if (b && (a.parentNode.nodeName.toLowerCase() != 'a')) {
  38.                         var m = b[0];
  39.                         if (a.nodeValue.length == m.length) {
  40.                                 var el = ce('a', [
  41.                                         {'href': a.nodeValue,
  42.                                         'class': 'chanlinkify'},
  43.                                         a.nodeValue
  44.                                 ]);
  45.                                 if (config.targetBlank) el.setAttribute('target', 'blank');
  46.                                 a.parentNode.replaceChild(el, a);
  47.                         }
  48.                         else {
  49.                                 var c = a.nodeValue.indexOf(m);
  50.                                 if (a.nodeValue.substring(c-1, c) == '(') {
  51.                                         if (/(.*)\x29$/.test(m)) {
  52.                                                 m = m.match(/(.*)\x29$/)[1];
  53.                                                 c = a.nodeValue.indexOf(m);
  54.                                         }
  55.                                 }
  56.                                 if (a.nodeValue.substring(c-2, c) == '("') {
  57.                                         if (/(.*)\x22\x29$/.test(m)) {
  58.                                                 m = m.match(/(.*)\x22\x29$/)[1];
  59.                                                 c = a.nodeValue.indexOf(m);
  60.                                         }
  61.                                 }
  62.                                 if (a.nodeValue.substring(c-2, c) == '(\'') {
  63.                                         if (/(.*)\x27\x29$/.test(m)) {
  64.                                                 m = m.match(/(.*)\x27\x29$/)[1];
  65.                                                 c = a.nodeValue.indexOf(m);
  66.                                         }
  67.                                 }
  68.                                 var d = c+m.length;
  69.                                 if (c != 0) a.parentNode.insertBefore(document.createTextNode(a.nodeValue.substring(0, c)), a);
  70.                                 var el = ce('a', [
  71.                                         {'href': m,
  72.                                         'class': 'chanlinkify'},
  73.                                         m
  74.                                 ]);
  75.                                 if (config.targetBlank) el.setAttribute('target', 'blank');
  76.                                 a.parentNode.insertBefore(el, a);
  77.                                 if (d == a.nodeValue.length) a.parentNode.removeChild(a);
  78.                                 else {
  79.                                         a.nodeValue = a.nodeValue.substring(d);
  80.                                         checkLinks(a);
  81.                                 }
  82.                         }
  83.                 }
  84.         }else if (a.className == 'quote') {
  85.     checkLinks(a.childNodes[0])
  86.   }else if (a.className == 'spoiler'){
  87.    if(a.childNodes.length != 0) {
  88.     checkLinks(a.childNodes[0]);
  89.    }else {
  90.     removeSpoiler(a)
  91.    }
  92.  
  93.   }
  94. }
  95.  
  96. foreach($$('blockquote'), function (a) {
  97.         foreach(a.childNodes, function (b) {
  98.                 checkLinks(b);
  99.         });
  100. });
  101.  
  102. foreach($$('span.subject'), function (b) {
  103.         checkLinks(b.childNodes[0]);
  104. });
  105.  
  106. document.addEventListener('DOMNodeInserted', function (e) {
  107.         var t = e.target;
  108.         if (t.nodeType === 1 && t.nodeName.toLowerCase() === 'div') {
  109.                 if (t.className.split(' ').filter(function (cls) { return cls === 'postContainer'; }).length) {
  110.                         foreach($('blockquote', t).childNodes, function (a) {
  111.                                 checkLinks(a);
  112.                         });
  113.                 }
  114.         }
  115.         if (t.nodeType === 3 && t.parentNode.nodeName.toLowerCase() === 'blockquote') {
  116.                 checkLinks(t);
  117.         }
  118.         if (t.nodeName.toLowerCase() == 'blockquote') {
  119.                 foreach(t.childNodes, function (a) {
  120.                         checkLinks(a);
  121.                 });
  122.         }
  123. }, false);
  124.  
  125. function appendNextTextNode (a) {
  126.         var r = (a.parentNode.className == 'quote') ? a.parentNode : a;
  127.         if (r.nextSibling && (r.nextSibling.nodeName.toLowerCase() == 'br')) {
  128.                 if (r.nextSibling.nextSibling && (r.nextSibling.nextSibling.nodeType == 3)) {
  129.                         var b = r.nextSibling.nextSibling;
  130.                         var c = b.nodeValue.match(/^[^\s]+/);
  131.                         if (!c) return;
  132.                         a.href += c[0];
  133.                         a.childNodes[0].nodeValue += c[0];
  134.                         if (b.nodeValue.length == c[0].length) {
  135.                                 r.parentNode.removeChild(r.nextSibling);
  136.                                 r.parentNode.removeChild(b);
  137.                         }
  138.                         else {
  139.                                 r.parentNode.removeChild(r.nextSibling);
  140.                                 b.nodeValue = b.nodeValue.substring(c[0].length);
  141.                         }
  142.                 }
  143.         }
  144. }
  145.  
  146. function removeSpoiler(a) {
  147.   b = a.previousSibling
  148.   c = a.nextSibling
  149.   if(c.nodeType == 3) {
  150.     a.parentNode.removeChild(a)
  151.     var d = c.nodeValue.match(/^[^\s]+/);
  152.     if (!d) return;
  153.                 b.href = b.childNodes[0].nodeValue + d[0];
  154.     b.childNodes[0].nodeValue = b.href;
  155.     if (c.nodeValue.length == d[0].length) {
  156.                         c.parentNode.removeChild(c);
  157.                 }
  158.                 else {
  159.                         c.nodeValue = c.nodeValue.substring(d[0].length);
  160.                 }
  161.   }
  162. }
  163.  
  164. if (config.multiLineLinks) document.addEventListener('click', function (e) {
  165.         var t = e.target;
  166.         if ((t.className == 'chanlinkify') && e.shiftKey && e.ctrlKey) {
  167.                 e.preventDefault();
  168.                 e.stopPropagation();
  169.                 appendNextTextNode(t);
  170.         }
  171. }, false);
  172.  
  173. })();