Wiki Monkey

From ArchWiki
Revision as of 00:51, 5 November 2011 by Kynikos (Talk | contribs) (what a stupid name eh? :))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Wiki Monkey is a Greasemonkey script that helps editing wiki pages by letting you create Javascript functions to automatize repetitive operations.

Currently it adds a Correct source button next to the Show changes button in edit pages: by clicking on it the installed user functions are executed one by one.

Installation/Configuration

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Wiki Monkey#)

Main script

wikimonkey.user.js
// ==UserScript==
// @name Wiki Monkey
// @namespace wikimonkey
// @description Perform automatic actions when editing wiki pages
// @author Dario Giovannetti (Kynikos) <dariogiova@gmail.com>
// @version 1.0
// @icon http://www.dariogiovannetti.com/files/wiki-monkey.png
// @include https://wiki.archlinux.org/*
// ==/UserScript==

/*
 * === USER FUNCTIONS ===
 * 
 * Define here your functions, then add their names to the array at the bottom
 * of the file
 * To keep the namespace tidy, all user function names should be prefixed with "UF_"
 */

function UF_example() {
    alert('This is a demonstration function');
}

/*
 * === CORE FUNCTIONS ===
 * 
 * These are the standard functions, you don't need to modify them
 */

function _getQueryString() {
    var qa = location.search.substr(1).split('&');
    
    var qd = new Object();
    var s = new Array();
    for each (var p in qa) {
        s = p.split('=');
        qd[s[0]] = s[1];
    }
    
    return qd;
}

// Get query string parameters only once
var querystring = _getQueryString();

function getQueryString() {
    return querystring;
}

function getTitle() {
    return (querystring['title']) ? querystring['title'] : null;
}

function isSection() {
    return (querystring['section']) ? true : false;
}

function readSource() {
    return document.getElementById('wpTextbox1').innerHTML;
}

function writeSource(text) {
    document.getElementById('wpTextbox1').innerHTML = text;
}

function readSummary() {
    return document.getElementById('wpSummary').innerHTML;
}

function writeSummary(text) {
    document.getElementById('wpSummary').innerHTML = text;
}

function appendToSummary(text) {
    document.getElementById('wpSummary').innerHTML += text;
}

function main(functions) {
    if (document.getElementById('editform')) {
        var bcorrect = document.createElement('input');
        bcorrect.setAttribute('type', 'button');
        bcorrect.setAttribute('value', 'Correct source');
        bcorrect.style.marginRight = '0.33em';
        for each (var f in functions) {
            bcorrect.addEventListener("click", f, false);
        }
        
        var buttons = document.getElementById('wpSave').parentNode;
        buttons.insertBefore(bcorrect, buttons.getElementsByTagName('span')[0]);
    }
}

/*
 * Add user functions to the array passed to main(), they will be executed in
 * the same order. 
 * For example:
 * 
 * main([
 *     UF_myFunction1,
 *     UF_myFunction2,
 *     UF_myFunction3,
 * ]);
 * 
 */

main([
    UF_example,
]);

API

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Wiki Monkey#)

getQueryString()

getTitle()

isSection()

readSource()

writeSource(text)

readSummary()

writeSummary(text)

appendToSummary(text)

User functions

Migration to new templates

function UF_newTemplates() {
    var s = readSource();
    
    var re1 = /\{\{ *(?:[Cc]odeline|[Ff]ilename) *\|([^\|\=\{]+?)\}\}/g;
    var re2 = /\{\{ *[Cc]li *\|([^\|\=\{]+?)\}\}/g;
    var re3 = /\{\{ *[Cc]ommand *\|(?: *(?:command|name) *\=)?([^\|\=\{]+?)\|(?: *output *\=)?([^\|\=\{]+?)\| *prompt *\= *([\$#]) *\}\}/g;
    var re4 = /\{\{ *[Cc]ommand *\| *prompt *\= *([\$#]) *\|(?: *(?:command|name) *\=)?([^\|\=\{]+?)\|(?: *output *\=)?([^\|\=\{]+?)\}\}/g;
    var re5 = /\{\{ *[Cc]ommand *\|(?: *(?:command|name) *\=)?([^\|\=\{]+?)\|(?: *output *\=)?([^\|\=\{]+?)\}\}/g;
    var re6 = /\{\{ *[Ff]ile *\|(?: *name *\=)?([^\|\=\{]+?)\|(?: *content *\=)?([^\|\=\{]+?)\}\}/g;
    
    s = s.replace(re1, '{{ic|$1}}');
    s = s.replace(re2, '{{bc|$1}}');
    s = s.replace(re3, '{{hc|$3 $1|$2}}');
    s = s.replace(re4, '{{hc|$1 $2|$3}}');
    s = s.replace(re5, '{{hc|\$ $1|$2}}');
    s = s.replace(re6, '{{hc|$1|$2}}');
    
    writeSource(s);
    
    var test1 = s.match(/\{\{ *[Cc]odeline/g);
    var test2 = s.match(/\{\{ *[Ff]ilename/g);
    var test3 = s.match(/\{\{ *[Cc]li/g);
    var test4 = s.match(/\{\{ *[Ff]ile/g);
    var test5 = s.match(/\{\{ *[Cc]ommand/g);
    if (test1 || test2 || test3 || test4 || test5) {
        alert('Migration to new templates:\n' +
              ((test1) ? (test1.length + ' Codeline instances\n') : '') +
              ((test2) ? (test2.length + ' Filename instances\n') : '') +
              ((test3) ? (test3.length + ' Cli instances\n') : '') +
              ((test4) ? (test4.length + ' File instances\n') : '') +
              ((test5) ? (test5.length + ' Command instances\n') : '') +
              'require manual intervention.');
    }
}

Fix multiple line breaks

function UF_multipleLineBreaks() {
    var s = readSource();
    
    s = s.replace(/[\n]{3,}/g, '\n\n');
    
    writeSource(s);
}

Ideas/Development

Extensions

Functions (EF_-prefixed) with their own API that extend the core functionalities.

Bot-like operations

Greasemonkey can keep an array of simple values across page-loads by using GM_setValue(), GM_listValues(), GM_getValue() and GM_deleteValue(). The idea is combining this feature with javascript redirections over a stored list of wiki articles while performing automatic or semi-automatic (with preview) edits.

function UF_fooBar() {
    // Remember a value
    GM_setValue('foo' + 'bar');
    
    // Alert all stored values
    for each (var val in GM_listValues()) {
      alert(val + ' : ' + GM_getValue(val));
    }
    
    // Reset array
    for each (var key in GM_listValues()) {
      GM_deleteValue(key);
    }
}

Help

See User talk:Kynikos/Wiki Monkey#Questions/Help.

Bug reports

See User talk:Kynikos/Wiki Monkey#Bugs.

Feature/Function requests

See User talk:Kynikos/Wiki Monkey#Requests.