Page MenuHomePhabricator

Expose username and current title as JavaScript variables
Closed, ResolvedPublic

Description

It can be trickier than expected for a JavaScript extension to find seeminbly
obvious values:

*The user's name
*The name of the current page

These could easily be made available in an HTML FORM with INPUTs of type HIDDEN
and no SUBMIT. Each INPUT would have a CSS ID.

If a FORM is deemed a bad idea, SPANs with their CSS display set to invisible
would also work.

There are surely other values that js hackers have gone to extremes to extract
from the document structure, which have to be changed often as the document
structure is developed.

User preference settings would also be valuable for JavaScript extensions but
that may be beyong the scope of this feature request.


Version: unspecified
Severity: enhancement

Details

Reference
bz6156

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 9:19 PM
bzimport set Reference to bz6156.
bzimport added a subscriber: Unknown Object (MLST).

koneko wrote:

I think they are not that hard to recover :

the user name is in : id="pt-userpage" > A > text node
the page name is in : id="ca-nstab-main" > A > HREF parameter

for example, for the page name, you can use something like :

function retrievePageName() {

var temp = document.getElementById("ca-nstab-main")
temp = temp.getElementsByTagName("A") //return an array with only one case
temp = temp[0].getAttribute("HREF") //now we have a string "/wiki/articleName"
return temp.substring(7, temp.length) //return the string after cutting "/wiki/"

}

note that I didn't test this function, just wrote it "on the fly", and also I
have the bad habit to re-use variables anytime I can which can be ake code a bit
confusing to read)

A response to Darkoneko's comments:

the user name is in : id="pt-userpage" > A > text node

This one does seem simple. I use this:

document.getElementById('pt-userpage').getElementsByTagName('a').item(0).firstChild.nodeValue;

the page name is in : id="ca-nstab-main" > A > HREF parameter

This one is not so simple. Not every page has "ca-nstab-main". I use this:

document.getElementById('p-cactions').getElementsByTagName('ul').item(0).getElementsByTagName('li').item(0).firstChild.href;

Specifically, we need something which works on all skins, whether viewing,
diffing, editing, previewing, etc.

We might also have to think about what, if anything, it should return on special
pages.

I just noticed this new javascript section today:

<script type= "text/javascript">
var skin = "monobook";
var stylepath = "/skins-1.5";

var wgArticlePath = "/wiki/$1";
var wgScriptPath = "/w";
var wgServer = "http://en.wiktionary.org";

var wgCanonicalNamespace = "";
var wgPageName = "south-south-east";
var wgTitle = "south-south-east";
var wgArticleId = 201743;

var wgUserName = "Hippietrail";
var wgUserLanguage = "en";
var wgContentLanguage = "en";
</script>

It provides more than I asked for in a very nice way.