Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @id             rthirtyfimgpgadj
  3. // @name           Rule34.xxx: Image Page Adjustments
  4. // @version        1.2
  5. // @include        *rule34.xxx/*
  6. // @domain         rule34.xxx
  7. // @run-at         document-start
  8. // @grant          none
  9. // ==/UserScript==
  10.  
  11. /* -------------------------------------------------------------------------- */
  12.  
  13. `use strict`;
  14.  
  15. let move_childnodes = function(Src, Dst) {
  16.     while (Src.hasChildNodes()) {Dst.appendChild(Src.firstChild);};
  17. };
  18.  
  19. let insert_style_rules = function(Rules) {
  20.     let Style = document.createElement(`style`);
  21.     document.head.appendChild(Style);
  22.     for (let Rule of Rules) {
  23.         Style.sheet.insertRule(Rule, Style.sheet.cssRules.length);
  24.     };
  25. };
  26.  
  27. let GlobalStyleRules = [
  28.     `* {color : #c0c0c0 !important;}`,
  29.     `a:link, a:visited {color : #b0e0b0 !important;}`,
  30.     `.tag-type-copyright > a {color : #f0a0f0 !important;}`,
  31.     `.tag-type-artist > a {color : #f0a0a0 !important;}`,
  32.     `.tag-type-character > a {color : #f0f0a0 !important;}`,
  33.  
  34.     "body {"+
  35.         "background-image : none !important;"+
  36.         "background-color : #303a30 !important;"+
  37.     "}",
  38.  
  39.     "#header * {color : #88a088 !important;}",
  40.     "#header, #header > * {"+
  41.         "background-image : none !important;"+
  42.         "background-color : #303030 !important;"+
  43.     "}",
  44.     "#header #subnavbar * {color : #878787 !important;}",
  45.  
  46.     "li.current-page,"+
  47.     "tr.tableheader, tr[style=\"background-image: url(topban00.jpg);\"],"+
  48.     "table.highlightable > tbody > tr[style=\"background: #006ffa;\"] {"+
  49.         "background-image : none !important;"+
  50.         "background-color : #505a50 !important;"+
  51.     "}",
  52.  
  53.     "table tr.pending-tag {"+
  54.         "background-color : #445644 !important;"+
  55.     "}",
  56.  
  57.     "#header #site-title {display : none !important;}",
  58.  
  59.     "div.quote {background-color : #505a50 !important;}",
  60.  
  61.     "div.has-mail {background-color : #303030 !important;}",
  62.  
  63.     `.status-notice,
  64.     #post-view > div [ /* child posts banner */
  65.         style="background: #f0f0f0; padding: 10px; `+
  66.         `text-align: center; border: 3px solid #dadada;"
  67.     ] {
  68.         background-color : #303030 !important;
  69.         border-width : 1px !important;
  70.         border-color : #505a50 !important;
  71.         border-style : solid !important;
  72.     }`,
  73.  
  74.     `.status-notice {margin : 0px 0px 1em 0px !important;}`,
  75.  
  76.     `table.highlightable > tbody >
  77.     tr:not([style="background: #006ffa;"]):not(.tableheader):hover {
  78.         background-color : initial !important;
  79.     }`,
  80.  
  81.     `img[src*="icame.png"][alt="I came!"] {
  82.         display : none !important;
  83.     }`,
  84.  
  85.     "input[type=text], textarea, select, select {"+
  86.         "background-color : #303030 !important;"+
  87.         "border-width : 1px !important;"+
  88.         "border-color : #505a50 !important;"+
  89.         "border-style : solid !important;"+
  90.     "}",
  91.     `input[type=submit], input[type=button], button {
  92.         padding : 0 0.3em !important;
  93.         color : #303a30 !important;
  94.         font-weight : bold !important;
  95.         background-color : #88a088 !important;
  96.         border-width : 1px !important;
  97.         border-style : solid !important;
  98.         border-color : #505a50 !important;
  99.         text-align : center;
  100.     }`,
  101.     `input[type=submit]:active, input[type=button]:active, button:active {
  102.         filter : invert(100%) hue-rotate(180deg);
  103.     }`,
  104.     "input[value=Search] {display : none !important;}",
  105.  
  106.     `iframe {display : none !important;}`,
  107.     `#blacklisted-sidebar {display : none !important;}`,
  108.  
  109.     `#tag-sidebar img {padding : 10px 0px;}`,
  110.     `#taglinks-container {margin-top : 10px;}`,
  111.     `img[src="//rule34.xxx/images/r34chibi.png"] {
  112.         opacity : 0.5;
  113.     }`
  114. ];
  115.  
  116. let adjust_image_page = function() {
  117.     insert_style_rules([
  118.         "#header li {display : block;}",
  119.         "#content {"+
  120.             "padding : 0px !important;"+
  121.             "text-align : center;"+
  122.         "}",
  123.  
  124.         `.fit-viewport {
  125.             max-width : 100vw;
  126.             max-height : 100vh;
  127.         }`,
  128.  
  129.         ".centre-box {"+
  130.             "display : inline-block;"+
  131.             "position : relative;"+
  132.             "text-align : initial;"+
  133.         "}",
  134.  
  135.         ".side-box {position : absolute; top : 0px;}",
  136.         ".side-box li {list-style-type : none;}",
  137.  
  138.         ".left-box {width : 135px; left : -155px;}",
  139.  
  140.         ".right-box {"+
  141.             "width : 250px;"+
  142.             "right : -300px;"+
  143.             "padding : 7px 10px 7px 20px;"+
  144.             "background-color : rgba(0, 0, 0, 0.15);"+
  145.         "}",
  146.  
  147.         "#note-container {position : absolute;}",
  148.         "#note-container > .note-body {"+
  149.             "position : absolute;"+
  150.             "padding : 5px;"+
  151.             "border : 1px solid black;"+
  152.             "max-width : 300px;"+
  153.             "min-height : 10px;"+
  154.             "min-width : 140px;"+
  155.             "overflow : auto;"+
  156.             "cursor : pointer;"+
  157.             "background-color : rgba(0, 0, 0, 0.7);"+
  158.         "}",
  159.         "#note-container > .note-body > p.tn {color: gray; font-size: 0.8em;}",
  160.         "#note-container > .note-box {"+
  161.             "position : absolute;"+
  162.             "height : 150px;"+
  163.             "width : 150px;"+
  164.             "background-color : #FFF;"+
  165.             "border : 1px solid black;"+
  166.             "cursor : move;"+
  167.         "}",
  168.         "#note-container > .note-box > .note-corner {"+
  169.             "position : absolute;"+
  170.             "height : 7px;"+
  171.             "width : 7px;"+
  172.             "right : 0;"+
  173.             "bottom : 0;"+
  174.             "background-color : black;"+
  175.             "cursor : se-resize;"+
  176.         "}",
  177.         "#note-container > .unsaved {"+
  178.             "border : 1px solid red;"+
  179.             "background-color : #FFF;"+
  180.         "}",
  181.         "#note-container > .unsaved > .note-corner {background-color : red;}",
  182.  
  183.         `#ci ~ div[style="display:inline;"] {/* comments */
  184.             display : block !important;
  185.             max-width : 1000px;
  186.             margin : 0px 10px;
  187.         }`,
  188.  
  189.         `.edit-overlay {
  190.             position : relative;
  191.             max-height : 0px;
  192.             opacity : 0.85;
  193.             z-index : 1;
  194.         }`,
  195.         `.edit-overlay > table {
  196.             max-width : 100vw;
  197.             width : -moz-available;
  198.             padding : 7px 5px;
  199.             background-color : rgb(41, 49, 41);
  200.         }`,
  201.         `.edit-overlay #tags, .edit-overlay #source {
  202.             width : -moz-available;
  203.             color : white !important;
  204.             text-shadow: 0px 0px 2px black;
  205.         }`,
  206.         `.edit-overlay #tags {
  207.             padding : 4px;
  208.         }`
  209.     ]);
  210.  
  211.     /* find "original image" link */
  212.     let OrigLink = (() => {
  213.         let Sidebar = document.getElementsByClassName(`sidebar`)[0];
  214.         if (!Sidebar) {return null;};
  215.         let LinkNode = Array.from(Sidebar.getElementsByTagName(`a`)).find(
  216.             function(Elem) {
  217.                 if (Elem.childNodes.length !== 1) {return false;};
  218.                 let Fc = Elem.firstChild;
  219.                 if (
  220.                     Fc && Fc.nodeType === Node.TEXT_NODE &&
  221.                     Fc.textContent === `Original image`
  222.                 ) {
  223.                     return true;
  224.                 };
  225.                 return false;
  226.             }
  227.         );
  228.         if (!LinkNode) {return null;};
  229.         return LinkNode;
  230.     })();
  231.     if (!OrigLink) {return;};
  232.  
  233.     /* find original image dimensions */
  234.     let ImgWh = (() => {
  235.         let TxtNode = document.evaluate(
  236.             `.//text()[contains(., 'Size:')]`, document.getElementById(`stats`),
  237.             null, XPathResult.ANY_TYPE, null
  238.         ).iterateNext();
  239.         if (TxtNode === null) {return [NaN, NaN];};
  240.         let RexResult = TxtNode.textContent.match(/(\d+)x(\d+)/);
  241.         if (RexResult === null) {return [NaN, NaN];};
  242.         return [parseInt(RexResult[1]), parseInt(RexResult[2])];
  243.     })();
  244.  
  245.     let NoteBox = document.getElementById(`note-container`);
  246.     if (!NoteBox) {
  247.         NoteBox = document.createElement(`div`);
  248.         NoteBox.setAttribute(`id`, `note-container`);
  249.     };
  250.  
  251.     /* find or create the image element */
  252.     let Img = document.getElementById(`image`);
  253.     if (Img) {
  254.         Img.removeAttribute(`width`);
  255.         Img.removeAttribute(`height`);
  256.     } else {
  257.         Img = document.createElement(`img`);
  258.         Img.setAttribute(`id`, `image`);
  259.  
  260.         let insert = E => {
  261.             NoteBox.parentNode.insertBefore(E, NoteBox.nextSibling);
  262.         };
  263.  
  264.         let Div = document.createElement(`div`);
  265.         Div.setAttribute(`style`, `margin-bottom: 1em;`);
  266.         insert(Div);
  267.         insert(document.createElement(`br`));
  268.         insert(Img);
  269.     };
  270.     Img.setAttribute(`src`, OrigLink.getAttribute(`href`));
  271.     Img.removeAttribute(`onclick`);
  272.     Img.classList.add(`fit-viewport`);
  273.     Img.addEventListener(`click`, () => {
  274.         Img.classList.toggle(`fit-viewport`);
  275.     });
  276.  
  277.     {/* ? */
  278.         let E = document.querySelector(`#tag-sidebar`).parentNode;
  279.         E.setAttribute(`id`, `taglinks-container`);
  280.     };
  281.    
  282.     {/* move status notice into sidebar */
  283.         let StatusNotice = document.querySelector(`#content .status-notice`);
  284.         if (StatusNotice) {
  285.             let E = document.querySelector(`#taglinks-container`);
  286.             E.parentNode.insertBefore(StatusNotice, E);
  287.         };
  288.     };
  289.  
  290.     {/* remove the 'cum on this' section */
  291.         let E = document.evaluate(
  292.             `.//h5//text()[contains(., 'Cum on this')]/../..`, document.body,
  293.             null, XPathResult.ANY_TYPE, null
  294.         ).iterateNext();
  295.         E.parentNode.removeChild(E);
  296.     };
  297.  
  298.     let RightBox = document.createElement(`div`);
  299.     RightBox.setAttribute(`class`, `right-box side-box`);
  300.     move_childnodes(document.querySelector(`#post-view > .sidebar`), RightBox);
  301.  
  302.     let LeftBox = document.createElement(`div`);
  303.     LeftBox.setAttribute(`class`, `left-box side-box`);
  304.     LeftBox.appendChild(document.getElementById(`header`));
  305.  
  306.     let CentreBox = document.createElement(`div`);
  307.     CentreBox.setAttribute(`class`, `centre-box`);
  308.     move_childnodes(document.getElementById(`image`).parentNode, CentreBox);
  309.  
  310.     {
  311.         let ContentBox = document.getElementById(`content`);
  312.         while (ContentBox.hasChildNodes()) {
  313.             ContentBox.removeChild(ContentBox.lastChild);
  314.         };
  315.         CentreBox.appendChild(RightBox);
  316.         CentreBox.appendChild(LeftBox);
  317.         ContentBox.appendChild(CentreBox);
  318.     };
  319.  
  320.     {/* put #image and #note-container in some boxes */
  321.         let OuterBox = document.createElement(`div`);
  322.         OuterBox.setAttribute(`style`, `text-align : center;`);
  323.         Img.parentNode.insertBefore(OuterBox, Img);
  324.  
  325.         let InnerBox = document.createElement(`div`);
  326.         InnerBox.setAttribute(
  327.             `style`, `display : inline-block; position : relative;`
  328.         );
  329.         InnerBox.appendChild(NoteBox);
  330.         InnerBox.appendChild(Img);
  331.  
  332.         OuterBox.appendChild(InnerBox);
  333.  
  334.     };
  335.  
  336.     {/* widen the searchbar */
  337.         let Bar = document.getElementById(`stags`);
  338.         Bar.removeAttribute(`size`);
  339.         Bar.setAttribute(`style`, `width : -moz-available;`);
  340.     };
  341.  
  342.     if (!isNaN(ImgWh[0]+ImgWh[1])) {/* image top margin */
  343.         let adjust_margin = function() {
  344.             let WinH = window.innerHeight;
  345.             let ImgH = ImgWh[1];
  346.             if (WinH <= ImgH) {Img.setAttribute(`style`, ``); return;};
  347.             let V = Math.min(Math.pow((WinH - ImgH)/60, 2), WinH/2 - ImgH/2);
  348.             Img.setAttribute(`style`, `margin-top : `+Math.floor(V)+`px;`);
  349.             NoteBox.setAttribute(`style`, `margin-top : `+Math.floor(V)+`px;`);
  350.         };
  351.         window.addEventListener(`resize`, adjust_margin, false);
  352.         adjust_margin();
  353.     };
  354.  
  355.     {/* overlay edit controls */
  356.         let E = document.getElementById(`edit_form`);
  357.         E.classList.add(`edit-overlay`);
  358.         E.parentNode.insertBefore(E, E.parentNode.firstChild);
  359.  
  360.         for (let X of [
  361.             `#title`, `input[name="parent"]`, `#next_post`, `#previous_post`
  362.         ]) {
  363.             E.querySelector(X).style.display = `none`;
  364.         };
  365.  
  366.         let Submit = E.querySelector(`input[name="submit"]`);
  367.         let Cancel = document.createElement(`button`);
  368.         Cancel.setAttribute(`form`, ` `);
  369.         Cancel.textContent = `Cancel`;
  370.         Submit.parentNode.insertBefore(Cancel, null);
  371.  
  372.         Cancel.addEventListener(`click`, Ev => {
  373.             E.style.display = `none`;
  374.         });
  375.     };
  376.  
  377.     {/* tag editor button */
  378.         let E = document.getElementById(`taglinks-container`);
  379.         let Btn = document.createElement(`button`);
  380.         Btn.setAttribute(`form`, ` `);
  381.         Btn.textContent = `Edit`;
  382.         Btn.style.marginLeft = `0.5em`;
  383.         E.firstChild.insertBefore(Btn, null);
  384.  
  385.         let Ed = document.getElementById(`edit_form`);
  386.         Btn.addEventListener(`click`, Ev => {
  387.             Ed.style.display = Ed.style.display === `none` ? `` : `none`;
  388.         });
  389.  
  390.         (new MutationObserver(() => {
  391.             let Txt = Ed.style.display === `none` ? `Edit` : `Cancel editing`;
  392.             if (Btn.textContent !== Txt) {Btn.textContent = Txt;};
  393.         })).observe(Ed, {attributes : true});
  394.     };
  395.  
  396.     /* ad space */
  397.     document.getElementById(`bottom`).remove();
  398.  
  399.     {/* correct note positioning */
  400.         let Txt = document.createTextNode(`(function() {
  401.             "use strict";
  402.             if (Note === undefined) {return;};
  403.             let adjust_all = function() {
  404.                 let Idx = 0; let Arr = Note.all; let Len = Arr.length;
  405.                 for (; Idx < Len; Idx += 1) {Arr[Idx].adjustScale();};
  406.             };
  407.             window.addEventListener("resize", adjust_all, false);
  408.             adjust_all();
  409.         })()`);
  410.         let Script = document.createElement(`script`);
  411.         Script.setAttribute(`type`, `application/javascript;version=1.8`);
  412.         Script.appendChild(Txt);
  413.         if (Img.complete) {
  414.             document.body.appendChild(Script);
  415.         } else {
  416.             Img.addEventListener(`load`, function f() {
  417.                 Img.removeEventListener(`load`, f);
  418.                 document.body.appendChild(Script);
  419.             }, false);
  420.         };
  421.     };
  422. };
  423.  
  424. let adjust_gallery_page = function() {
  425.     insert_style_rules([
  426.         `#content {padding : 0px !important;}`,
  427.         `#post-list {display : flex !important; align-items : flex-start;}`,
  428.         `#post-list > * {float : none !important;}`,
  429.         `#post-list > .sidebar {
  430.             width : auto !important;
  431.             max-width : 19em !important;
  432.             margin-right : auto !important;
  433.             padding : 7px 10px 7px 20px;
  434.             flex-shrink : 0;
  435.             align-self : stretch;
  436.             background-color : rgba(0, 0, 0, 0.15);
  437.         }`,
  438.         `#post-list > .content {
  439.             display : flex;
  440.             flex-direction : column;
  441.             justify-content : center;
  442.             align-items : center;
  443.             min-height : calc(100vh - 14px);
  444.             width : auto !important;
  445.             flex-grow : 1;
  446.             text-align : center;
  447.             padding : 7px 0px;
  448.         }`,
  449.         `#post-list .image-container {
  450.             display : inline-block;
  451.             width : -moz-fit-content;
  452.         }`,
  453.         `#header li {display : block !important;}`,
  454.         `#header {
  455.             height : 7em;
  456.             overflow : hidden;
  457.             position : relative;
  458.             border-width : 1px;
  459.             border-color : #505a50;
  460.             border-style : solid;
  461.         }`,
  462.         `#header.open {height : auto !important;}`,
  463.         `#header > .more {
  464.             display : block;
  465.             position : absolute;
  466.             bottom : 0px;
  467.             width : 100%;
  468.             text-align : center;
  469.             background-color : #505a50 !important;
  470.             color : #303030 !important;
  471.         }`,
  472.         `#header.open > .more {position : relative;}`,
  473.         `#header > * {margin : 0px !important;}`,
  474.         `.blacklisted-image {background : unset !important;}`,
  475.         `.blacklisted-image > a {display : block !important;}`,
  476.         `.blacklisted-image > a > img {
  477.             display : block; border : 2px solid black;
  478.         }`,
  479.         `.blacklisted-image > a:after {
  480.             content : "[blacklisted]";
  481.             color : #aaa;
  482.             background-color : black;
  483.         }`,
  484.         `.thumb > a > img.preview {
  485.             vertical-align : unset !important;
  486.         }`,
  487.         `.thumb > a {
  488.             border-bottom : 1px dotted #B0E0B0 !important;
  489.         }`,
  490.         `.thumb > a:visited {
  491.             border-bottom-color : #303a30 !important;
  492.         }`,
  493.     ]);
  494.  
  495.     let Header = document.querySelector(`#header`);
  496.     if (Header) {
  497.         let E = document.createElement(`a`);
  498.         E.setAttribute(`href`, `#`);
  499.         E.addEventListener(`click`, () => Header.classList.toggle(`open`));
  500.         E.setAttribute(`class`, `more`);
  501.         E.appendChild(document.createTextNode(`• • •`));
  502.         Header.appendChild(E);
  503.     };
  504.  
  505.     let Sidebar = document.querySelector(`#post-list > .sidebar`);
  506.     Sidebar.insertBefore(Header, Sidebar.firstChild);
  507.  
  508.     let Content = document.querySelector(`#post-list > .content`);
  509.     let ImageContainer = Content.querySelector(
  510.         `#post-list > .content > div:not([id])`
  511.     );
  512.     ImageContainer.classList.add(`image-container`);
  513.     let Paginator = Content.querySelector(`#paginator`);
  514.  
  515.     while (Content.lastChild) {Content.removeChild(Content.lastChild);};
  516.     Content.appendChild(ImageContainer);
  517.     if (Paginator) {Content.appendChild(Paginator);};
  518.     let Tips = document.querySelector(`#header > div.tips`);
  519.     if (Tips) {
  520.         Tips.setAttribute(`style`, (
  521.             `background-image : none !important; text-align : center;`
  522.         ));
  523.         Content.appendChild(Tips);
  524.     };
  525.  
  526.     {/* ? */
  527.         let E = document.querySelector(`#tag-sidebar`).parentNode;
  528.         E.setAttribute(`id`, `taglinks-container`);
  529.         E.querySelector(`h5`).setAttribute(`style`, `display : none;`);
  530.     };
  531.  
  532.     {/* widen the searchbar */
  533.         let Bar = document.getElementById(`tags`);
  534.         Bar.removeAttribute(`size`);
  535.         Bar.setAttribute(`style`, `width : -moz-available;`);
  536.     };
  537. };
  538.  
  539. /* --- --- */
  540.  
  541. let on_document_head_loaded = function() {
  542.     if (document.location.pathname === `/`) {
  543.         document.location.pathname = `/index.php?page=post&s=list`
  544.         return;
  545.     };
  546.  
  547.     if (RegExp(
  548.         `/(index.php|chat.php|icameout.php|(stats(/[^?]*)?))?(\\?.*)?$`
  549.     ).test(document.location.pathname+document.location.search)) {
  550.         insert_style_rules(GlobalStyleRules);
  551.     };
  552. };
  553.  
  554. /* delay until document fully loaded */
  555. document.addEventListener(`DOMContentLoaded`, function() {
  556.     /* only apply to …/index.php?page=post… */
  557.     if (document.location.pathname !== `/index.php`) {return;};
  558.     let QueryTbl = {};
  559.     document.location.search.substring(1).split(`&`).forEach(function(Txt) {
  560.         let Arr = Txt.split(`=`);
  561.         QueryTbl[Arr[0]] = Arr[1];
  562.     });
  563.     if (QueryTbl[`page`] !== `post`) {return;};
  564.     if (QueryTbl[`s`] === `view`) {return adjust_image_page();};
  565.     if (QueryTbl[`s`] === `list`) {return adjust_gallery_page();};
  566. });
  567.  
  568. {/* delay until document.head exists */
  569.     let on_root_append = function(_, Obs) {
  570.         if (!document.head) {return;};
  571.         Obs.disconnect();
  572.         on_document_head_loaded();
  573.     };
  574.  
  575.     let on_document_append = function(_, Obs) {
  576.         if (!document.documentElement) {return;};
  577.         Obs.disconnect();
  578.         let NextObs = new MutationObserver(on_root_append)
  579.         NextObs.observe(document.documentElement, {childList : true});
  580.         on_root_append(null, NextObs);
  581.     };
  582.  
  583.     let Obs = new MutationObserver(on_document_append);
  584.     Obs.observe(document, {childList : true});
  585.     on_document_append(null, Obs);
  586. };
  587.  
  588. /* -------------------------------------------------------------------------- */
  589.  
  590. /*
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634. */
  635.  
  636. /* -------------------------------------------------------------------------- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement