User:Pgk/script
From Wikipedia, the free encyclopedia
< User:Pgk
//***********************************************
// 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
// 2.3 By pgk:
// - Added a diff template for wikilinks so [[sdfs+newver+oldver]] will go to the diff
// 2.4 By pgk:
// - Convert full difflinks into "--Diff Page--" link to page
// - Convert full version edit link into "--Version Edit Page--""
// 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);
client.munger.addRule("wiki-diff-link",/(http:\/\/..\.wikipedia\.org.*?&diff=.*?&oldid=\d*)/, simpleWikiDiffLink);
client.munger.addRule("wiki-version-edit-link",/(http:\/\/..\.wikipedia\.org.*?&action=edit&oldid=\d*)/, simpleWikiVersionEditLink);
// 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 simpleWikiDiffLink(matchText, containerTag) {
var reallink = matchText;
var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
"html:a");
anchor.setAttribute("href", reallink);
anchor.setAttribute("class", "chatzilla-link "+plugin.prefs["class"]);
insertHyphenatedWord("Diff Page", anchor);
containerTag.appendChild(document.createTextNode("--"));
containerTag.appendChild(anchor);
containerTag.appendChild(document.createTextNode("--"));
}
function simpleWikiVersionEditLink(matchText, containerTag) {
var reallink = matchText;
var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
"html:a");
anchor.setAttribute("href", reallink);
anchor.setAttribute("class", "chatzilla-link "+plugin.prefs["class"]);
insertHyphenatedWord("Version Edit Page", anchor);
containerTag.appendChild(document.createTextNode("--"));
containerTag.appendChild(anchor);
containerTag.appendChild(document.createTextNode("--"));
}
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];
}
var ary2 = wikiLink.match(/^(.*?)\+(\d+)\+(\d+)$/);
if (ary2) {
ary2[1] = escape(ary2[1].replace(/ /g, "_"));
wikiLink="http://en.wikipedia.org/w/index.php?title="+ary2[1]+"&diff="+ary2[2]+"&oldid="+ary2[3];
}
else
{
wikiLink = escape(wikiLink.replace(/ /g, "_"));
wikiLink = "http://en.wikipedia.org/wiki/"+wikiLink;
}
var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
"html:a");
anchor.setAttribute("href", 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"] );
}

