Skip to content

Instantly share code, notes, and snippets.

@rafcontreras
Last active June 13, 2023 06:02
Show Gist options
  • Save rafcontreras/99ce0580c6e45dee54b3427ab3dff5f0 to your computer and use it in GitHub Desktop.
Save rafcontreras/99ce0580c6e45dee54b3427ab3dff5f0 to your computer and use it in GitHub Desktop.
Riddle Voting

Instructions

  1. Install Tampermonkey extension for your browser
  2. Copy the code from the Riddle.js file
  3. Go to the Tampermonkey dashboard and click on the [+] button to create a new UserScript
  4. Paste the code
  5. Make sure the script is not Enabled
  6. Go to the Stuff page where the poll is running
  7. Get the option you want to select
  8. Change the desiredOption variable
// Before
const desiredOption = "Yeah";
// After
const desiredOption = "Nah";
  1. Enable the Userscript
  2. Reload the Stuff page

Getting more votes

It takes around 7 seconds between votes, if you want to speed up voting just open more browser tabs with the same page.

// ==UserScript==
// @name Riddle
// @namespace http://contrer.as/
// @version 0.3
// @description try to take over the world!
// @author Rafael Contreras
// @match https://www.riddle.com/*
// @grant none
// ==/UserScript==
const desiredOption = "Nah";
const closedString = "Votings is closed";
const waitTime = 1000;
const errorClassName = "error-page";
const footerClassName = "w-card__footer";
const widgetId = "choices";
const getRandomInt = (min, max) => {
const newMin = Math.ceil(min);
const newMax = Math.floor(max);
return Math.floor(Math.random() * (newMax - newMin + 1)) + newMin;
};
const clickOn = (x, y) => {
const ev = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
screenX: x,
screenY: y
});
var el = document.elementFromPoint(x, y);
try {
el.dispatchEvent(ev);
} catch (e) {
location.reload();
}
};
const addElement = dimensions => {
// create a new div element
const newDiv = document.createElement("div");
const textDiv = document.createElement("div");
const top = dimensions.top + getRandomInt(1, 40);
const left = dimensions.left + getRandomInt(1, 640);
const totals = Object.entries(localStorage)
.map(([key, value]) => ({ key, value }))
.map(({ key, value }) => {
if (key.startsWith(`tacoVoteCounter`)) {
return Number(value) + 1;
}
return null
})
.filter(item => item !== null && item !== 0)
.join("");
newDiv.style.position = "absolute";
newDiv.style.top = `${top}px`;
newDiv.style.left = `${left}px`;
newDiv.style.width = `10px`;
newDiv.style.height = `10px`;
newDiv.style.backgroundColor = "#ffff0088";
newDiv.style.border = "1px solid #ffff00";
newDiv.style.borderRadius = "5px";
newDiv.style.zIndex = "2000";
newDiv.style.pointerEvents = "none";
textDiv.style.position = "absolute";
textDiv.style.top = `${top + 4}px`;
textDiv.style.left = `${left + 4}px`;
textDiv.style.padding = `1px 2px`;
textDiv.style.backgroundColor = "#00000088";
textDiv.style.color = "#fff";
textDiv.style.border = "1px solid transparent";
textDiv.style.borderRadius = "5px";
textDiv.style.zIndex = "2000";
textDiv.style.pointerEvents = "none";
textDiv.innerHTML = `Clicked here<br/>${totals} times`;
document.body.appendChild(newDiv);
document.body.appendChild(textDiv);
clickOn(left, top);
};
const docReady = fn => {
// see if DOM is already available
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
};
const waitForElm = selector =>
new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
const sleep = milliseconds => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
const voteCounter = add => {
const poll = window.location.pathname
?.split("/")
?.filter(path => path !== "" && path !== null && path !== undefined)
?.at(-1);
let votes = 0;
const currentValue = localStorage.getItem(`tacoVoteCounter${poll}`);
if (currentValue) {
votes = parseInt(currentValue, 10);
} else {
localStorage.setItem(`tacoVoteCounter${poll}`, votes.toString());
}
Object.entries(localStorage)
.map(([key, value]) => ({ key, value }))
.map(({ key }) => {
if (key.startsWith(`tacoVoteCounter${poll}`)) {
if (add) {
votes += 1;
}
return localStorage.setItem(`tacoVoteCounter${poll}`, votes.toString());
}
if (key.startsWith("tacoV")) {
return null;
}
return localStorage.removeItem(key);
});
return votes;
};
const clearStorage = async (vote) => {
await sleep(100);
document.cookie.split(";").forEach(cookies => {
document.cookie = cookies
.replace(/^ +/, "")
.replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");
});
sessionStorage.clear();
voteCounter(vote);
await sleep(100);
return true;
};
const checkForErrorPage = () =>
document.getElementsByClassName(errorClassName).length > 0;
const rigThisPoll = async () => {
await clearStorage(false);
const elements = Array.from(document.getElementsByClassName(widgetId) ?? []);
if (elements?.length === 1) {
const childList = Array.from(elements?.[0]?.children ?? []);
const desiredElement = childList?.filter(item =>
item?.innerHTML?.includes(desiredOption)
);
if (desiredElement?.length === 1) {
console.log(`Clicking option ${desiredOption}`);
await sleep(waitTime);
const dimensions = desiredElement?.[0]?.getBoundingClientRect();
addElement(dimensions);
await sleep(500);
addElement(dimensions);
await sleep((waitTime * getRandomInt(4, 9)) + (getRandomInt(1, 9) * 0.1));
await clearStorage(true);
window.parent.postMessage({ "reload": true }, "*");
await sleep(100);
location.reload();
}
} else {
console.log("Too many options found");
}
};
window.addEventListener('message', (event) => {
if (event.data?.reload) {
location.reload();
}
});
docReady(async () => {
await sleep(waitTime);
setInterval(function () {
clearStorage(false);
}, 200);
const exists = await waitForElm(`.${widgetId}`);
if (exists) {
try {
rigThisPoll();
} catch (e) {
location.reload();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment