Advertisement
Guest User

Untitled

a guest
May 30th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name     4chan2reddit
  3. // @version  1
  4. // @grant    none
  5. // @description Destroy free speech and rely on the discernment of children
  6. // @include      *://boards.4chan.org/*
  7. // @include      *://boards.4channel.org/*
  8. // ==/UserScript==
  9.  
  10. // User preferences will be loaded.
  11. var thresholdPreference = "-10" // TODO Load from cookie?
  12.  
  13. function generateRandomColor() {
  14.     let red = Math.floor(((Math.random() * 256) + 255) / 2)
  15.     let green = Math.floor(((Math.random() * 256) + 255) / 2)
  16.     let blue = Math.floor(((Math.random() * 256) + 255) / 2)
  17.     return `rgb(${red},${green},${blue})`
  18. }
  19.  
  20. function updateScores() {
  21.     // User preferences will be stored.
  22.   const thresholdMin0 = document.getElementById('thresholdMin0')
  23.   const thresholdMin1 = document.getElementById('thresholdMin1')
  24.   if (thresholdMin0.value !== thresholdPreference) {
  25.     // TODO: Store to cookie?
  26.     thresholdPreference = thresholdMin0.value
  27.     thresholdMin1.value = thresholdPreference
  28.   } else if (thresholdMin1.value !== thresholdPreference) {
  29.     // TODO: Store to cookie?
  30.     thresholdPreference = thresholdMin1.value
  31.     thresholdMin0.value = thresholdPreference
  32.   }
  33.  
  34.   // The script will read posts and apply rules when it sees "+1", "-1", and #hashtags.
  35.   const posts = document.getElementsByClassName('postMessage')
  36.   const upvoteFilter = /\+\d/;
  37.   const downvoteFilter = /\-\d/;
  38.   const tagSearchQuery = /[^0-9a-z]#([0-9a-z]+)/ig;
  39.  
  40.   // Posts are upvoted, downvoted, and tagged by replies, so tabulate votes by most recent.
  41.   [...posts].forEach(p => { p.voteScore = 0; p.tags = []; })
  42.   const totals = document.getElementsByClassName('totals')
  43.   while (totals.length > 0) {
  44.     totals[0].parentNode.removeChild(totals[0])
  45.   }
  46.   const oldTags = document.getElementsByClassName('tag')
  47.   while (oldTags.length > 0) {
  48.     oldTags[0].parentNode.removeChild(oldTags[0])
  49.   }
  50.   for (let i = posts.length - 1; i >= 0; i--) {
  51.     const post = posts[i]
  52.     const upvoted = post.innerText.match(upvoteFilter)
  53.     const downvoted = post.innerText.match(downvoteFilter)
  54.     const tags = post.innerText.match(tagSearchQuery)
  55.     const postQuotes = [...post.getElementsByClassName('quotelink')].map(q => q.hash.substring(2))
  56.     const repliedTo = []
  57.     if (postQuotes.length){
  58.       // TODO: Better vote assignment rules
  59.       // If someone replies to many posts, they all receive the same vote and tags.
  60.       repliedTo.push(...([...posts].filter(p => postQuotes.includes(p.id.substring(1)))))
  61.     } else {
  62.       // Posters can self-tag and self-downvote if they do not contain a reply.
  63.       repliedTo.push(post)
  64.     }
  65.     repliedTo.forEach(p => {
  66.       if (upvoted && p !== post) p.voteScore++
  67.       if (downvoted) p.voteScore--
  68.       if (tags){
  69.         // TODO: Count duplicate tags
  70.         p.tags.push(...tags)
  71.       }
  72.     })
  73.     // Posts outside the user's thresholds will be hidden.
  74.     if (post.voteScore < parseInt(thresholdPreference)) {
  75.       post.style.display = 'none'
  76.     } else {
  77.       post.style.display = 'block'
  78.         // Individual posts will add a vote total and a tag cloud.
  79.       const total = document.createElement('div')
  80.       total.className = 'totals'
  81.       total.innerHTML = post.voteScore
  82.       if (post.voteScore >= 0)
  83.         total.style = "background-color:#0f0"
  84.       else
  85.         total.style = "background-color:#f00"
  86.       post.appendChild(total)
  87.       if (post.tags)
  88.         post.tags.forEach(t => {
  89.           const tag = document.createElement('div')
  90.           tag.innerHTML = t.toString()
  91.           tag.className = 'tag'
  92.           tag.style = "display:inline;background-color:" + generateRandomColor()
  93.           post.appendChild(tag)
  94.         })
  95.     }
  96.   }
  97. }
  98.  
  99.  
  100. const navLinkContainers = document.getElementsByClassName('navLinks')
  101. for (let i = 0; i < navLinkContainers.length; i++) {
  102.   const mainContainer = document.createElement('div')
  103.  
  104.   // Add a refresh button.
  105.   const updateScore = document.createElement('button')
  106.   updateScore.innerHTML = 'Update Scores'
  107.   updateScore.type = 'button'
  108.   updateScore.addEventListener('click', updateScores)
  109.   mainContainer.appendChild(updateScore)
  110.  
  111.   mainContainer.style = "display:inline"
  112.   if (i < 2) {
  113.     // Threshold Min textbox will be added to the thread.
  114.     const thresholdMin = document.createElement('input')
  115.     thresholdMin.id = 'thresholdMin' + i
  116.     thresholdMin.type = 'number'
  117.     thresholdMin.value = thresholdPreference
  118.     const thresholdMinLabel = document.createElement('label')
  119.     thresholdMinLabel.for = thresholdMin.id
  120.     thresholdMinLabel.innerHTML = 'Minimum score'
  121.     mainContainer.appendChild(thresholdMinLabel)
  122.     mainContainer.appendChild(thresholdMin)
  123.   }
  124.  
  125.     navLinkContainers[i].appendChild(mainContainer)
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement