SHARE
TWEET

Untitled

a guest Jan 3rd, 2016 61 Never
  1. // ==UserScript==
  2. // @name         8ch browser
  3. // @namespace    d107074fbd9a45c8a8fb0f080fed13db
  4. // @description  Button in catalog that shows all the images in all posts.
  5. // @version      0.1
  6. // @match        http://8ch.net/*/catalog.html
  7. // @match        https://8ch.net/*/catalog.html
  8. // @grant        none
  9. // ==/UserScript==
  10. /*global window*/
  11.  
  12. (function () {
  13.     "use strict";
  14.  
  15.     var app;
  16.  
  17.     app = {};
  18.  
  19.     app.width = 220;
  20.     app.height = 220;
  21.     app.threadQuery = ".thread";
  22.     app.imageQuery = ".fileinfo + a";
  23.     app.threads = [];
  24.  
  25.     app.atPageEnd = function () {
  26.         var height;
  27.  
  28.         height = window.document.documentElement.scrollHeight;
  29.  
  30.         if (height < window.innerHeight) {
  31.             return true;
  32.         }
  33.  
  34.         return Math.ceil(window.pageYOffset + window.innerHeight) >= height;
  35.     };
  36.  
  37.     app.videoLabel = function () {
  38.         var canvas, context;
  39.  
  40.         canvas = window.document.createElement("canvas");
  41.         canvas.width = app.width;
  42.         canvas.height = app.height;
  43.         context = canvas.getContext("2d");
  44.         context.fillStyle = "hsla(0, 0%, 80%, 0.25)";
  45.         context.strokeStyle = "hsla(0, 0%, 100%, 0.5)";
  46.         context.lineWidth = 2;
  47.         context.moveTo(app.width / 3, app.height / 3);
  48.         context.lineTo(app.width / 3 * 2, app.height / 2);
  49.         context.lineTo(app.width / 3, app.height / 3 * 2);
  50.         context.closePath();
  51.         context.fill();
  52.         context.stroke();
  53.  
  54.         return canvas;
  55.     };
  56.  
  57.     app.videoLabel = app.videoLabel();
  58.  
  59.     app.style = function () {
  60.         var text, style;
  61.  
  62.         text = [
  63.             "img {",
  64.             "    margin: auto;",
  65.             "    display: block;",
  66.             "    max-height: " + app.height + "px;",
  67.             "    max-width: " + app.width + "px;",
  68.             "}",
  69.             "a.app {",
  70.             "    display: block;",
  71.             "    position: relative;",
  72.             "}",
  73.             "div.app {",
  74.             "    margin: 10px;",
  75.             "    padding: 10px;",
  76.             "    height: " + app.height + "px;",
  77.             "    width: " + app.width + "px;",
  78.             "    display: inline-block;",
  79.             "}",
  80.             "canvas {",
  81.             "    position: absolute;",
  82.             "    top: 0px;",
  83.             "}"
  84.         ];
  85.  
  86.         text = text.join("\n");
  87.  
  88.         style = window.document.createElement("style");
  89.         style.innerHTML = text;
  90.         window.document.head.appendChild(style);
  91.     };
  92.  
  93.     app.processImage = function (link, error) {
  94.         var img, a, div, canvas;
  95.  
  96.         if (!link.href) {
  97.             return;
  98.         }
  99.  
  100.         img = window.document.createElement("img");
  101.         img.src = link.querySelector("img").src;
  102.         img.addEventListener("error", error);
  103.         img.addEventListener("load", function () {
  104.             if (img.naturalWidth > img.naturalHeight) {
  105.                 img.width = app.width;
  106.             } else {
  107.                 img.height = app.height;
  108.             }
  109.         });
  110.  
  111.         a = window.document.createElement("a");
  112.         a.className = "app";
  113.         a.href = link.href;
  114.         a.target = "_blank";
  115.         a.appendChild(img);
  116.  
  117.         div = window.document.createElement("div");
  118.         div.className = "app reply post body-not-empty";
  119.         div.appendChild(a);
  120.  
  121.         if (link.href.indexOf("webm") !== -1) {
  122.             canvas = window.document.createElement("canvas");
  123.             canvas.height = app.height;
  124.             canvas.width = app.width;
  125.             canvas.style.position = "absolute";
  126.             canvas.style.top = "0px";
  127.             canvas.getContext("2d").drawImage(app.videoLabel, 0, 0);
  128.             a.appendChild(canvas);
  129.         }
  130.  
  131.         return div;
  132.     };
  133.  
  134.     app.loading = false;
  135.  
  136.     app.addThread = function () {
  137.         var thread, request, div, p, a, button, subject;
  138.  
  139.         if (app.loading) {
  140.             return;
  141.         }
  142.  
  143.         app.loading = true;
  144.  
  145.         thread = app.threads.pop();
  146.  
  147.         button = window.document.createElement("button");
  148.         button.innerHTML = "X";
  149.         button.addEventListener("click", function () {
  150.             window.document.body.removeChild(div);
  151.             if (app.atPageEnd()) {
  152.                 app.addThread();
  153.             }
  154.         });
  155.  
  156.         subject = thread.querySelector(".subject");
  157.         if (subject) {
  158.             subject = subject.innerHTML;
  159.         } else {
  160.             subject= "Thread";
  161.         }
  162.  
  163.         a = window.document.createElement("a");
  164.         a.href = thread.querySelector("a").href;
  165.         a.innerHTML = subject;
  166.  
  167.         p = window.document.createElement("p");
  168.         p.appendChild(button);
  169.         p.appendChild(window.document.createTextNode(" "));
  170.         p.appendChild(a);
  171.  
  172.         div = window.document.createElement("div");
  173.         div.appendChild(p);
  174.         window.document.body.appendChild(div)
  175.  
  176.         request = new window.XMLHttpRequest();
  177.         request.open("GET", a.href);
  178.         request.responseType = "document";
  179.         request.addEventListener("load", function () {
  180.             var images;
  181.  
  182.             app.loading = false;
  183.  
  184.             images = request.response.querySelectorAll(app.imageQuery);
  185.             Array.prototype.forEach.call(images, function (image) {
  186.                 image = app.processImage(image, function () {
  187.                     div.removeChild(image);
  188.                 });
  189.                 div.appendChild(image);
  190.             });
  191.  
  192.             if (app.atPageEnd()) {
  193.                 app.addThread();
  194.             }
  195.         });
  196.         request.send();
  197.     };
  198.  
  199.     app.run = function () {
  200.         app.style();
  201.  
  202.         window.addEventListener("scroll", function () {
  203.             if (app.atPageEnd()) {
  204.                 app.addThread();
  205.             }
  206.         });
  207.  
  208.         app.threads = window.document.querySelectorAll(app.threadQuery);
  209.         app.threads = Array.prototype.slice.call(app.threads);
  210.         window.document.body.innerHTML = "";
  211.  
  212.         app.addThread();
  213.     };
  214.  
  215.     app.button = function () {
  216.         var button, child;
  217.  
  218.         button = window.document.createElement("button");
  219.         button.innerHTML = "Browse images";
  220.         button.addEventListener("click", app.run);
  221.         child = window.document.body.firstChild;
  222.         window.document.body.insertBefore(button, child);
  223.     };
  224.  
  225.     app.button();
  226. }());
RAW Paste Data
Top