Skip to content

Instantly share code, notes, and snippets.

@mahdavipanah
Last active May 13, 2019 17:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahdavipanah/385d479d6cdcf52455a203827b18ef72 to your computer and use it in GitHub Desktop.
Save mahdavipanah/385d479d6cdcf52455a203827b18ef72 to your computer and use it in GitHub Desktop.
Automatically cancel all friend requests that you've sent in Facebook with a simple script

This document is also available in English.

چطوری به شکل اتوماتیک در فیس‌بوک تمام درخواست‌های دوستی‌ای که فرستاده‌ایم رو کنسل کنیم؟

از اونجایی که فیس‌بوک امکانی برای کنسل کردن تمام درخواست‌های دوستی‌ای که فرستاده‌ایم به صورت یکجا نداره، مجبوریم تا تمام درخواست‌های دوستی رو دونه دونه کنسل کنیم. اسکریپت پیش رو مثل یه یوزر واقعی مرورگر عمل میکنه و به جای شما دونه دونه درخواست‌های دوستی‌ای که فرستاده‌اید رو کنسل میکنه و شما می‌تونید در همین حین از قهوتون لذت ببرید 😊

مراحل زیر رو دنبال کنید:

  • مرورگر گوگل کروم رو دانلود و اجرا کنید
  • وارد اکانت فیس‌بوک خود شود و بعد از لاگین به صفحه‌ی https://www.facebook.com/friends/requests/?fcref=none&outgoing=1 برید (در این صفحه می‌تونید لیست تمام درخواست‌های دوستی‌ای که فرستاده‌اید رو به صورت یکجا ببینید)
  • پنجره‌ی Developer Tools رو توی مرورگرتون باز کنید (یک راه اینه که در صفحه راست‌کلیک کنید و بعد Inspect رو بزنید)
  • تو صفحه‌ی باز شده وارد تب Console شوید
  • محتوای فایل script.js رو کپی و داخل کنسول پیست کنید و سپس Enter بزنید

توجه:

  • زمانی که اسکریپت داره اجرا میشه باید به اینترنت وصل باشید
  • این اسکریپت روی ورژن ۶۸ مرورگر گوگل کروم تست شده، ولی احتمالا روی ورژن‌های به‌روز از مرورگرهای معروف نیز بدون مشکل اجرا میشه
  • زمانی که اسکریپت در حال اجراس، اشاره‌گر موس باید یا داخل پنجره‌ی Developer Tools بمونه و یا خارج از پنجره‌ی کل مرورگر باشه تا تداخلی با کار اسکریپت ایجاد نکنه
  • برای متوقف کردن اسکریپت (قبل از اینکه تمام درخواست‌های دوستی رو کنسل کنه) خیلی راحت صفحه‌ی مرورگر رو ببندید
  • از اونجایی که احتمال داره ساختار صفحه‌ی فیس‌بوک عوض بشه و در نتیجه اسکریپت کار نکنه، اگه به مشکلی برخوردید بهم اطلاع بدید تا اسکریپت رو آپدیت کنم
  • پیغام هشداری در صفحه‌ی کنسول توسط فیس‌بوک نمایش داده‌میشود که داره میگه مراقب باشید تا هر اسکریپتی رو از هرکسی نگیرید و اجرا کنید. این هشدار باید جدی گرفته بشه ولی از اونجایی که اسکریپت پیش رو متن‌بازه، خود شما و یا هرکسی که اطلاعات فنی کافی داشته باشه میتونه اونو بخونه و از بی‌خطر بودنش مطمئن بشه

از اونجایی که سورس کد اسکریپت کامل کامنت‌شده هست، خوندن و مرورش خیلی راحته و به گرمی پذیرای contribution های شما هستیم.

این سند به زبان فارسی نیز موجود است.

How to automatically cancel all friend requests that you've sent in Facebook with a simple script?

Since Facebook does not allow you to cancel all friend requests you've sent, at once, you have to cancel every request one by one. This script acts like a real web browser user and cancels every request one by one, while you're enjoying your coffee :)

Follow these steps:

  • Download and start Google Chrome
  • Login to your Facebook account then go to https://www.facebook.com/friends/requests/?fcref=none&outgoing=1 (here you can see a list of all friend requests that you've sent
  • Open Developer Tools window (one way is to right click on the web page and clicking Inspect)
  • In the appeared window, go to Console tab
  • Copy script.js file's content and then paste it in the console and press Enter

NOTE:

  • You have to be connected to internet while the script is running
  • This script is tested in Google Chrome version 68 but is probably working in lastet version of all major browsers
  • While the script is running, your mouse pointer must be either inside the Developer Tools window or outside the browser's window to not interfere with the script
  • To stop the script before it's done (before canceling all requests), simply close the web page
  • Since Facebook's web page structure can change and consequently cause this script to not work properly. If you encountered any error, let me know.

The script's source code is well commented and is easy to read and review and your contributions are welcome.

/*
* Author : Hamidreza Mahdavipanah (mahdavipanah.com)
* Repository: https://gist.github.com/mahdavipanah/385d479d6cdcf52455a203827b18ef72/edit
*/
// this script has to be an async function in order to run smoothly and not causing the browser to hang
(async () => {
// this function is used for async sleeping
function sleep(after) {
// returns a promise that will resolve in 'after' millisecond
return new Promise(resolve => setTimeout(resolve, after));
}
// list of all "Friend Request Sent" butonns
// this list contains all buttons, even those who are not visible yet
const mainButtons = document.getElementsByClassName('_42ft _4jy0 FriendRequestOutgoing enableFriendListFlyout outgoingButton _4jy3 _517h _51sy');
// iterate over each button and try to cancel the requested message
for (let i = 0; i < mainButtons.length; i++) {
// if the button is hidden, which means we have canceled all visible buttons
if (mainButtons[i].classList.contains('hidden_elem')) {
// click "See More Requests Button"
document.getElementsByClassName('pam uiBoxLightblue _5cz uiMorePagerPrimary')[0].click();
// wait until more requests to load and be appeared on the page
do {
await sleep(400);
} while (mainButtons[i].classList.contains('hidden_elem'));
}
// click on the "Friend Request Sent" button for it's menu to open
mainButtons[i].click();
// wait until the menu opens
let cancelRequestButton = null;
do {
await sleep(400);
cancelRequestButton = document.getElementsByClassName('uiMenuItem FriendListCancel')[0];
} while (!cancelRequestButton);
// click the "Cancel Request" button on the menu
cancelRequestButton.click();
// wait for the confirmation modal to appear
let confirmButton = null;
do {
await sleep(400);
confirmButton = document.getElementsByClassName('_42ft _42fu layerConfirm uiOverlayButton selected _42g- _42gy')[0];
} while (!confirmButton);
// click on "Cancel Request" button on the "Confirmation Required" modal
confirmButton.click();
// wait until the server confirms the cancellation
// in another word: wait until the "Friend Request Sent" button gets hidden and "Add Friend" button shows up
do {
await sleep(400);
} while (!mainButtons[i].classList.contains('hidden_elem'));
}
console.log("Done!");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment