i would like to clear my old post in my Facebook timeline. I have tried using JS to automate this operation, but I am not able to make the goal. The strange thing, for me, is that if I try the single steps they works fine, when I put all together it doesn't work.
- Selecting the Story options button to open the menu (it works)
- Clicking the “Delete” item in the submenu, it works putting a little delay,
Selecting - and clicking - the confirm button to delete the post (it doesn't workss)
Trying to selecting the Confirm button (Delete Post) it is null. But if I try to execute the step no. 3 after 1 and 2 it works.
This is my code, I run it from the Firefox Scratchpad (Shift+F4) in a specific year section of the timeline (click on a year on facebook.com/me)
// Replace these values if your locale in FB is different from en-US
let l10nALabel="Story options";
let l10nDelete = "Delete";
let l10nCButton = "Delete Post";
// Change this value if the script doesn't works properly
let delay = 1000;
function sleep(delay) {
// Code snippet taken from this post https://stackoverflow.com/questions/1183872/put-a-delay-in-javascrip https://stackoverflow.com/questions/1183872/put-a-delay-in-javascript
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
getElementByText = function(selector, text){
els = document.querySelectorAll(selector);
for (i=0; i<els.length; i++) if ((els[i].textContent==text) && (els[i].children.length==0)) break;
if (i==els.length) return null;
else return els[i];
}
deletePost = function(post, delay){
post.click();
sleep(delay);
let delb = getElementByText("span", l10nDelete);
console.log(delb);
delb.click();
sleep(delay);
let cb = getElementByText("button", l10nCButton);
console.log(cb);
cb.click();
}
let a = document.querySelectorAll('[aria-label="' + l10nALabel + '"]')
deletePost(a[0], delay);