-1

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.

  1. Selecting the Story options button to open the menu (it works)
  2. Clicking the “Delete” item in the submenu, it works putting a little delay,
  3. 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);
ldz
  • 2,217
  • 16
  • 21
gialloporpora
  • 199
  • 2
  • 11
  • @Mark Stroeven I know that exists APIs to do these things, but I was searching the fastest, and simplest, way to do this, not the best possible. My goal is not to release an add-on/GMscript to clear the timeline but only to remove quickly all my post. Using APIs requires to write good code, and I am not a JS developer, and creating API keys, instead the few lines written above requires for me only some minutes. But I have asked the question because I am wondered of the strange behavior (it works for single steps but not when are put together) and your reply is what I am searching :-) Thanks – gialloporpora Dec 15 '15 at 16:51
  • Well if you really want to do this in a bad way, try looking at selenium to generate your code :) http://www.seleniumhq.org/ – Mark Stroeven Dec 17 '15 at 06:15

1 Answers1

1

Using javascript to automate commands is not the proper way to do this! Facebook has built in security measures for these practices, and should you succeed in doing it your way the main question qould be why would you do it in such a inefficient way.

We have API's / libraries (official ones) for a reason. You should use the facebook graph API for doing such things. https://developers.facebook.com/docs/graph-api

Mark Stroeven
  • 676
  • 2
  • 6
  • 24