Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 4chan2reddit
- // @version 1
- // @grant none
- // @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 thresholdPreference = "-10" // TODO Load from cookie?
- function generateRandomColor() {
- let red = Math.floor(((Math.random() * 256) + 255) / 2)
- let green = Math.floor(((Math.random() * 256) + 255) / 2)
- let blue = Math.floor(((Math.random() * 256) + 255) / 2)
- return `rgb(${red},${green},${blue})`
- }
- function updateScores() {
- // User preferences will be stored.
- const thresholdMin0 = document.getElementById('thresholdMin0')
- const thresholdMin1 = document.getElementById('thresholdMin1')
- if (thresholdMin0.value !== thresholdPreference) {
- // TODO: Store to cookie?
- thresholdPreference = thresholdMin0.value
- thresholdMin1.value = thresholdPreference
- } else if (thresholdMin1.value !== thresholdPreference) {
- // TODO: Store to cookie?
- thresholdPreference = thresholdMin1.value
- thresholdMin0.value = thresholdPreference
- }
- // The script will read posts and apply rules when it sees "+1", "-1", and #hashtags.
- const posts = document.getElementsByClassName('postMessage')
- const upvoteFilter = /\+\d/;
- const downvoteFilter = /\-\d/;
- const tagSearchQuery = /[^0-9a-z]#([0-9a-z]+)/ig;
- // Posts are upvoted, downvoted, and tagged by replies, so tabulate votes by most recent.
- [...posts].forEach(p => { p.voteScore = 0; 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 upvoted = post.innerText.match(upvoteFilter)
- const downvoted = post.innerText.match(downvoteFilter)
- const tags = post.innerText.match(tagSearchQuery)
- const postQuotes = [...post.getElementsByClassName('quotelink')].map(q => q.hash.substring(2))
- const repliedTo = []
- if (postQuotes.length){
- // TODO: Better vote assignment rules
- // If someone replies to many posts, they all receive the same vote and tags.
- repliedTo.push(...([...posts].filter(p => postQuotes.includes(p.id.substring(1)))))
- } else {
- // Posters can self-tag and self-downvote if they do not contain a reply.
- repliedTo.push(post)
- }
- repliedTo.forEach(p => {
- if (upvoted && p !== post) p.voteScore++
- if (downvoted) p.voteScore--
- if (tags){
- // TODO: Count duplicate tags
- p.tags.push(...tags)
- }
- })
- // Posts outside the user's thresholds will be hidden.
- if (post.voteScore < parseInt(thresholdPreference)) {
- post.style.display = 'none'
- } else {
- post.style.display = 'block'
- // Individual posts will add a vote total and a tag cloud.
- const total = document.createElement('div')
- total.className = 'totals'
- total.innerHTML = post.voteScore
- if (post.voteScore >= 0)
- total.style = "background-color:#0f0"
- else
- total.style = "background-color:#f00"
- post.appendChild(total)
- if (post.tags)
- post.tags.forEach(t => {
- const tag = document.createElement('div')
- tag.innerHTML = t.toString()
- tag.className = 'tag'
- tag.style = "display:inline;background-color:" + generateRandomColor()
- post.appendChild(tag)
- })
- }
- }
- }
- const navLinkContainers = document.getElementsByClassName('navLinks')
- for (let i = 0; i < navLinkContainers.length; i++) {
- const mainContainer = document.createElement('div')
- // Add a refresh button.
- const updateScore = document.createElement('button')
- updateScore.innerHTML = 'Update Scores'
- updateScore.type = 'button'
- updateScore.addEventListener('click', updateScores)
- mainContainer.appendChild(updateScore)
- mainContainer.style = "display:inline"
- if (i < 2) {
- // Threshold Min textbox will be added to the thread.
- const thresholdMin = document.createElement('input')
- thresholdMin.id = 'thresholdMin' + i
- thresholdMin.type = 'number'
- thresholdMin.value = thresholdPreference
- const thresholdMinLabel = document.createElement('label')
- thresholdMinLabel.for = thresholdMin.id
- thresholdMinLabel.innerHTML = 'Minimum score'
- mainContainer.appendChild(thresholdMinLabel)
- mainContainer.appendChild(thresholdMin)
- }
- navLinkContainers[i].appendChild(mainContainer)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement