Skip to content

Instantly share code, notes, and snippets.

@alexey-sh
Created May 13, 2023 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexey-sh/772cde4226339d7194d3a6b13d79ece8 to your computer and use it in GitHub Desktop.
Save alexey-sh/772cde4226339d7194d3a6b13d79ece8 to your computer and use it in GitHub Desktop.
Кнопка для быстрого бана автора поста на пикабу
function getXCsrfToken() {
const data = JSON.parse(document.querySelector('[data-entry="initParams"]').innerText);
return data.csrfToken;
}
function banRequest(userId, xCsrfToken) {
const data = `authors=${userId}&communities=&tags=&keywords=&period=forever&action=add_rule`;
const url = 'https://pikabu.ru/ajax/ignore_actions.php';
return fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
'X-Csrf-Token': xCsrfToken,
'Referer': 'https://pikabu.ru/ignore-list?from=avatar-menu',
'X-Requested-With': 'XMLHttpRequest'
},
body: data,
}).catch(e => {
console.error('failed to ban user', e);
throw e;
}).then(r => r.json());
}
function ban(userId) {
const token = getXCsrfToken();
return banRequest(userId, token);
}
function createBanBtn(username, userId) {
const btn = document.createElement('button');
btn.innerText = 'Забанить автора';
btn.style.padding = '0px 20px';
btn.style.marginRight = '1rem';
btn.style.whiteSpace = 'nowrap';
const listener = () => {
ban(userId).then((data) => {
console.log(data);
if (data.result) {
btn.removeEventListener('click', listener);
btn.remove();
} else {
alert(data.message);
}
});
console.log('ban', username, userId);
}
btn.addEventListener('click', listener);
return btn;
}
setInterval(function () {
const users = document.querySelectorAll('.story__user-link.user__nick:not(.with_ban_btn)');
for (const user of users) {
const username = user.getAttribute('data-name');
const userId = user.getAttribute('data-id');
const btn = createBanBtn(username, userId);
user.parentNode.insertBefore(btn, user);
user.classList.add('with_ban_btn');
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment