Greasemonkey script fails when unusual content loading is used
I'm trying to write a Greasemonkey script for Facebook and am having some problems with the funky page / content load they are doing (I don't quite understand this - many links actually just change the GET, but I think they are doing some sort of server redirect. to make the url look the same in the browser?). Basically the only requirement is to install it GM_log()
yourself in the script. If you hit Facebook, even with facebook.com/* as your template, it often fails. Is there anything I can do, or is the "page load" idea captured in Greasemonkey and FB is "tricking" it into not working using a single URL?
If I try to do some basic content manipulation like this:
GM.log("starting");
var GM_FB=new Object;
GM_FB.birthdays = document.evaluate("//div[@class='UIUpcoming_Item']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (i = GM_FB.birthdays.snapshotLength - 1; i >= 0; i--) {
if (GM_FB.birthdayRegex.test(GM_FB.birthdays.snapshotItem(i).innerHTML)) {
GM_FB.birthdays.snapshotItem(i).setAttribute('style','font-weight: bold; background: #fffe88');
}
}
As a result, it turns out that sometimes you only manually refresh the page. Pulling the Firebug console and forcing code execution works fine. Note that this is not due to late loading of some parts of the DOM: I add some code later to wait for the corresponding elements, and most importantly, the message is never logged for certain transitions. For example, when I switch from messages to the news channel and back.
a source to share
Can't they use ajax to load content in a div ? You can find the item being updated using Firebug for example.
When you click something and the url changes but with #
in the url and after that some text means the text is not a path, this is a parameter, the browser will not change your page as GreaseMonkey injects a script to load the page , it won't re-enter because the page doesn't reload. As in your example, the url facebook.com/#!/sk=messages
doesn't move away from facebook.com/
, it doesn't fire the event window.load
. So you need to find which element is changing and add an event listener to that element, you can use Firebug as I mentioned earlier.
After you know which element is receiving content, you need to add an event listener to that element, not to the page (GreaseMonkey only adds the window load event).
So you have a GM script you will have ("air code")
document.getElement('dynamic_div').addEvent('load', /*your script*/);
a source to share