Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 4chan2reddit
- // @namespace fourchantoreddit
- // @grant GM.getValue
- // @grant GM.setValue
- // @version 1
- // @description Destroy free speech and rely on the discernment of children
- // @include *://boards.4chan.org/*
- // @include *://boards.4channel.org/*
- // ==/UserScript==
- // User preferences will be loaded.
- var thresholdMinPreference = "-4";
- var thresholdMaxPreference = "4";
- function generateRandomColor() {
- return `rgb(${Math.floor((Math.random() * 256 + 255) / 2)},${Math.floor((Math.random() * 256 + 255) / 2)},${Math.floor((Math.random() * 256 + 255) / 2)})`;
- }
- // The script will read posts and apply rules when it sees "+1", "-1", and #hashtags.
- const upvoteFilter = /\s\+\d+\s|up(vot|doot|boat)|t ?h ?a ?n ?k (y ?o ?)?u|bump|sauce|\bbased|good ?job|\b(nice|lol|lmao|kek|.pbp\b)/i;
- const downvoteFilter = /\s\-\d+\s|down(vot|doot|boat)|\bkys\b|\bcringe\b|\bcope\b|f[a]g|gay|reddit|kil+ yourself|f.ck|r.tard|nig(s|ger)|j[e]w|\bb(8|ait)\b|schizo|stfu|rent.?free|cancer|\bloser/i;
- const tagSearchQuery = /[^0-9a-z]#([0-9a-z]{2,})/gi;
- function voteAndTag(reply, replyText, post) {
- if (!post) return;
- replyText = ` ${replyText} `;
- const upvoted = replyText.match(upvoteFilter);
- const downvoted = replyText.match(downvoteFilter);
- const tags = replyText.match(tagSearchQuery);
- if (upvoted && post !== reply) post.voteScore++;
- else if (downvoted) post.voteScore--;
- for (let i = 0; i < tags?.length; i++) {
- const tag = post.tags[tags[i].toString()] || {
- count: 0,
- color: generateRandomColor(),
- };
- tag.count++;
- post.tags[tags[i].toString()] = tag;
- }
- }
- function updateScores() {
- // User preferences will be stored.
- const thresholdMin0 = document.getElementById("thresholdMin0");
- const thresholdMin1 = document.getElementById("thresholdMin1");
- if (thresholdMin0 && thresholdMin0.value !== thresholdMinPreference) {
- thresholdMinPreference = thresholdMin0.value || "-4";
- thresholdMin0.value = thresholdMinPreference;
- if (thresholdMin1) thresholdMin1.value = thresholdMinPreference;
- } else if (thresholdMin1 && thresholdMin1.value !== thresholdMinPreference) {
- thresholdMinPreference = thresholdMin1.value || "-4";
- if (thresholdMin0) thresholdMin0.value = thresholdMinPreference;
- thresholdMin1.value = thresholdMinPreference;
- }
- GM.setValue("minThreshold", thresholdMinPreference);
- const thresholdMax0 = document.getElementById("thresholdMax0");
- const thresholdMax1 = document.getElementById("thresholdMax1");
- if (thresholdMax0 && thresholdMax0.value !== thresholdMaxPreference) {
- thresholdMaxPreference = thresholdMax0.value || "4";
- thresholdMax0.value = thresholdMaxPreference;
- if (thresholdMax1) thresholdMax1.value = thresholdMaxPreference;
- } else if (thresholdMax1 && thresholdMax1.value !== thresholdMaxPreference) {
- thresholdMaxPreference = thresholdMax1.value || "4";
- thresholdMax1.value = thresholdMaxPreference;
- if (thresholdMax0) thresholdMax0.value = thresholdMaxPreference;
- }
- GM.setValue("maxThreshold", thresholdMaxPreference);
- // Posts are upvoted, downvoted, and tagged by replies, so tabulate votes by most recent.
- const posts = document.getElementsByClassName("postMessage");
- [...posts].forEach((p) => {
- p.voteScore = 0;
- if (p.tags) Object.keys(p.tags).forEach((k) => (p.tags[k].count = 0));
- else p.tags = {};
- });
- const totals = document.getElementsByClassName("totals");
- while (totals.length > 0) {
- totals[0].parentNode.removeChild(totals[0]);
- }
- const oldTags = document.getElementsByClassName("tag");
- while (oldTags.length > 0) {
- oldTags[0].parentNode.removeChild(oldTags[0]);
- }
- for (let i = posts.length - 1; i >= 0; i--) {
- const post = posts[i];
- const postQuotes = post.getElementsByClassName("quotelink");
- const op = post.parentElement.parentElement.parentElement.getElementsByClassName("postMessage")[0]
- if (postQuotes.length) {
- // Parse replies to many posts into votes and tags.
- let replyTo = [];
- let data = "";
- for (let j = 0; j < post.childNodes.length; j++) {
- const node = post.childNodes[j];
- if ([...postQuotes].includes(node)) {
- const repliedTo = [...posts].find(
- (p) => p.id.substring(1) === node.hash.substring(2)
- );
- if (!replyTo.includes(repliedTo)) {
- if (data) {
- if (replyTo.length)
- replyTo.forEach((r) => voteAndTag(post, data, r));
- else voteAndTag(post, data, op);
- replyTo = [];
- data = "";
- }
- replyTo.push(repliedTo);
- }
- } else if (node.data) data += node.data + " ";
- }
- if (replyTo.length) replyTo.forEach((r) => voteAndTag(post, data, r));
- else voteAndTag(post, data, op);
- } else {
- // Posters can self-tag and self-downvote.
- voteAndTag(post, post.innerText, op);
- }
- // Posts outside the user's thresholds will be hidden.
- if (
- post.voteScore < parseInt(thresholdMinPreference) ||
- post.voteScore > parseInt(thresholdMaxPreference)
- ) {
- post.style.display = "none";
- } else {
- post.style.display = "block";
- const postQuote = ">>" + post.id.substring(1) + "\n";
- const upvote = document.createElement("a");
- upvote.addEventListener("click", () => {
- [...document.getElementsByTagName("textArea")].forEach((t) => {
- t.value += (t.value.includes(postQuote) ? " " : postQuote) + "+1";
- });
- });
- upvote.style = "display:inline;color:#0f0;font-weight:bolder;background-color:#888";
- upvote.innerHTML = " ⬆ ";
- const downvote = document.createElement("a");
- downvote.innerHTML = " ⬇ ";
- downvote.addEventListener("click", () => {
- [...document.getElementsByTagName("textArea")].forEach((t) => {
- t.value += (t.value.includes(postQuote) ? " " : postQuote) + "-1";
- });
- });
- downvote.style = "display:inline;color:#f00;font-weight:bolder;background-color:#888";
- // Individual posts will add a vote total and a tag cloud.
- const total = document.createElement("div");
- total.innerHTML = " " + post.voteScore;
- total.style = "display:inline;color:#222";
- const votes = document.createElement("div");
- votes.innerHTML = " ";
- votes.style =
- "width:80%" +
- (post.voteScore > 0
- ? ";background-color:#8f8"
- : post.voteScore < 0 ? ";background-color:#f88" : "");
- votes.className = "totals";
- votes.appendChild(upvote);
- votes.appendChild(downvote);
- votes.appendChild(total);
- post.appendChild(votes);
- if (post.tags)
- Object.keys(post.tags).forEach((k) => {
- const t = post.tags[k];
- const tag = document.createElement("div");
- tag.innerHTML = k + (t.count > 1 ? " x" + t.count : "");
- tag.className = "tag";
- tag.style = "display:inline;color:#222;background-color:" + t.color;
- post.appendChild(tag);
- });
- }
- }
- }
- GM.getValue("minThreshold", "-4").then(async (v) => {
- thresholdMinPreference = v || "-4";
- thresholdMaxPreference = (await GM.getValue("maxThreshold", "4")) || "4";
- setTimeout(() => {
- const navLinkContainers = document.getElementsByClassName("navLinks");
- for (let i = 0; i < navLinkContainers.length; i++) {
- const mainContainer = document.createElement("div");
- mainContainer.style = "display:inline";
- //// Add a refresh button.
- //const updateScore = document.createElement("button");
- //updateScore.id = "updateScore" + i;
- //updateScore.type = "button";
- //updateScore.innerHTML = "Update scores";
- //updateScore.onclick = updateScores;
- //mainContainer.innerHTML += " ";
- //mainContainer.appendChild(updateScore);
- if (i < 2) {
- // Threshold Min/Max textboxes will be added to the thread.
- const thresholdMin = document.createElement("input");
- thresholdMin.id = "thresholdMin" + i;
- thresholdMin.type = "number";
- thresholdMin.value = thresholdMinPreference;
- const thresholdMinLabel = document.createElement("label");
- thresholdMinLabel.for = thresholdMin.id;
- thresholdMinLabel.innerHTML = "Minimum score";
- mainContainer.innerHTML += " ";
- mainContainer.appendChild(thresholdMinLabel);
- mainContainer.innerHTML += " ";
- mainContainer.appendChild(thresholdMin);
- const thresholdMax = document.createElement("input");
- thresholdMax.id = "thresholdMax" + i;
- thresholdMax.type = "number";
- thresholdMax.value = thresholdMaxPreference;
- const thresholdMaxLabel = document.createElement("label");
- thresholdMaxLabel.for = thresholdMax.id;
- thresholdMaxLabel.innerHTML = "Max";
- mainContainer.innerHTML += " ";
- mainContainer.appendChild(thresholdMaxLabel);
- mainContainer.innerHTML += " ";
- mainContainer.appendChild(thresholdMax);
- }
- navLinkContainers[i].appendChild(mainContainer);
- }
- updateScores();
- setInterval(updateScores, 9000)
- }, 4000);
- });
Add Comment
Please, Sign In to add comment