User:Quarl/wikipage.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:Quarl/wikipage. |
// User:Quarl/wikipage.js - "WikiPage" class for page name, etc. functionality
// requires: util.js
// Suppose we are editing [[Template talk:Foo bar 'blah']].
// WikiPage.editingP: true;
// ...
// wikiPage is a pre-initialized global variable using the current page's canonical URL.
// It uses the "Retrieved from" page hyperlink which is robust against redirections, editing, special characters, special pages.
//
// wikiPage.url: "http://en.wikipedia.org/wiki/Template_talk:Foo_bar_%27blah%27"
// wikiPage.qurl: "http://en.wikipedia.org/w/index.php?title=Template_talk:Foo_bar_%27blah%27"
// wikiPage.page: "Template talk:Foo bar 'blah'"
// wikiPage.article: "Foo bar 'blah'"
// wikiPage.namespace: "Template talk"
// wikiPage.namespaceNT: "Template"
// wikiPage.talkP: true
// wikiPage.nsTemplateP: true
// wikiPage.nsMainP: false
// wikiPage.nsUserP: false
// wikiPage.nsCategoryP: false
// wikiPage.nsSpecialP: false
// wikiPage.nsProjectP: false // (namespace "Wikipedia")
// To create new WikiPage object from a URL:
// var wp = new WikiPage("http://en.wikipedia.org/wiki/Article_Name");
// To create a new WikiPage object from a page name:
// var wp = new WikiPage(null, 'Article Name');
// <pre><nowiki>
/*
<div class="printfooter">
Retrieved from "<a href="http://en.wikipedia.org/wiki/Albert_Einstein">http://en.wikipedia.org/wiki/Albert_Einstein</a>"</div>
*/
// the "retrieved from" text contains the canonical article URL (even if we're looking at an edit or history page)
function getCanonPageURL0() {
return getElementsByClass("printfooter", null, 'div')[0].getElementsByTagName('a')[0].href;
}
function getUsername0() {
// read username from pt-userpage link.
// <li id="pt-userpage"><a href="/wiki/User:Quarl">Quarl</a></li>
return document.getElementById('pt-userpage').getElementsByTagName('a')[0].text;
}
var wpNamespaces = {'Media':1, 'Special':1,
'Talk':1,
'User':1, 'User talk':1,
'Project':1, 'Project talk':1,
'Image':1, 'Image talk':1,
'MediaWiki':1, 'MediaWiki talk':1,
'Template':1, 'Template talk':1,
'Help':1, 'Help talk':1,
'Category':1, 'Category talk':1,
'Portal':1, 'Portal talk':1
};
function WikiPage(url,page) {
if (!(this instanceof WikiPage)) return new WikiPage(url,page);
if (url) {
url = "" + url;
if (url.match(/^(?:http:\/\/en.wikipedia.org)?\/wiki\//)) {
this.pageQuoted = RegExp.rightContext;
} else if (url.match(/^(?:http:\/\/en.wikipedia.org)?\/w\/index.php\?title=([^&]+)/)) {
this.pageQuoted = RegExp.$1;
} else {
alert("WikiPage: Couldn't parse page name from url '"+url+"'");
return;
}
this.page = wpadecode(this.pageQuoted);
} else if (page) {
this.page = page.replace(/_/g, ' ');
this.pageQuoted = wpaescape(page);
} else {
alert("WikiPage: must specify url or page"); return;
}
this.url = 'http://en.wikipedia.org/wiki/' + this.pageQuoted;
this.qurl = 'http://en.wikipedia.org/w/index.php?title=' + this.pageQuoted;
// Get term on the left of ":". Not any string is a namespace though, only certain hardcoded ones!
if (this.page.match(/:/) && wpNamespaces[RegExp.leftContext]) {
this.namespace = RegExp.leftContext;
this.article = RegExp.rightContext;
} else {
this.namespace = ''; // (main)
this.article = this.page;
}
if (this.namespace == 'Talk') {
this.talkP = true;
this.namespaceNT = '';
} else if (this.namespace.match(/ talk$/)) {
this.talkP = true;
this.namespaceNT = RegExp.leftContext;
} else {
this.talkP = false;
this.namespaceNT = this.namespace;
}
if (this.article.match(/\//)) {
this.superarticle = RegExp.leftContext;
this.subarticle = RegExp.rightContext;
} else {
this.superarticle = this.article;
this.subarticle = '';
}
this.nsMainP = (this.namespaceNT == '');
this.nsMediaP = (this.namespaceNT == 'Media');
this.nsSpecialP = (this.namespaceNT == 'Special');
this.nsUserP = (this.namespaceNT == 'User');
this.nsPortalP = (this.namespaceNT == 'Portal');
this.nsImageP = (this.namespaceNT == 'Image');
this.nsMediaWikiP = (this.namespaceNT == 'MediaWiki');
this.nsTemplateP = (this.namespaceNT == 'Template');
this.nsHelpP = (this.namespaceNT == 'Help');
this.nsCategoryP = (this.namespaceNT == 'Category');
this.nsProjectP = (this.namespaceNT == 'Wikipedia');
this.talkPage = function() {
if (this.talkP) { return this; }
else if (this.namespaceNT == '') { return new WikiPage(null, 'Talk:'+this.article); }
else { return new WikiPage(null, this.namespaceNT+' talk:'+this.article); }
}
this.notalkPage = function() {
if (!this.talkP) { return this; }
else if (this.namespaceNT == '') { return new WikiPage(null, this.article); }
else { return new WikiPage(null, this.namespaceNT+':'+this.article); }
}
}
function getUsernameFromLink(link) {
return link && (new WikiPage(link)).superarticle;
}
function getRelevantUser0() {
if (wikiPage.nsUserP) return wikiPage.superarticle;
if (wikiPage.page == 'Special:Contributions') {
var cdiv = document.getElementById('contentSub');
if (cdiv.textContent == "For newbies") return null;
return getUsernameFromLink(cdiv.getElementsByTagName('a')[0]);
}
return null;
}
// Get the oldid ("permalink") for the current page.
// Note that we can't get oldid for editing pages, special pages, etc.
function getOldid0(perm) {
var tagid = perm ? 'ca-edit' : 't-permalink';
var tag = document.getElementById(tagid);
if (!tag) return null;
var href = tag.getElementsByTagName("a")[0].href;
if (!href) return null;
href.match(/&oldid=([0-9]+)/);
return RegExp.$1;
}
function getQueryVars0(){
var res = new Array();
var pairs = location.search.substring(1).split("&");
for(var i=0; i < pairs.length; i++){
var pair = pairs[i].split("=");
res[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return res;
}
function initWikiPage() {
window.wikiPage = new WikiPage(getCanonPageURL0());
// the window.* variables below are deprecated.
WikiPage.username = window.username = getUsername0();
WikiPage.relevantUser = window.relevantUser = getRelevantUser0();
WikiPage.editingP = window.editingP = Boolean(document.forms.editform); //document.title.match(/^Editing /)
WikiPage.newSectionP = window.newSectionP = (document.editform.wpSection.value == "new");
WikiPage.movePageP = window.movepageP = Boolean(document.forms.movepage);
WikiPage.previewP = window.previewP = Boolean(document.getElementById("wikiPreview"));
WikiPage.historyP = window.historyP = Boolean(document.getElementById("pagehistory"));
WikiPage.sandboxP = window.sandboxP = Boolean(window.wikiPage.page.match(/sandbox$/i));
WikiPage.permalinkP = window.permalinkP = Boolean(document.getElementById("t-ispermalink"));
WikiPage.oldid = window.pageOldid = getOldid0(permalinkP);
WikiPage.queryVars = window.queryVars = getQueryVars0();
}
// DEPRECATED:
function getPname() { return wikiPage.page; }
function getPnameNS() { return wikiPage.namespace; }
function getPnameNoNS() { return wikiPage.article; }
function pageIsSpecial() { return wikiPage.nsSpecialP; }
function get_query_vars() { return queryVars; }
addOnloadHook(initWikiPage);
// obsolete method 1:
// function getPname0() {
// var z=document.getElementById("content").childNodes;
// for (var n=0;n<z.length;n++) {
// if (z[n].className=="firstHeading") return z[n].textContent;
// }
// }
// function getPname1() {
// var t = getPname0();
// t = t.replace(/^Editing /,'');
// t = t.replace(/ \(section\)$/,'');
// return t;
// }
// obsolete method 2:
// document.title.substr(0, document.title.lastIndexOf(' - Wikipedia, the free'));
//</nowiki></pre>