User:Visviva/creation.js

/*

Automatically create Korean entries based on ko-inline template. Copied shamelessly from User:Conrad.Irwin/creation.js



/** * Escape the values we are passing to AutoEdit */ //AutoEdit irritatingly doesn't undo %XX encodings, so we can't send them. //FIXME: Not a lot we can do about &s maybe patch AutoEdit. function get_escape (get) { return get.replace (/&/g,"%26"); } function clean_regexp (re) { return re.replace (/([\\\*\+\[\]\{\}\(\)\.~])/g,"\\$1"); } function clean_variable (va) { return va.replace (/([\\~])/g,"\\$1"); } function variable (needle, replacement) { return get_escape ('s~~' + clean_variable (replacement) + '~g;'); }

/** * Variables in the templates. */ // The page's language. function set_lang (lang) { return variable ('lang', lang); } function set_pos (pos) { return variable ('pos', pos); } function set_gloss (gloss) { return variable ('gloss', gloss); } function set_rom (rom) { return variable ('rom', rom) }

// The optional language parameter to give to a template (either '' for english, or '|lang=xx') function set_template_lang (lang) { return variable ('template-lang', 'ko'); } // This pages name. function set_origin (given) { return variable ('origin', wgTitle + (given ? "|" + given : "")); }

// The page we are about to create with links on individual words. function set_pagename_linked (link) { var pagename = (link.innerText || link.textContent);

var op = pagename; pagename = pagename.replace (/([ -])/g,"]]$1[[");

if (op != pagename) { pagename = +pagename+; }

return variable('pagename-linked', pagename); }

/** * Check if we know of a template that will do the job. * Use an explicit list to make it harder to subvert. */ function get_preload_template (form, lang) {

var prefix = 'User:Visviva/creation.js/';

switch (form) { case 'noun': return 'ko-basic-noun'; case 'verb': return 'ko-basic-verb'; default: return 'ko-basic'; } } /** * Convert a raw new link into a snazzy one. */ function add_create_button (details, link) {

var lang = 'ko'; var pos = ''; var rom = ''; var gloss = ''; var given_origin = false;

if (preload = get_preload_template(pos, lang) ) { try { link.href += '&preload=' + encodeURIComponent(preload) + '&autoedit=' + set_lang (lang) + set_template_lang (lang) + set_pos (pos) + set_rom (rom) + set_gloss (gloss) + set_origin (given_origin) + set_pagename_linked (link) + '&autosummary=' + encodeURIComponent(                       'Creating entry from ' + wgTitle                        + ' (Accelerated)' );

link.style.color = '#22CC00'; }catch(e) { //Something must've gone wrong.. *shrug }   }

}

/** * Recursively find first red link in "form-of" spans. * FIXME: would be better to return an array as multiple params often occur */ function find_red_link (span) { var poss = span.firstChild;

while (poss) { if(poss.nodeType == 1) { if (poss.nodeName.toUpperCase == 'A' && poss.className.indexOf('new') >= 0) { return poss; }           else if (recurse = find_red_link(poss)) { return recurse; }       }        poss = poss.nextSibling; }

return null; } /** * Recursively find anything tagged with "ko-inline" */

function find_makeme_spans { if (typeof(document.getElementsByClassName) == 'function') { return document.getElementsByClassName ('ko-inline'); } else { var spans = document.getElementsByTagName ('span'); var makes = new Array ; for (var i=0; i<spans.length; i++) { if (spans[i].className.match(/(^| +)ko-inline( +|$)/)) { makes.push (spans[i]); }       }        return makes; } } /** * Get the show on the road */ addOnloadHook( window.blahblah = function  {

poss = find_makeme_spans ;

for (var i = 0;i<poss.length; i++) { var link = find_red_link (poss[i]); if (link) {

add_create_button (poss[i].className.replace(/(^| +)ko-inline( +|$)/,'').split(' '), link); }

}

} );

$ (alert("working"));