Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Greasemonkey script to count and display the number of links in a DIV on a Goal Line Blitz web page...

Posted on 2010-09-10
11
Medium Priority
?
695 Views
Last Modified: 2013-12-07
I've asked this question before but I seem to have stumped the Experts.  I'll ask it again in a (I hope) more understandable form.

I have written a Greasemonkey script that changes the titles of the management team of a GLB team page.  The code is listed below.  This GM script changes the names of the team management from those used natively by GLB to those wanted by the team owner.  This script currently works perfectly.

I would now like to add more functionality to this GM script.  In addition to changing the management titles I would like it also to count up and display the total number of GMs (now changed to Whores) on the page.

Where it currently shows:

Whores: We_Rule, VonKortez, bmac1188

I would like it to show the total number of Whores like this:

Whores (3): We_Rule, VonKortez, bmac1188

The existing View Page Source of that section follows below.

			<div id="team_gms">GMs: <a href="/game/home.pl?user_id=5755">We_Rule</a>, <a href="/game/home.pl?user_id=144617">VonKortez</a>, <a href="/game/home.pl?user_id=286298">bmac1188</a></div>

Open in new window



This new functionality needs to be integrated into the current name-changing GM script shown below.  In addition, this needs to work as a Greasemonkey script.

I hope I made the requirements clear.  If not, please let me know how I can clarify it.
// ==UserScript==
// @name   GLB Management Name Change 
// @namespace  ErDrRon
// @description Replace OC and DC on team page
// @include  http://goallineblitz.com/game/team.pl?team_id=6144
// ==/UserScript==


var e = document.getElementsByTagName("body")[0].getElementsByTagName("*");
            for(var i=0;i<e.length;i++) {
                  if(e[i].innerHTML) {
                        e[i].innerHTML = e[i].innerHTML.replace(/OC:/g,"Bartender:").replace(/DC:/g,"Bouncer:").replace(/CFO:/g,"Mr. Money Bags:").replace(/Recruiters:/g,"Pimps:").replace(/Co Owned by/g,"lolowners bitch:").replace(/GMs:/g,"Whores:").replace(/Owned by/g,"lolowner:").replace(/Head Coach/g,"Disc Jockey:").replace(/ST Coach:/g,"Yoga Instructer:").replace(/Scouts:/g,"Talent Evaluators:").replace(/Asst OC/g,"Janitor").replace(/Asst DC/g,"Security").replace(/Note from the Owner/g,"Note from the lolowner");
                  }
            }

Open in new window

0
Comment
Question by:ErDrRon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 3

Accepted Solution

by:
T1750 earned 2000 total points
ID: 33652025
var gms = document.evaluate( 'count(//div[@id = "team_gms"]//a)', document, null, XPathResult.NUMBER_TYPE, null).numberValue;

That's how you can count them.
0
 
LVL 3

Expert Comment

by:T1750
ID: 33652027
I'm sure you can fill in the rest.
0
 

Author Comment

by:ErDrRon
ID: 33653337
I created a quick GM script using your code and reporting the results in an alert box.  It seems to function until the number of GMs exceeds 99.  Is their a limitation to the magnitude of the variable that an alert can present?

I've included the code for the test script below.

Thanks!

// ==UserScript==
// @name           Test GM count
// @namespace      ErDrRon
// @description    Test GM count
// @include        http://goallineblitz.com/game/team.p*
// ==/UserScript==


var gms = document.evaluate( 'count(//div[@id = "team_gms"]//a)', document, null, XPathResult.NUMBER_TYPE, null).numberValue;
alert(gms);

Open in new window

0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 3

Expert Comment

by:T1750
ID: 33653371
Not that I'm aware of, seems like a bug. You could try running the same thing on unsafewindow.
0
 
LVL 3

Expert Comment

by:T1750
ID: 33653377
The only other option available to you is to walk the DOM tree, collect the child nodes that are anchors and count them with an accumulator. Using innerHTML is a hack and often breaks things.
0
 
LVL 3

Expert Comment

by:T1750
ID: 33653408
Something like:

var gm_container = document.getElementById('team_gms');
var gms= 0;
for (i = 0; i < gm_container.childElementCount; i++) { if (gm_container.childNodes[i].nodeName.toLowerCase() == "a") gms++; }

But problems with the DOM do often happen if you write to innerHTML.
0
 

Author Comment

by:ErDrRon
ID: 33653715
I tried the above code but it doesn't seem to work.  It reports out anywhere from 1/2 to 1/4 of the actual number of GMs.  Thoughts?
0
 

Author Comment

by:ErDrRon
ID: 33653718
What is an unsafe window?
0
 

Author Comment

by:ErDrRon
ID: 33653756
I actually went back and re-counted the one I thought was incorrect at 99 and found it was actually correct.  the script works now.  Thanks You!!

I've included the completed script below.
// ==UserScript==
// @name           GLB Management Name Change 
// @namespace      ErDrRon
// @description    Replace management titles on lolgm Trophy Whores team page
// @version        1.0.1
// @date           2010-09-05
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @include        http://goallineblitz.com/game/team.pl?team_id=6144
// ==/UserScript==

var gms = document.evaluate( 'count(//div[@id = "team_gms"]//a)', document, null, XPathResult.NUMBER_TYPE, null).numberValue;

var e = document.getElementsByTagName("body")[0].getElementsByTagName("*");
            for(var i=0;i<e.length;i++) {
                  if(e[i].innerHTML) {
                        e[i].innerHTML = e[i].innerHTML.replace(/OC:/g,"Bartender:").replace(/DC:/g,"Bouncer:").replace(/CFO:/g,"Mr. Money Bags:").replace(/Recruiters:/g,"Pimps:").replace(/Co Owned by/g,"lolowners bitch:").replace(/GMs:/g,"Whores (" + gms + "):").replace(/Owned by/g,"lolowner:").replace(/Head Coach/g,"Disc Jockey:").replace(/ST Coach:/g,"Yoga Instructer:").replace(/Scouts:/g,"Talent Evaluators:").replace(/Asst OC/g,"Janitor").replace(/Asst DC/g,"Security").replace(/Note from the Owner/g,"Note from the lolowner");
                  }
            }

Open in new window

0
 

Author Closing Comment

by:ErDrRon
ID: 33653766
Great help.  Thanks!
0
 
LVL 3

Expert Comment

by:T1750
ID: 33653768
Thoughts are that I haven't seen the fully formatted output of your gm game so I can't know the exact code to parse or if I have nodes to ignore or have to do depth traversal, but as you see, XPATH makes life easy so none of that matters I'm glad it works for you.
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question