Skip to content

Instantly share code, notes, and snippets.

@sayhicoelho
Last active March 13, 2020 17:47
Show Gist options
  • Save sayhicoelho/8d9425f8764c257d4fdedfd0c378fec8 to your computer and use it in GitHub Desktop.
Save sayhicoelho/8d9425f8764c257d4fdedfd0c378fec8 to your computer and use it in GitHub Desktop.
Block users in their comments by pressing right mouse button.
// ==UserScript==
// @name Facebook Instant Block User
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description try to take over the world!
// @author You
// @match https://www.facebook.com*
// @grant none
// ==/UserScript==
(function () {
setInterval(addBehavior, 5000)
document.body.addEventListener('click', hideContextMenus)
function addBehavior() {
const comments = document.querySelectorAll('._72vr:not(._blockinit)')
comments.forEach(comment => addContextMenu(comment))
}
function addContextMenu(comment) {
comment.addEventListener('contextmenu', function (e) {
e.preventDefault()
showContextMenu(e.pageX, e.pageY, comment)
})
comment.classList.add('_blockinit')
}
function showContextMenu(x, y, comment) {
hideContextMenus()
const ul = document.createElement('ul')
const items = []
items.push({
label: 'Instant block user',
onclick: e => handleBlockUser(comment)
})
items.push({
label: 'Cancel',
onclick: hideContextMenus
})
items.forEach(item => {
const li = document.createElement('li')
li.textContent = item.label
li.onclick = item.onclick
li.style.padding = '20px'
li.style.cursor = 'pointer'
li.style.fontSize = '14px'
li.addEventListener('mouseenter', e => {
li.style.backgroundColor = '#f2f2f2'
})
li.addEventListener('mouseleave', e => {
li.style.backgroundColor = 'white'
})
ul.appendChild(li)
})
ul.style.backgroundColor = 'white'
ul.style.color = 'black'
ul.style.listStyle = 'none'
ul.style.boxShadow = '0 3px 6px 1px rgba(0, 0, 0, .5)'
ul.style.position = 'absolute'
ul.style.left = x + 'px'
ul.style.top = y + 'px'
ul.style.zIndex = '1'
ul.className = '_blockuser_dropdown'
ul.onclick = e => e.stopPropagation()
document.body.appendChild(ul)
}
function hideContextMenus() {
document.querySelectorAll('._blockuser_dropdown')
.forEach(contextmenu => contextmenu.remove())
}
function handleBlockUser(comment) {
if (window.confirm('Are you sure you want to block this user?')) {
const hovercard = comment.querySelector('a').dataset.hovercard
const profile_id = hovercard.split('?id=')[1]
const fb_dtsg = document.querySelector('input[name="fb_dtsg"]').value
const fd = new FormData()
fd.append('uid', profile_id)
fd.append('confirmed', '1')
fd.append('fb_dtsg', fb_dtsg)
fetch('https://www.facebook.com/ajax/privacy/block_user.php', {
method: 'POST',
body: fd
})
hideContextMenus()
comment.closest('li').remove()
}
}
addBehavior()
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment