As @Siguza suggested, I used MutationObservers.
But I didn't know how to inspect a mutation to know if its type is an "Url change". So I just checked with function for each mutation occuring.
I also had to set flags to check if the buttons I wanted to add were already added or not.
I had to create two ways to add the buttons I wanted to add, one is a simple function that checks if the target element (to contain the buttons) already exists, the second one is the MutationObserver targeted by the creation of this element.
So I get something like this :
// ==UserScript==
// @name Facebook_cleaner
// @namespace Facebook_cleaner
// @description cleans facebook timeline
// @include http://*.facebook.com/*
// @include https://*.facebook.com/*
// @require http://code.jquery.com/jquery-1.7.1.min.js
// @version 1
// @grant none
// ==/UserScript==
/*
** Variables :
*/
// Selectors :
var button_location = 'div[class="_2o3t fixed_elem"]';
var button_classes = '_42ft _4jy0 _11b _4jy3 _4jy1 selected _51sy';
// Flags :
var buttons_added = false;
var url_observer_launched = false;
var set = false;
/*
** Basic functions :
*/
function check_timeline()
{
if (/(allactivity)/g.test($(location).attr('href'))){
return true;
}
return false;
}
function reset(){
buttons_added = false;
set = false;
}
/*
** Evolved functions :
*/
function add_buttons(){
buttons_added = true;
set = true;
add_all_button();
add_one_button();
}
/*
** Event handling functions :
*/
function handling_url_change(mutations){
mutations.forEach(function (mutation){
if (check_timeline()){
// console.log("buttons added : " + buttons_added);
if (!buttons_added){
var element = $(document).find(button_location);
if (element && element.length > 0){
add_buttons();
}
}
}else if (set){
reset();
}
});
};
/*
** Mutation observers :
*/
var url_mutation_observer = new MutationObserver(handling_url_change);
/*
** Mutation observer starting functions :
*/
var dictionnary = {
childList: true,
subtree: true,
attributes: false,
characterData: false
};
// Must start url_mutation_observer.
function start_url_observer(){
url_mutation_observer.observe(document, dictionnary);
url_observer_launched = true;
}
/*
** Launching :
*/
function launch()
{
if (check_timeline()){
if (!buttons_added){
var element = $(document).find(button_location);
if (element && element.length > 0){
add_buttons();
}
}
}else if (set){
reset();
}
if (!url_observer_launched){
start_url_observer();
}
}
launch();
I hope than this will help somebody else.