I'm trying to make a script with Greasemonkey for the Facebook's Timeline log page.
I need to catch two kind of events :
1)The URL change (Since the changes are made with AJAX on Facebook and therefore the page isn't fully reloaded, and neither the script).
2)The appearance of some elements.
I tried to make two MutationObservers, one to catch URL changes, and the other to catche the elements appearance.
But it seems to trigger only one (the url_mutation_observer
).
Here is some of my code :
function handling_url_change(mutations){
mutations.forEach(function (mutation){
if (check_timeline()){
if (!buttons_added){
var element = $(document).find(button_location);
if (element && element.length > 0){
add_buttons();
}
}
}else if (set){
reset();
}
});
}
function handle_deletion(mutations){
mutations.forEach(function (mutation){
if (mutation.addedNodes){
for (var i = 0; i < mutation.addedNodes.length; i++){
if (isheader(mutation.addedNodes[i])){
mutation.addedNodes[i].remove()
}else if (isactivity(mutation.addedNodes[i])){
delete_activity(mutation.addedNodes[i]);
}
return true;
}
}
});
return true;
}
/*
** Mutation observers :
*/
var url_mutation_observer = new MutationObserver(handling_url_change);
var delete_mutation_observer = new MutationObserver(handle_deletion);
I have some questions :
1) Isn't each MutationObserver catching every mutation?
2) Is the addition of a button (like my script does) counted as a mutation?
3) If I add an element with a callback function triggered by the addition of such element. Isn't there a risk of recursion and infinite loop?
4) Is that possible to catch only the URL change with a MutationObserver, and to catch the appearance of only some kinds of elements with the other one? (To be sure that each one catches only what he needs to catch, I didn't know how to do and I checked the url with a function inspecting the page, and not with a function checking if the mutation is a "UrlChangeMutation" or something like that).
5) What could I do?
NB:
If you want too, here is the full script with console.log()
calls for debugging.
http://dpaste.com/3NWV08J
NB2:
If you also want this script without the console.log()
calls :
http://dpaste.com/3AH8YF5