1

I am trying to get code to execute every x amount of time and begin doing so after the page has loaded for the first time, while my debug throws no errors, the script doesn't actually do anything.

So, I am at a bit of a loss and I am thinking it has something to do with the syntax or the way the code is nested. Can you be so kind as to look at my code below and tell me what in the heck I am doing wrong and if there is a better way to fix it?

window.addEventListener('load', function()  {
setInterval(function setTitle(){
    var sInfo = document.getElementById("play_info");
    var iTags = sInfo.innerHTML.split("\">");
    var pTags = iTags[3].split("<");
    document.title = pTags[0];
    return setTitle;
},5000);
});

This is a code inside a Greasemonkey script that will run inside Firefox. Thank you.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
GµårÐïåñ
  • 2,846
  • 5
  • 31
  • 35

2 Answers2

1

Update (Now that target page was given):
The question code did not match the structure of the actual page. It typically would throw TypeError: iTags[3] is undefined errors.

Using DOM methods, to get the desired info, does the job. A working script is:

// ==UserScript==
// @name     _KROQ, song info to page title
// @include  http://betaplayer.radio.com/player/*
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
if (window.top != window.self)  //-- Don't run on frames or iframes.
    return;

window.addEventListener ('load', function () {
    setInterval (setTitle, 5000);
}, false);

function setTitle () {
    var songInfoNode = document.querySelector (
        "#play_info #oflw_shield div.sleeve.hori"
    );
    if (songInfoNode) {
        var songInfoText = songInfoNode.textContent.trim ();

        //console.log ("songInfoText", songInfoText);
        document.title = songInfoText;
    }
}



That code is "brittle", but it will work fine on a site that has the structure you seem to expect. However, the only "output" is that the page title might change. That doesn't happen?

One or more of the following is the script's immediate problem:

  • The page does not have the structure you are expecting, so the code throws exceptions. You say, "My debug throws no errors". Verify that you checked Firefox's error console, ControlShiftJ, with the display set to "Errors" or to "All".
  • Something else in the script, that you are not showing us, is the problem. Include or link to the complete Greasemonkey script.
  • The script is operating on <iframe>d content, so changing document.title will have no visible effect. Link to the target page.

For reference, here is more robust version of that code, that also has a console message, so that you can verify operation:

window.addEventListener ('load', function () {
    setInterval (setTitle, 5000);
}, false);

function setTitle () {
    console.log ("Running setTitle()."); 
    var sInfo = document.getElementById ("play_info");
    if (sInfo) {
        var iTags = sInfo.innerHTML.split ("\">");
        if (iTags  &&  iTags.length > 3) {
            var pTags = iTags[3].split ("<");
            if (pTags  &&  pTags.length) {
                document.title = pTags[0];
            }
        }
    }
}

Note that it's not good practice to parse HTML that way, but without seeing the actual target page (or at least the structure of the play_info node), we can't provide a specific alternative.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thank you for getting back to me on this. I know the code is pretty specific and "brittle" as you put it but it does have a very limited and specific function on that particular site. As you stated, it only parses the information from a DIV and then makes it the page title (may seem asinine but trust me, it has a real cosmetic purpose) and no it doesn't happen. Now when I run the code by itself without the use of the two events, it runs fine and does its thing. when trying to get the load event and timer to function, it won't. – GµårÐïåñ Jul 12 '13 at 20:46
  • I know its not the best way to parse the information but unfortunately due to the page design limitations, it was the cleanest way that I could think of. The `innerHTML` for that div is `

    Imagine Dragons - Radioactive

    ` so I just grabbed the first DIV I could and parsed the rest, make sense? If there is better way, PLEASE tell me and I would be grateful to you. But, however dirty, it works just the two events mentioned are the problem at the moment. Also, GM script is self loading and no iframe.
    – GµårÐïåñ Jul 12 '13 at 20:53
  • Like I said, what you have shown us is not the problem. **Something not in your question** is the (main) problem. Your code works, in a GM script, on a test page. **Provide the full script.** What version of FF and GM are you using? Link to the target page or save its HTML to Pastebin and link to that. – Brock Adams Jul 12 '13 at 20:56
  • Use my code above. Do you see the console messages? Install Firebug and use it to check, too. – Brock Adams Jul 12 '13 at 20:57
  • I tried the code you provide, thank you, and yet still getting no functionality. Just for sake of ease debugging, this is the location for the script to run http://betaplayer.radio.com/player/kroq and there is NOTHING in the error console in any section. Other than **Error: uncaught exception: DataCloneError: The object could not be cloned.** which I believe is unrelated to us, just caused by the page itself, unless I am mistaken. Suggestions? – GµårÐïåñ Jul 12 '13 at 21:00
  • Brock, this is the full script, I just left the header off but if you like I can provide that too. I am using Fx 22.0 and GM 1.10 nothing is left out here, I promise. I even added **grant setInterval and addEventListener** just to cover all bases, even though I am pretty sure its not needed. – GµårÐïåñ Jul 12 '13 at 21:07
  • See the updated answer. The question code WAS throwing errors. Anyway, using DOM methods against the linked-page structure works. Always loved KROQ, BTW. \*thumbs\*  (^_^) – Brock Adams Jul 13 '13 at 08:46
  • Thank you Brock, I appreciate it but honestly I ran the code with alerts in between and verified that it didn't and using the GM run to test the code, it worked fine, I am surprised you got an error but none the less I appreciate the effort and learned a new way to query the information, thank you for that. I will test the code right now and see how it goes. Yeah love KROQ, so glad to find a fellow fan, thumbs up indeed :) You can checkout my original here (http://tmp.myguardian.net/original_code.txt) – GµårÐïåñ Jul 13 '13 at 10:52
  • Brock, you are the man and I thank you very much. The code works beautifully and elegantly and I appreciate it very much. I removed the iframe check you had at the beginning though since it throws this error (Warning: Warning: use of return outside of functions is deprecated and may cause failures in future versions of Greasemonkey. Line: 11) but otherwise, works like a charm, thank you so much. – GµårÐïåñ Jul 13 '13 at 11:06
  • You want to ignore that warning and leave the iframe check in. That page apparently uses a lot of frames and this script could start bogging down your browser (depending on your machine and usage habits). – Brock Adams Jul 13 '13 at 11:10
0

As Brandon Boone stated, if you are looking for a pure JS solution to find cross-browser solutions for addEventListener, I suggest you use a solution like this.

var e = window.addEventListener ? window.addEventListener : window.attachEvent;
e('load',function(){ ... };
  • The OP stated Firefox. IE, and `attachEvent` don't apply to a Greasemonkey scenario anyway (with almost no exceptions). – Brock Adams Jul 12 '13 at 12:59
  • Ditto, thank you for the input however, will vault that as a possible technique in the future; however, at the moment, just dealing with Fx and GM, thank you. – GµårÐïåñ Jul 12 '13 at 20:54