The following script is aimed to run on facebook.com's conversations page (the page in which a user can see all its conversations).
The script's purpose is to automize the "delete conversation" process which naturally includes 4 clicks and can be tiresome and time wasting when you have hundreds of conversations --- deletion will be done from keyboard by hitting the "D" key.
I run the script with Greasemonkey.
The script is comprised of 4 main segments:
- Listen to all "D" key hitting events.
- Click the link that opens the modal with the "Delete" option (the small chainwheel).
- Clink the "Delete" link in that modal (it will open a second modal "delete confirmation").
- Click the new "Delete" link in that modal, to confirm conversation deletion.
My script
document.addEventListener('keydown', (k)=>{
if ( k.keyCode === 68 ) {
console.log('keydown: D');
return dC();
}
});
let dC = ()=>{
document.querySelector('._5blh._4-0h').click();
document.querySelector('.uiContextualLayer > [id^="js"] > div > ul > li:nth-child(4)').click();
setTimeout(()=>{ document.querySelector('._3quh._30yy._2t_._3ay_._5ixy').click(); }, 500);
};
As a beginner, I tried put parts of the code in functions, I tried iterating with for
instead forEach()
, I tried using return dC()
or return false
under the dC()
call. All of these yielded the same results so I walked in circles not understanding (or denying) a deeper logical error which I sorely miss.
Reproducing
Install as Greasemonkey script, (match as https:www.facebook.com/*
just for the test), go to conversations page and hit "D".
My question
Why the event is listened only once? That is, why clicking D once will case the script to work but any further clicks will do nothing?
I will have to refresh the page for that to reuse the script and that's not the intended behavior.
Note: I would prefer a vanilla solution.