Advertisement
Guest User

BritPolNews.user.js

a guest
Dec 9th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        BritPolNews
  3. // @namespace   britpolnews
  4. // @description Automatically includes recent relevant news in the OP of Brit/pol/.
  5. // @include     https://boards.4chan.org/pol/thread/*
  6. // @version     1
  7. // @grant       GM_xmlhttpRequest
  8. //
  9. // Save in file with extension ".user.js" then drag the file in to your open browser to install.
  10. //
  11. // ==/UserScript==
  12.  
  13. // Ensure it only runs once.
  14. if (window.top != window.self)
  15.   return;
  16.  
  17. var visible = false;
  18. var bits;
  19.  
  20. GM_xmlhttpRequest({
  21.   method: "GET",
  22.   url: "https://farage.neocities.org/",
  23.   onload: function(response) {
  24.     var parser, doc, headlines, links, time, oppost, showhidediv, showhidebutton, spacer;
  25.    
  26.     // Setup
  27.     parser = new DOMParser();
  28.     doc = parser.parseFromString(response.responseText, "text/html");
  29.     headlines = doc.getElementsByClassName("quote");
  30.     links = doc.getElementsByTagName("a");
  31.     oppost = document.getElementsByClassName("postMessage")[0];
  32.    
  33.     // Create button
  34.     showhidediv = document.createElement("div");
  35.     showhidediv.style.textAlign = "center";
  36.     showhidediv.style.marginTop = "10px";
  37.     showhidediv.innerHTML = "[<a id='showhide' style='cursor: pointer;'>Show Live Headlines</a>]";
  38.     oppost.appendChild(showhidediv);
  39.     showhidebutton = document.getElementById("showhide");
  40.  
  41.     // Add event listener
  42.     showhidebutton.addEventListener("click", function() {
  43.       visible = !visible;
  44.       if (visible) {
  45.         this.innerHTML = "Hide Live Headlines";
  46.         for (var i = 0; i < bits.length; i++)
  47.           bits[i].style.display = "inline";
  48.       } else {
  49.         this.innerHTML = "Show Live Headlines";
  50.         for (var i = 0; i < bits.length; i++)
  51.           bits[i].style.display = "none";
  52.       }
  53.     });
  54.    
  55.     // Add top spacer
  56.     spacer = document.createElement("hr");
  57.     oppost.appendChild(spacer);
  58.    
  59.     // Create timestamp bit
  60.     time = document.createElement("span");
  61.     time.innerHTML = doc.getElementsByTagName("small")[0].innerHTML;
  62.     time.innerHTML = "Headlines " + time.innerHTML.substring(4, time.innerHTML.length);
  63.     time.style.fontStyle = "italic";
  64.     time.className += " showhidebit";
  65.    
  66.     // Append bits
  67.     for (var i = 0; i < headlines.length; i++) {
  68.       headlines[i].className += " showhidebit";
  69.       links[i].className += " showhidebit";
  70.       oppost.appendChild(headlines[i]).appendChild(document.createElement("br")).className += " showhidebit";
  71.       oppost.appendChild(links[i]).appendChild(document.createElement("br")).className += " showhidebit";
  72.       oppost.appendChild(document.createElement("br")).className += " showhidebit";
  73.       oppost.appendChild(time);
  74.     }
  75.    
  76.     // Get all elements with class "showhidebit". Kind of a work-around because I'm shit and JS is for gayers.
  77.     bits = document.getElementsByClassName("showhidebit");
  78.    
  79.     // Hide all bits by default
  80.     for (var i = 0; i < bits.length; i++)
  81.       bits[i].style.display = "none";
  82.   }
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement