Guest User

Untitled

a guest
Jul 18th, 2019
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        2draw picrandom
  3. // @namespace   *
  4. // @description Displays random picture from the room archive each time when the user clicks the button.
  5. // @include     http://2draw.me/archive/*
  6. // @include     https://2draw.me/archive/*
  7. // @match       http://2draw.me/archive/
  8. // @match       https://2draw.me/archive/
  9. // @version     1
  10. // @grant       none
  11. // ==/UserScript==
  12.  
  13. var style = document.createElement('style');
  14. style.type = 'text/css';
  15. style.innerHTML = "#randomPic {text-align:center; width:650px;color:#000;font-size:20px;}"
  16. +"#randomPic a { display:inline !important; border:none !important; color:#eee !important; background-color: #aaa !important; font-size:20px !important; line-height: 21px !important; vertical-align: text-top !important;}"
  17. +"#randomPic a:hover {display:inline !important; color:aqua !important;} "
  18. +"#randomPic a:hover::after {display:inline !important;} ";
  19. document.getElementsByTagName('head')[0].appendChild(style);
  20.  
  21. var completedThreads, locationParts = window.location.toString().split(/\//g);
  22. if (locationParts.length === 6) { // we are on the room archive page
  23.     var roomName = locationParts[4];
  24.     var span = document.getElementById("range");
  25.     var thumbs = document.getElementById("thumbs");
  26.     completedThreads = ~~(thumbs.children[0].attributes["data-index"].value);
  27.    
  28.     var getRandomPicture = function(){
  29.         var thread = Math.floor(Math.random() * completedThreads) + 1;
  30.         var threadUrl = locationParts[0]+"//2draw.me/archive/base/"+thread+".htm";
  31.         var reqListener = function() {
  32.             var p = this.responseText, startIndex = p.indexOf("<pre>"), endIndex = p.indexOf("</pre>"), posts = [], lines = p.substr(startIndex, endIndex-startIndex).split(/\n/g);
  33.            
  34.             for (var i=0; i < lines.length; i++){
  35.                 var line = lines[i].split(/\t/g);
  36.                 if (line.length >= 3){
  37.                     var timestamp = line[0].split(/,/)[0], author=line[1];
  38.                     if (line[2].indexOf("<a href") != -1 || line[2].indexOf("<img") != -1){
  39.                         var imageHtml = line[2].substr(0, line[2].indexOf(";"));
  40.                         var legend = "Author: "+author+" (" + new Date(~~timestamp*1000).toLocaleString() + ", thread <a href="+threadUrl+">"+thread+"</a>)";
  41.                         var newHtml = "<table id='randomPic'><tbody><tr><td>"+legend+"</td></tr><tr><td>"+imageHtml+"</td></tr></tbody></table>";
  42.                         posts.push(newHtml);
  43.                     }
  44.                 }
  45.             }
  46.             thumbs.innerHTML = posts[Math.floor(Math.random() * posts.length)];
  47.         }
  48.  
  49.         var req = new XMLHttpRequest();
  50.         req.addEventListener("load", reqListener);
  51.         req.open("GET", threadUrl);
  52.         req.send();
  53.     }
  54.  
  55.     if (span) {
  56.         span.insertAdjacentHTML("afterend", "&nbsp;<input type='button' id='rndPicture' value='Random picture'>");
  57.         document.getElementById("rndPicture").addEventListener("click", getRandomPicture);
  58.     }
  59. }
RAW Paste Data