1

I have this code snippet that works:

var oldInput = document.getElementsByName ("prio");
oldInput[0].setAttribute ("disabled", "disabled");

And this is the relevant, target-page HTML (corrected):

<div id="content">
    <p>(CONTENT)</p>
    <form>
      <p>Priority: <input name="prio" type="text" value="285"></p>
      <p>Success: <input name="succ" type="text" value="6"></p>
    </form>
</div>
<table border="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="user">
            &nbsp;
            <a href="#" class="nav" title="Logout John">Logout</a>

            &nbsp;(User: John)
            </td>
        </tr>
    </tbody>
</table>

See it at jsfiddle.net/Kc3BB/3.

I have 1 access-point for many users. but I want 5 users to be able to change the prio value. You can see that the username is in the footer.

Is it possible to do that in javascript with the code above for Firefox 2.x and without jQuery?

Example:

if USER (from footer) == JOHN || LUKE || JEFF || MAX || ANDY
do nothing

else
disable PRIO BOX (from content)
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
bernte
  • 1,184
  • 2
  • 19
  • 34
  • It will be pretty simple for Natasha to subvert this scheme. All she has to do is temporarily switch off Greasemonkey. ☻ – Brock Adams Sep 01 '12 at 21:29
  • no! Natasha can't switch off Greasmonkey :D this section is password protected :D – bernte Sep 01 '12 at 21:47

3 Answers3

1

Try this:

var tds = document.getElementsByTagName("TD",
    footerText = tds[tds - 1].textContent,
    user = footerText.match(/\(User: ([^\)]*\))/)[1].toUpperCase();

if (" JOHN LUKE JEFF MAX ANDY ".indexOf(" " + user + " ") !== -1) {
    // Do stuff
} else {
    // Do else stuff
    var oldInput = document.getElementsByName("prio");
    oldInput[0].setAttribute("disabled", "disabled");
}

Note: your pseudocode means the exact opposite of what you said in words. Fix the above code to your actual desires.

Edit: I couldn't remember if Firefox 2 supports indexOf on arrays. It does. So you can use this line instead:

if (["JOHN", "LUKE", "JEFF", "MAX", "ANDY"].indexOf(user) !== -1) {

Edit 2: I just saw your edit and changed the answer accordingly. I hope your HTML snippet actually reflects the page's source, or else finding that footer elements will need another method.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • 1
    sorry for my pseudocode! i thought its the best way to explain it. your code doesn't work. it gives me an error in the error-console on line 8 unterminated parenthetical (user = footerText.match(/\(User: ([^\)]*\)/)[1].toUpperCase();) – bernte Sep 01 '12 at 21:04
  • @bernte Fixed the code, sorry. And using pseudocode is fine, just be sure that it matches what you want ;) – MaxArt Sep 01 '12 at 21:07
  • Note that this will crash if the regex fails to match. (Not logged in, whitespace mismatch, other?) – Brock Adams Sep 01 '12 at 21:20
  • @BrockAdams Yes, of course. Then there would be the bigger problem of an unexpected footer. But, if bernte says the footer is like that, I'll take it as granted. – MaxArt Sep 01 '12 at 23:06
  • Actually, turns out the footer wasn't like that. See the updated question. – Brock Adams Sep 01 '12 at 23:11
1
var allowed_users = ['John', 'Luke', 'Jeff', 'Max', 'Andy'];
var user = /\(User: ([^)]*)\)/.exec(document.getElementById('footer').textContent​​​​​​​​)[1];
if(allowed_users.indexOf(user) === -1) // user not in allowed_users array, disable priority
{
    var oldInput = document.getElementsByName('prio')[0];
    oldInput.setAttribute('disabled', 'disabled');
}
Tyilo
  • 28,998
  • 40
  • 113
  • 198
1

This should work in Firefox 2.0:

//--- Make sure this list of names is all uppercase.
var usersWhoCanSetPriority = ['JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY'];

var bDisablePrio    = true;
var tdNodes         = document.getElementsByTagName ("TD");
for (var J = tdNodes.length - 1;  J >= 0;  --J) {
    var tdNode      = tdNodes[J];
    if (tdNode.className == "user") {
        var userName        = tdNode.textContent.replace (
            /^(?:.|\n|\r)+\(User:\s+([^)]+)\)(?:.|\n|\r)+$/i, "$1"
        ).toUpperCase ();

        if (usersWhoCanSetPriority.indexOf (userName) > -1) {
            bDisablePrio    = false;
        }
    }
}

if (bDisablePrio) {
    var oldInput = document.getElementsByName ("prio");
    oldInput[0].setAttribute ("disabled", "disabled");
}

See the updated Fiddle.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • hi brooks :) your code looks great! but there is on problem! i made the id="footer" just for jsfiddle so i recieve the error FooterNode has no properties. sorry that was my fault :( i replaced the footer with the original code.. i have only a class="user" http://jsfiddle.net/Kc3BB/3/ – bernte Sep 01 '12 at 21:41
  • See the updated answer. You should probably upvote everybody's answer too, since they were working off faulty information and answered (mostly) the original question as posed. (Might keep them from downvoting the question. :) ) – Brock Adams Sep 01 '12 at 22:02
  • Error: document.getElementsByClassName is not a function on Line: 11 .. seems that ff2.0 can't handle "getElementsByClassName" – bernte Sep 01 '12 at 22:10
  • Okay, try the new code. Even IE supports it (!!) so it should work in FF 2. – Brock Adams Sep 01 '12 at 22:48
  • doesn't work in ff2.x the box is disabled to all names! and i don't recieve any error in the error-console. but it works in ff10 – bernte Sep 01 '12 at 22:56
  • Well, I don't have a machine I can load FF2 on. You're going to have to upgrade or debug to see what part is failing. Start with logging or alerting all the variable values. First try `alert ([5, 6, 7].indexOf (7) )`. Does it give `2`? – Brock Adams Sep 01 '12 at 23:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16144/discussion-between-brock-adams-and-bernte) – Brock Adams Sep 01 '12 at 23:17