TinyAiTweet1.3: Difference between revisions

From fridemar.ai
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 16: Line 16:
// @require http://code.jquery.com/jquery-latest.js
// @require http://code.jquery.com/jquery-latest.js


// @grant        CcBySa
// @grant        CcBySa Disclaimer, use and improve it at your own risk.


// ==/UserScript==
// ==/UserScript==
function ai() {
function ai() {
     var WikiMap = ['http://google.com/search?q=','https://aipedia.eu/wiki/','https://twitter.com/search?q=','https://twitter.com/hashtag/'],
     var WikiMap = ['http://google.com/search?q=','https://aipedia.eu/wiki/','https://twitter.com/search?q=','https://twitter.com/hashtag/'],
         i, j, k, len, llen, link, linkpatt,
         i, j, k, len, llen, link, linkpatt,
         linkText, linkTextArray = [], msg, myArray, node, txt, href, a, reg, span,
         linkText, linkTextArray = [], msg, myArray, node, txt, href, a, reg, span,
         lastLastIndex, textNodeXpath, textNodes;
         lastLastIndex, textNodeXpath, textNodes;
     textNodeXpath = './/text()[not(ancestor::a) and not(ancestor::head)and not(ancestor::option) and not(ancestor::textarea)]';
     textNodeXpath = './/text()[not(ancestor::a) and not(ancestor::head)and not(ancestor::option) and not(ancestor::textarea)]';
     textNodes = document.evaluate(textNodeXpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
     textNodes = document.evaluate(textNodeXpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
     len = textNodes.snapshotLength;
     len = textNodes.snapshotLength;
     reg = /^\s+$/g;
     reg = /^\s+$/g;
     linkpatt = /\b(?:([a-z0-9äöü]*(?:[A-ZÄÖÜ_]|_)[a-z0-9äöü_]*){2,})\b/g;
     linkpatt = /\b(?:([a-z0-9äöü]*(?:[A-ZÄÖÜ_]|_)[a-z0-9äöü_]*){2,})\b/g;
     for (i = 0; i < len; i++) {
     for (i = 0; i < len; i++) {
         node = textNodes.snapshotItem(i);
         node = textNodes.snapshotItem(i);
         txt = node.nodeValue; txt = txt.replace(reg, '');
         txt = node.nodeValue; txt = txt.replace(reg, '');
         if (linkpatt.test(txt)) {
         if (linkpatt.test(txt)) {
             span = document.createElement('span');
             span = document.createElement('span');
Line 51: Line 40:
                     myArray = linkpatt.exec(txt);
                     myArray = linkpatt.exec(txt);
                     if (!myArray) break;
                     if (!myArray) break;
                     link = myArray[0];
                     link = myArray[0];
                     linkTextArray = [link, ' °', ' ¹', ' ²', ' ³'];
                     linkTextArray = [link, ' °', ' ¹', ' ²', ' ³'];
Line 59: Line 47:
                     link = myArray[0];
                     link = myArray[0];
                     llen = link.length;
                     llen = link.length;
                     if (link.substring(0, 2) === '' && link.substring(llen - 2, llen) === '') {
                     if (link.substring(0, 2) === '' && link.substring(llen - 2, llen) === '') {
                         link = link.substring(2, llen - 2);
                         link = link.substring(2, llen - 2);
                     }
                     }
                     for (j = 0; j < Math.min(WikiMap.length, linkTextArray.length); j++) {
                     for (j = 0; j < Math.min(WikiMap.length, linkTextArray.length); j++) {
                         href = WikiMap[j] + link;
                         href = WikiMap[j] + link;

Latest revision as of 02:31, 16 December 2017

// ==UserScript==

// @name TinyAiTweet1.3

// @namespace http://tampermonkey.net/

// @version 1.3

// @description wikify

// @author Fridemar Pache aka AipediaFridemar

// @match *://*/*

// @require http://code.jquery.com/jquery-latest.js

// @grant CcBySa Disclaimer, use and improve it at your own risk.

// ==/UserScript== function ai() {

   var WikiMap = ['http://google.com/search?q=','https://aipedia.eu/wiki/','https://twitter.com/search?q=','https://twitter.com/hashtag/'],
       i, j, k, len, llen, link, linkpatt,
       linkText, linkTextArray = [], msg, myArray, node, txt, href, a, reg, span,
       lastLastIndex, textNodeXpath, textNodes;
   textNodeXpath = './/text()[not(ancestor::a) and not(ancestor::head)and not(ancestor::option) and not(ancestor::textarea)]';
   textNodes = document.evaluate(textNodeXpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
   len = textNodes.snapshotLength;
   reg = /^\s+$/g;
   linkpatt = /\b(?:([a-z0-9äöü]*(?:[A-ZÄÖÜ_]|_)[a-z0-9äöü_]*){2,})\b/g;
   for (i = 0; i < len; i++) {
       node = textNodes.snapshotItem(i);
       txt = node.nodeValue; txt = txt.replace(reg, );
       if (linkpatt.test(txt)) {
           span = document.createElement('span');
           lastLastIndex = 0;
           linkpatt.lastIndex = 0;
           try {
               while (true) {
                   myArray = linkpatt.exec(txt);
                   if (!myArray) break;
                   link = myArray[0];
                   linkTextArray = [link, ' °', ' ¹', ' ²', ' ³'];
                   span.appendChild(document.createTextNode(txt.substring(lastLastIndex, myArray.index)));
                   lastLastIndex = linkpatt.lastIndex;
                   linkText = myArray[1];
                   link = myArray[0];
                   llen = link.length;
                   if (link.substring(0, 2) ===  && link.substring(llen - 2, llen) === ) {
                       link = link.substring(2, llen - 2);
                   }
                   for (j = 0; j < Math.min(WikiMap.length, linkTextArray.length); j++) {
                       href = WikiMap[j] + link;
                       a = document.createElement('a');
                       a.setAttribute('href', href);
                       a.appendChild(document.createTextNode(linkTextArray[j]));
                       span.appendChild(a);
                   }
               }
               span.appendChild(document.createTextNode(txt.substring(lastLastIndex)));
               node.parentNode.replaceChild(span, node);
           } catch (e) {
               alert(e);
           }
       }
   }