User:Ocee/wikilinks.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. |
![]() | Documentation for this user script can be added at User:Ocee/wikilinks. |
//***********************************************
// IceKarma's WikiLinks script for ChatZilla
// Version 1.2
// 1.2 by James Ross: fix the normal links by shunting the
// word-hyphenator as well.
// Version 2.0
// 2.0 By Glen Mailer:
// - Converted to new plugin API
// - Ripped out a whole load of unused stuff
// - Also Made to fit chatzilla coding pedantics
// 2.1 By Alphax:
// - Added basic template linking functionality
// 2.2 By Alphax:
// - subst: and pipes now handled correctly in templates
// This file is hereby placed by the author into the public domain.
plugin.id = "WikiLinks";
plugin.prefary = [
["class", "wiki-link", ""],
];
//
// Plugin management
//
plugin.init =
function init(glob) {
plugin.major = 2;
plugin.minor = 2;
plugin.version = plugin.major + "." + plugin.minor;
plugin.description =
"Munges wiki-links to be clickable in the output window";
plugin.prefary = plugin.prefary.concat(plugin.prefary);
}
plugin.disable =
function disable()
{
client.munger.delRule("wiki-link");
client.munger.delRule("wiki-template-link");
client.commandManager.removeCommands(plugin.commands);
display( plugin.id + " v" + plugin.version + " disabled.");
return true;
}
plugin.enable =
function enable()
{
client.munger.addRule("wiki-link", /(\[\[.+?\]\])/, insertWikiLink);
client.munger.addRule("wiki-template-link", /(\{\{.+?\}\})/, insertWikiTemplateLink);
// ugly hack to cause the munger to evaluate wiki-link before these rules
var tmp1 = client.munger.entries["link"];
var tmp2 = client.munger.entries["channel-link"];
var tmp3 = client.munger.entries["word-hyphenator"];
delete client.munger.entries["link"];
delete client.munger.entries["channel-link"];
delete client.munger.entries["word-hyphenator"];
client.munger.entries["link"] = tmp1;
client.munger.entries["channel-link"] = tmp2;
client.munger.entries["word-hyphenator"] = tmp3;
var cmdary = [
[ "wiki-links-class", cmdClass, CMD_CONSOLE, "[<className>]" ],
];
plugin.commands = client.commandManager.defineCommands(cmdary);
display( plugin.id + " v" + plugin.version + " enabled.");
return true;
}
//
// Mungers
//
function insertWikiLink(matchText,containerTag) {
var wikiLink = matchText;
var linkTitle;
wikiLink = matchText.replace(/^\[\[/, "");
wikiLink = wikiLink.replace(/\]\]$/, "");
linkTitle = wikiLink;
if (linkTitle.match(/\|/)) {
var ary = linkTitle.match(/^(.*?)\|(.*)$/);
wikiLink = ary[1];
linkTitle = ary[2];
}
wikiLink = escape(wikiLink.replace(/ /g, "_"));
var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
"html:a");
anchor.setAttribute("href", "http://en.wikipedia.org/wiki/" + wikiLink);
anchor.setAttribute("class", "chatzilla-link "+plugin.prefs["class"]);
insertHyphenatedWord(linkTitle, anchor);
containerTag.appendChild(document.createTextNode("[["));
containerTag.appendChild(anchor);
containerTag.appendChild(document.createTextNode("]]"));
}
function insertWikiTemplateLink(matchText,containerTag) {
var wikiLink = matchText;
var linkTitle;
wikiLink = matchText.replace(/^\{\{/, "");
wikiLink = wikiLink.replace(/\}\}$/, "");
linkTitle = wikiLink;
if (linkTitle.match(/^subst:/))
{
var ary = linkTitle.match(/^(subst:)(.*)$/);
wikiLink = ary[2];
}
if (linkTitle.match(/\|/))
{
if(linkTitle.match(/^subst:/))
{
var ary = linkTitle.match(/^(subst:)(.*?)\|(.*)$/);
wikiLink = ary[2];
}
else
{
var ary = linkTitle.match(/^(.*?)\|(.*)$/);
wikiLink = ary[1];
}
}
wikiLink = escape(wikiLink.replace(/ /g, "_"));
var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
"html:a");
anchor.setAttribute("href", "http://en.wikipedia.org/wiki/Template:" + wikiLink);
anchor.setAttribute("class", "chatzilla-link "+plugin.prefs["class"]);
insertHyphenatedWord(linkTitle, anchor);
containerTag.appendChild(document.createTextNode("{{"));
containerTag.appendChild(anchor);
containerTag.appendChild(document.createTextNode("}}"));
}
//
// Commands
//
function cmdClass(e) {
if ( null != e.linkclass )
plugin.prefs["class"] = e.linkclass;
display( "Current value: " + plugin.prefs["class"] );
}