0

I am trying to make a greasemonkey script that will run the following code. I have tested it and it definitely works via URL (javascript: blah blah;) minus the comment of course:

// Your country code
var cc = 'net';

var j = document.getElementsByTagName('td');
for (i=0; i <= j.length; i++) {
    if (j[[i]].innerHTML.indexOf('points - punishment #A') > 0 && j[[i]].innerHTML.length == 30) {
        var k = j[[i]].innerHTML;
        j[[i]].innerHTML = k.slice(0, 23) + '<a href="http://www.example.' + cc + '/admin.php?screen=affront_tool&mode=view_punishment&punishment_id=' + k.slice(25) + '">' + k.slice(23) + '</a>';
    }
    else if (j[[i]].innerHTML.indexOf('points - punishment #A') > 0 && j[[i]].innerHTML.length == 29) {
        var k = j[[i]].innerHTML;
        j[[i]].innerHTML = k.slice(0, 22) + '<a href="http://www.example.' + cc + '/admin.php?screen=affront_tool&mode=view_punishment&punishment_id=' + k.slice(24) + '">' + k.slice(22) + '</a>';
    }
    else if (j[[i]].innerHTML.indexOf('points - punishment') > 0 && j[[i]].innerHTML.length <= 67) {
        var k = j[[i]].getElementsByTagName('a')[0];
        var l = 'http://www.example.' + cc + '/admin.php?screen=affront_tool&mode=view_punishment&punishment_id=' + k.getAttribute('href').slice(19);
        k.setAttribute('href', l);
    }
}

Unfortunately this script works on an element of the page dynamically generated up to ten seconds after load (expect about three), which makes things difficult. I've tried numerous ways of attaching the script as an "onload" attribute to the body with a setTimeout of ten thousand ms, to no avail. Perhaps I should be trying a different method, or I've repeated a simple mistake in the process? Could someone show me how they would do it?

lpd
  • 2,151
  • 21
  • 29

1 Answers1

2

I would include the jquery library on your page and make your code into a function then do:

$(document).ready(function () {
           setTimeout('myFunction();', 10000);
        });

In this case the function name is "myFunction()"

Avitus
  • 15,640
  • 6
  • 43
  • 53
  • Hmm, never used Jquery before. Presuming the page doesn't include it natively, would these instructions be correct to include it: http://stackoverflow.com/questions/859024/how-can-i-use-jquery-in-greasemonkey – lpd Mar 06 '11 at 01:56
  • I would just go to http://jquery.com/ and click download and put the .js file in your site and make reference to it in your page. – Avitus Mar 06 '11 at 01:59
  • I don't have access to modify the page myself, but if worst comes to worst, I'm sure it could get done. Cheers :) – lpd Mar 06 '11 at 02:05
  • Tried it, got it working :) Just linking a few things I found useful in implementation: http://stackoverflow.com/questions/1566595/can-i-use-multiple-versions-of-jquery-on-the-same-page http://stackoverflow.com/questions/2303147/injecting-js-functions-into-the-page-from-a-greasemonkey-script-on-chrome – lpd Mar 06 '11 at 05:30
  • http://stackoverflow.com/questions/2246901/include-jquery-inside-greasemonkey-script-under-google-chrome – lpd Mar 06 '11 at 11:15