Skip to content

Instantly share code, notes, and snippets.

@xmannv
Last active February 27, 2020 11:03
Show Gist options
  • Select an option

  • Save xmannv/4102c8b78d4bc2733a1da9f5c0532889 to your computer and use it in GitHub Desktop.

Select an option

Save xmannv/4102c8b78d4bc2733a1da9f5c0532889 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name FB: Unfollow
// @version 0.1
// @description Quick Unfollow User from Facebook NewsFeed
// @author Xman (https://codetay.com)
// @match https://www.facebook.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.onscroll = (event) => {
// Thêm nút vào sau tên profile
const selectors = document.querySelectorAll('h5._7tae._14f3._14f5._5pbw._5vra');
let i = 0;
for (i = 0; i < selectors.length; i++) {
// Lấy User ID
//const dataHoverCard = selectors[i].querySelector('a').getAttribute('data-hovercard');
const dataHoverCards = selectors[i].querySelectorAll('a[data-hovercard]');
let unfollow = false;
if (dataHoverCards.length === 1) {
unfollow = true;
} else {
const profileLinks = selectors[i].querySelectorAll('a[data-hovercard].profileLink');
if (profileLinks.length > 1) {
unfollow = true;
}
}
if (unfollow) {
const matches = dataHoverCards[0].getAttribute('data-hovercard').match(/\d+/);
if (!selectors[i].querySelector('a.xUnFollow')) {
selectors[i].querySelector('span').insertAdjacentHTML('afterend', `<a class="xUnFollow" data-userID="${matches[0]}" href="javascript:void(${matches[0]});" style="font-weight:bold; padding: 10px;color:red">Unfollow</a>`);
}
}
}
}
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
document.addEventListener('click', function(event) {
// If the clicked element doesn't have the right selector, bail
if (!event.target.matches('.xUnFollow')) return;
// Don't follow the link
event.preventDefault();
event.stopPropagation()
// ID người dùng
const userID = getCookie('c_user');
// ID người bị unfollow
const profileID = event.target.getAttribute('data-userID');
// Lấy token
const fbDtsg = document.querySelector('input[name=fb_dtsg]').value
event.target.innerHTML = 'working...'
fetch("https://www.facebook.com/ajax/follow/unfollow_profile.php", {
"credentials": "include",
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,vi;q=0.8,lb;q=0.7",
"content-type": "application/x-www-form-urlencoded",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
},
"referrer": "https://www.facebook.com/",
"referrerPolicy": "origin-when-cross-origin",
"body": `profile_id=${profileID}&location=5&__user=${userID}&__a=1&fb_dtsg=${fbDtsg}`,
"method": "POST",
"mode": "cors"
})
.then(response => {
const el = document.querySelector(`a[data-userid="${profileID}"]`);
el.closest('[data-testid="fbfeed_story"]').remove();
//console.log('done')
});
return;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment