Ответ на пост «Ого»
// ==UserScript==
// @name Pikabu: авто-рандомный рейтинг при наведении
// @namespace pikabu-auto-random-vote
// @VERSION 1.1
// @Description Курсор задержался на посте/комментарии — рандомно ставится плюс или минус
// @match *://pikabu.ru/*
// @match *://*.pikabu.ru/*
// @run-at document-idle
// @GrAnt none
// ==/UserScript==
(function () {
'use strict';
const DELAY = 700; // мс задержки перед голосом (чтобы не срабатывать при пролёте мимо)
const voted = new WeakSet(); // уже отработанные контейнеры
let timer = null;
let current = null;
const findBtn = (root, dir) => {
const sel = dir === 'up'
? '.story__rating-up, .comment__rating-up, [class*="__rating-up"]'
: '.story__rating-down, .comment__rating-down, [class*="__rating-down"]';
const btn = root.querySelector(sel);
return btn ? (btn.closest('[class*="rating"]') || btn) : null;
};
const showToast = (text, ok) => {
const t = document.createElement('div');
t.textContent = text;
t.style.cssText = `
position:fixed; z-index:999999; top:20px; left:50%;
transform:translateX(-50%); padding:6px 14px; border-radius:8px;
background:${ok ? '#2bb24c' : '#999'}; color:#fff; font:bold 13px sans-serif;
box-shadow:0 2px 8px rgba(0,0,0,.35); pointer-events:none;
opacity:0; transition:opacity .15s;`;
document.body.appendChild(t);
requestAnimationFrame(() => (t.style.opacity = 1));
setTimeout(() => { t.style.opacity = 0; setTimeout(() => t.remove(), 200); }, 700);
};
const clearTimer = () => { clearTimeout(timer); timer = null; };
const vote = (root) => {
const dir = Math.random() < 0.5 ? 'up' : 'down';
const btn = findBtn(root, dir);
if (!btn) return;
btn.click();
showToast(dir === 'up' ? 'Авто: +1' : 'Авто: −1', true);
};
// Делегирование: работает и с подгружаемой лентой
document.addEventListener('mouseover', (e) => {
const root = e.target.closest('.story, .comment');
if (!root || root === current) return;
clearTimer();
current = root;
if (voted.has(root)) return; // этот пост/коммент уже отработан
timer = setTimeout(() => {
// убеждаемся, что курсор всё ещё на этом контейнере
if (root.matches(':hover')) {
voted.add(root);
vote(root);
}
}, DELAY);
});
document.addEventListener('mouseout', (e) => {
const root = e.target.closest('.story, .comment');
if (root && root === current && !root.matches(':hover')) {
clearTimer();
current = null;
}
});
})();