[Greasemonkey] Getting currently signed on user without parsing html or dom

esquifit esquifit at googlemail.com
Sun May 28 01:12:06 EDT 2006


This does not respond your question; however I think it can improve
readability an easy of maintenance of your code in case you should ever need
to make adjustment in the event of a change of the site design.

/* not tested! */
function signedInUser() {

    var xpath = "//*[@id='masthead']/p/a[1]/text()",
        regexp = 'Signed in as ([^ |]* |)',
        usermatch = new Array();

    login_node = document.evaluate(
        xpath, document, null,
        XPathResult.ANY_UNORDERED_NODE_TYPE,
        null ).singleNodeValue;

    usermatch = login_node?login_nod.nodeValue.match(regexp):null;
    return usermatch?usermatch[1]:'';
}

In addition, you can check for other attributes of the node you' re
attempting to get at, such as length, background colour, etc.  You can also
spot certain invariable elements whose relative position to your element
remains presumably constant, for example the FAQ link next to the Sign In
text; in this case you'd compute the path to your element from this one
instead of the html root.

Disclaimer: I haven't got an account on ma.gnolia.com, so I haven't tested
this approach.

2006/5/27, daddydave <daddydave at gmail.com>:
>
> 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.
>


More information about the Greasemonkey mailing list