MediaWiki:Common.js: Difference between revisions

From fridemar.ai
Jump to navigation Jump to search
No edit summary
(pasted MediaWiki:Common.js from sandbox.fridemar.ai first part to here)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load.  
 
Todo: Update this page from sandbox.fridemar.ai
 
*/
 
/* Any JavaScript here will be loaded for all users on every page load.
 
Todo: extending
// WikiMap = [
'https://google.com/search?q=',
'https://etherpad.wikimedia.org/p/',
'https://wikipedia.org/wiki/',
'https://twitter.com/search?q=',
'https://twitter.com/hashtag/',
'https://www.youtube.com/results?search_query=',
'https://fridemar.ai/wiki/'
] by component https://communitywiki.org/wiki/
 
// linkTextArray = [link, ' (e', ' w', ' t', ' h', ' y', ' f)'] by component ' c'
                   
*/
 


function ai() {
function ai() {
     var WikiMap = ['http://google.com/search?q=', 'https://aipedia.eu/wiki/'],
     var WikiMap = [ // Done: insert https://communitywiki.org/wiki/
        i, j, k, len, llen, link, linkpatt,
 
'https://google.com/search?q=',
'https://etherpad.wikimedia.org/p/',
'https://communitywiki.org/wiki/',
'https://wikipedia.org/wiki/',
'https://twitter.com/search?q=',
'https://twitter.com/hashtag/',
'https://www.youtube.com/results?search_query=',
'https://fridemar.ai/wiki/'  
],      
 
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;
Line 28: Line 62:


                     link = myArray[0];
                     link = myArray[0];
                     linkTextArray = [link, ' °', ' ¹', ' ²', ' ³'];
                   
                     linkTextArray = [link, ' (e', ' c', ' w', ' t', ' h', ' y', ' f)']; // Done: insert ' c'
                   
                     span.appendChild(document.createTextNode(txt.substring(lastLastIndex, myArray.index)));
                     span.appendChild(document.createTextNode(txt.substring(lastLastIndex, myArray.index)));
                     lastLastIndex = linkpatt.lastIndex;
                     lastLastIndex = linkpatt.lastIndex;
Line 35: Line 71:
                     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);
                     }
                     }
Line 54: Line 90:
         }
         }
     }
     }
    msg = 'alca enabled with:\n' + '\nMainLink:' + WikiMap[0] + '\n0:' + WikiMap[1] + '\n1:' + WikiMap[2] + '\n2:' + WikiMap[3] + '\n3:' + WikiMap[4] + '\n:' + '\n:' + 'Intermediate Webpages:' + '\n:' + document.location;
}
}




Line 129: Line 162:
     //ModifySidebar( 'remove', 'toolbox', 'Upload file', 'http://en.wikipedia.org/wiki/Special:Upload' );
     //ModifySidebar( 'remove', 'toolbox', 'Upload file', 'http://en.wikipedia.org/wiki/Special:Upload' );


     ModifySidebar('add', 'toolbox', 'ai1.2', 'javascript:ai()');
     ModifySidebar('add', 'toolbox', 'WikifyWeb', 'javascript:ai()');
}
}


jQuery(CustomizeModificationsOfSidebar);
jQuery(CustomizeModificationsOfSidebar);

Latest revision as of 20:19, 17 November 2023

/* Any JavaScript here will be loaded for all users on every page load. 

Todo: Update this page from sandbox.fridemar.ai

*/

/* Any JavaScript here will be loaded for all users on every page load. 

Todo: extending
 
// WikiMap = [
'https://google.com/search?q=', 
'https://etherpad.wikimedia.org/p/',
'https://wikipedia.org/wiki/',
'https://twitter.com/search?q=',
'https://twitter.com/hashtag/',
'https://www.youtube.com/results?search_query=',
'https://fridemar.ai/wiki/' 
] by component https://communitywiki.org/wiki/

// linkTextArray = [link, ' (e', ' w', ' t', ' h', ' y', ' f)'] by component ' c'
                    
*/


function ai() {
    var WikiMap = [ // Done: insert https://communitywiki.org/wiki/

'https://google.com/search?q=',
'https://etherpad.wikimedia.org/p/',
'https://communitywiki.org/wiki/',
'https://wikipedia.org/wiki/',
'https://twitter.com/search?q=',
'https://twitter.com/hashtag/',
'https://www.youtube.com/results?search_query=', 
'https://fridemar.ai/wiki/' 
],        

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, ' (e', ' c', ' w', ' t', ' h', ' y', ' f)']; // Done: insert ' c'
                    
                    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);
            }
        }
    }
}



function ModifySidebar(action, section, name, link) {
    try {
        switch (section) {
            case 'languages':
                var target = 'p-lang';
                break;
            case 'toolbox':
                var target = 'p-tb';
                break;
            case 'navigation':
                var target = 'p-navigation';
                break;
            default:
                var target = 'p-' + section;
                break;
        }

        if (action == 'add') {
            var node = document.getElementById(target)
                .getElementsByTagName('div')[0]
                .getElementsByTagName('ul')[0];

            var aNode = document.createElement('a');
            var liNode = document.createElement('li');

            aNode.appendChild(document.createTextNode(name));
            if (link.startsWith('javascript:')) {
                aNode.setAttribute('onclick', link);
            } else {
                aNode.setAttribute('href', link);
            }

            liNode.appendChild(aNode);
            liNode.className = 'plainlinks';
            node.appendChild(liNode);
        }

        if (action == 'remove') {
            var list = document.getElementById(target)
                .getElementsByTagName('div')[0]
                .getElementsByTagName('ul')[0];

            var listelements = list.getElementsByTagName('li');

            for (var i = 0; i < listelements.length; i++) {
                if (
                    listelements[i].getElementsByTagName('a')[0].innerHTML == name ||
                    listelements[i].getElementsByTagName('a')[0].href == link
                ) {
                    list.removeChild(listelements[i]);
                }
            }
        }


    } catch (e) {
        // let's just ignore what's happened
        return;
    }
}

function CustomizeModificationsOfSidebar() {
    // adds [[Special:CategoryTree|Special:CategoryTree]] to toolbox
    //ModifySidebar( 'add', 'toolbox', 'CategoryTree', 'http://en.wikipedia.org/wiki/Special:CategoryTree' );

    // removes [[Special:Upload|Special:Upload]] from toolbox
    //ModifySidebar( 'remove', 'toolbox', 'Upload file', 'http://en.wikipedia.org/wiki/Special:Upload' );

    ModifySidebar('add', 'toolbox', 'WikifyWeb', 'javascript:ai()');
}

jQuery(CustomizeModificationsOfSidebar);