[Greasemonkey] Getting currently signed on user without parsing
html or dom
daddydave
daddydave at gmail.com
Sat May 27 01:51:57 EDT 2006
I have a function to get the name of the user currently signed on in
ma.gnolia.com. I haven't tested it in its current form, it may have
some bugs, but that is not really the point of this question.
function signedInUser() {
var masthead = document.getElementById("masthead");
if (!masthead) return '';
var p = masthead.getElementsByTagName("p");
if (!p) return '';
var pchild = p[0].firstChild;
if (!pchild) return '';
if (pchild.nodeName != "#text") return '';
var usermatch = new Array();
usermatch = ptext.nodeValue.match('Signed in as ([^ |]* |)');
if (!usermatch) return '';
return usermatch[1];
}
(By the way, can I just say "var usermatch;" instead of "var
usermatch = new Array();" ? It seems to work either way)
OK, finally my question:
I am curious if you think there could be a more robust way to get the
current user name other than parsing the HTML. If the site design
changes slightly (say they change the terminology from "Signed in" to
"Signed on"), it would break the script.
Using the Web Developer extension, I skimmed all known javascript to
see if there was any existing function on the page like
getCurrentUser() and it appears there is none.
Anything else I might check to find some way to get the signed on
username from the server directly? I know I am probably grasping at
straws here.
--------------------
David Eason
Ma.gnolia Condenser
http://www.freewebs.com/daddydave/download.htm
More information about the Greasemonkey
mailing list