User:Inductiveload/Template autoloader.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Inductiveload/Template autoloader. |
/*
* Does a lookup two previous Page:* from the current, extract the rh from
* its content, insert it in the header after trying to increase the rh page
* number. See the FIXME comment(s) for caveats.
*
* There is two way to use it, for both add
* importScript('User:Phe/Running header.js');
* to your javascript.
* then you can use it through a new icon to the toolbar, add to your js
* addOnloadHook(addRhButton); THIS WAY IS BROKEN
* or trough a link in the left regex menu, add to your .js
* regexTool('Running header', 'set_running_header()');
*/
function get_autoload_data(data)
{
try {
//the textbox to insert the text into
var textbox = document.getElementsByName('wpTextbox1')[0];
if (textbox && !data.query.pages["-1"]) {
for (var ids in data.query.pages) {
var content = data.query.pages[ids].revisions[0]['*'];
var r = new RegExp("\<pre id=\"autoload_template\">(.*)\</pre\>");
var match = r.exec(content);
if (match) {
var template_text = match[0];
// prepend the autoloaded text to the editbox content
// TODO: insert at cursor (possible?)
header.value = autoload_text + "\n" + header.value
}
break;
}
}
}
catch (err) { }
}
function create_script_obj(url)
{
var scriptObj = document.createElement("script");
scriptObj.setAttribute("type", "text/javascript");
scriptObj.setAttribute("src", url);
document.body.appendChild(scriptObj);
}
function autoloadTemplate( name )
{
var pagename = 'Template:' + name;
var url = wgServer + wgScriptPath
+ "/api.php" + "?action=query" + "&prop=revisions"
+ "&callback=get_autoload_data" + "&rvprop=content"
+ "&format=json" + "&titles=" + encodeURIComponent(pagename);
create_script_obj(url);
}