bellcollar

8ch/kun uwupgrade script

Mar 7th, 2020
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   _    _          _    _                           _      
  3.  | |  | |        | |  | |                         | |    
  4.  | |  | |_      _| |  | |_ __   __ _ _ __ __ _  __| | ___
  5.  | |  | \ \ /\ / / |  | | '_ \ / _` | '__/ _` |/ _` |/ _ \
  6.  | |__| |\ V  V /| |__| | |_) | (_| | | | (_| | (_| |  __/
  7.   \____/  \_/\_/  \____/| .__/ \__, |_|  \__,_|\__,_|\___|
  8.                         | |     __/ |                    
  9.                         |_|    |___/                      
  10. */
  11.  
  12. //version 1.2.0
  13. //Quality of life scripts for 8ch/kun
  14.  
  15. function uwupgrade_board()
  16. {
  17.     var boards = [
  18.         //"ALL!", //uncomment for use on all boards
  19.         "fur",
  20.         "furry",
  21.         "test",
  22.     ]
  23.  
  24.     configure_post_form(boards);
  25.     enable_spoiler_settings(boards);
  26. }
  27.  
  28. function on_ready(fn)
  29. {
  30.     if (document.readyState == 'loading') {
  31.         window.addEventListener('DOMContentLoaded', fn);
  32.     }
  33.  
  34.     else {
  35.         fn();
  36.     }
  37. }
  38. on_ready(uwupgrade_board);
  39.  
  40. function valid_board(list)
  41. {
  42.     for (var i = 0; i < list.length; i++) {
  43.         if(list[i] === window.board_name || list[i] === "ALL!"){
  44.             return true;
  45.         }
  46.     }
  47.     return false;
  48. }
  49.  
  50. function url_contains(string)
  51. {
  52.     return (window.location.href.indexOf(string) > -1) ? true : false;
  53. }
  54.  
  55. /*
  56.   ___        _     ___              
  57.  | _ \___ __| |_  | __|__ _ _ _ __  
  58.  |  _/ _ (_-<  _| | _/ _ \ '_| '  \
  59.  |_| \___/__/\__| |_|\___/_| |_|_|_|
  60.                                    
  61. */
  62.  
  63. function configure_post_form(boards, active=true)
  64. {
  65.  
  66.     function get_row(table, name)
  67.     {
  68.         var list = table.getElementsByTagName("th");
  69.  
  70.         for (var i = 0; i < list.length; i++)
  71.         {
  72.             if (list[i].innerHTML.indexOf(name) > -1){
  73.                 return email = list[i].parentElement;
  74.             }
  75.         }
  76.         return null;
  77.     }
  78.  
  79.     function hide_email()
  80.     {
  81.         var mainTable = document.getElementsByClassName("post-table")[0];
  82.  
  83.         var email = get_row(mainTable, "Email");
  84.         email.style.display = "none";
  85.     }
  86.  
  87.     function move_password()
  88.     {
  89.         var mainTable = document.getElementsByClassName("post-table")[0];
  90.         var subTable = document.getElementsByClassName("post-table-options")[0];
  91.  
  92.         var password = get_row(mainTable, "Password");
  93.         var other = get_row(subTable, "Other");
  94.         subTable.childNodes[0].insertBefore(password, subTable.childNodes[0].childNodes[0]);
  95.     }
  96.  
  97.     function rename_options()
  98.     {
  99.         var subTable = document.getElementsByClassName("post-table-options")[0];
  100.  
  101.         var other = get_row(subTable, "Options");
  102.         other.getElementsByTagName("th")[0].innerHTML = "Other";
  103.     }
  104.  
  105.     function rename_archive()
  106.     {
  107.         var mainTable = document.getElementsByClassName("post-table")[0];
  108.  
  109.         var options = get_row(mainTable, "Archive");
  110.         options.getElementsByTagName("th")[0].innerHTML = "Options";
  111.     }
  112.  
  113.     function create_option()
  114.     {
  115.         var mainTable = document.getElementsByClassName("post-table")[0];
  116.  
  117.         var row = document.createElement("TR");
  118.         var header = document.createElement("TH");
  119.         var data = document.createElement("TD");
  120.  
  121.         header.innerHTML = "Options"
  122.         row.appendChild(header);
  123.         row.appendChild(data);
  124.  
  125.         mainTable.childNodes[0].insertBefore(row, mainTable.childNodes[0].childNodes[5]);
  126.     }
  127.  
  128.     function option_settings()
  129.     {
  130.         var mainTable = document.getElementsByClassName("post-table")[0];
  131.         var options = get_row(mainTable, "Options");
  132.         var data = options.getElementsByTagName("td")[0];
  133.  
  134.         //create anchor/sage option
  135.         var anchor = document.createElement("INPUT");
  136.         anchor.title = "Don't bump";
  137.         anchor.id = "no-bump";
  138.         anchor.name = "no-bump";
  139.         anchor.type = "checkbox"
  140.  
  141.         var anchorLabel = document.createElement("LABEL");
  142.         anchorLabel.appendChild(anchor);
  143.         anchorLabel.innerHTML += "Anchor post";
  144.  
  145.         //create spoiler option
  146.         var spoiler = document.createElement("INPUT");
  147.         spoiler.title = "Hide file thumbnails with a spoiler";
  148.         spoiler.id = "spoiler";
  149.         spoiler.name = "spoiler";
  150.         spoiler.type = "checkbox"
  151.  
  152.         var spoilerLabel = document.createElement("LABEL");
  153.         spoilerLabel.appendChild(spoiler);
  154.         spoilerLabel.innerHTML += "Spoiler all files";
  155.  
  156.         //append options
  157.         data.innerHTML = ""
  158.         data.appendChild(spoilerLabel);
  159.         data.appendChild(anchorLabel);
  160.     }
  161.  
  162.     function other_settings()
  163.     {
  164.         var subTable = document.getElementsByClassName("post-table-options")[0];
  165.         var other = get_row(subTable, "Other");
  166.         var data = other.getElementsByTagName("td")[0];
  167.  
  168.         //remove extra
  169.         var oldAnchor = data.getElementsByClassName("no-bump-option")[0];
  170.         data.removeChild(oldAnchor);
  171.     }
  172.  
  173.     if(!valid_board(boards))
  174.         return;
  175.  
  176.     if(!url_contains("catalog")) //thread
  177.     {
  178.         hide_email();
  179.         move_password();
  180.  
  181.         rename_options();
  182.         rename_archive();
  183.  
  184.         option_settings();
  185.         other_settings();
  186.     }
  187.     else //catalog
  188.     {
  189.         hide_email();
  190.         rename_options();
  191.  
  192.         create_option();
  193.  
  194.         option_settings();
  195.         other_settings();
  196.     }
  197.    
  198. }
  199.  
  200. /*
  201.   ___           _ _            ___         _           _
  202.  / __|_ __  ___(_) |___ _ _   / __|___ _ _| |_ _ _ ___| |
  203.  \__ \ '_ \/ _ \ | / -_) '_| | (__/ _ \ ' \  _| '_/ _ \ |
  204.  |___/ .__/\___/_|_\___|_|    \___\___/_||_\__|_| \___/_|
  205.      |_|                                                
  206. */
  207.  
  208. //Adapted from marktaiwan => https://github.com/marktaiwan/8kun-disable-spoiler-thumbnail
  209. //It's not feasible to unspoiler images in catalog
  210. function enable_spoiler_settings(boards)
  211. {
  212.  
  213.     function unspoiler(postImage)
  214.     {
  215.         if( postImage.src.endsWith(`/static/assets/${window.board_name}/spoiler.png`) ||  
  216.             postImage.src.endsWith('/static/spoiler.png'))
  217.         {
  218.             //❔
  219.             const fileURL = postImage.closest('div.file').querySelector('.fileinfo>a[title]').href;
  220.             if (fileURL === undefined || fileURL.indexOf('/file_store/') == -1) return;
  221.  
  222.             const spoilerImg = postImage.src;
  223.             const thumbURL = fileURL.replace('/file_store/', '$&thumb/').replace((/(webm|mp4)$/), 'jpg');
  224.  
  225.             postImage.onerror = () => postImage.src = spoilerImg;
  226.             postImage.onload = () => {
  227.                 const multifile = postImage.closest('.multifile');
  228.  
  229.                 postImage.style.width = `${postImage.naturalWidth}px`;
  230.                 postImage.style.height = `${postImage.naturalHeight}px`;
  231.  
  232.                 if (multifile) {
  233.                     multifile.style.width = `${postImage.naturalWidth + 40}px`;
  234.                 }
  235.  
  236.                 //set indicator - so you know a file was successfully spoilered
  237.                 const span = postImage.closest('div.file').querySelector('.fileinfo>span');
  238.                 span.innerHTML = "❓" + span.innerHTML;
  239.             };
  240.  
  241.             postImage.src = thumbURL;
  242.         }
  243.     }
  244.  
  245.     function enable_general_setting()
  246.     {
  247.         //create state for setting if needed
  248.         if(localStorage.disable_image_spoiler === undefined) {
  249.             localStorage.disable_image_spoiler = 'false';
  250.         }
  251.  
  252.         //create setting html
  253.         var setting = document.createElement("INPUT");
  254.         setting.id = "disable-spoiler";
  255.         setting.type = "checkbox"
  256.  
  257.         var settingLabel = document.createElement("LABEL");
  258.         settingLabel.appendChild(setting);
  259.         settingLabel.innerHTML += "Remove file spoilers (extension)";
  260.        
  261.         //extend general options with setting
  262.         window.Options.extend_tab("general", settingLabel.outerHTML);
  263.  
  264.         //add toggle logic to setting
  265.         document.querySelector("#disable-spoiler").addEventListener('change', () => {
  266.             result = (localStorage.disable_image_spoiler === 'true') ? 'false' : 'true';
  267.             localStorage.disable_image_spoiler = result;
  268.         });
  269.  
  270.         if(localStorage.disable_image_spoiler === 'true') {
  271.             document.querySelector('#disable-spoiler').setAttribute('checked', true);
  272.         }
  273.     }
  274.  
  275.     function unspoiler_page()
  276.     {
  277.         document.querySelectorAll('.post-image').forEach(unspoiler);
  278.         //jQuery(document).on('new_post', function (e, post) {
  279.         //post.querySelectorAll('.post-image').forEach(unspoiler);
  280.         //});
  281.  
  282.         document.addEventListener('new_post', function (e, post){
  283.         post.querySelectorAll('.post-image').forEach(unspoiler);
  284.         });
  285.     }
  286.  
  287.     function enable_thread_setting()
  288.     {
  289.         //create state for setting if needed
  290.         if(sessionStorage.disable_image_spoiler === undefined){
  291.             sessionStorage.disable_image_spoiler = 'false';
  292.         }
  293.  
  294.         //create setting html
  295.         var setting = document.createElement("A");
  296.         setting.className = "unimportant";
  297.         setting.href = "javascript:void(0)";
  298.         setting.innerHTML = (sessionStorage.disable_image_spoiler === 'false') ?
  299.                             'Reveal Spoilers' : 'Restore Spoilers';
  300.  
  301.         var container = document.createElement("DIV");
  302.         container.id = "reveal-spoilers";
  303.         container.appendChild(setting);
  304.  
  305.         var body = document.getElementsByClassName("8kun")[0];
  306.         var target = document.getElementById("expand-all-images")
  307.         body.insertBefore(container, target.nextSibling.nextSibling);
  308.  
  309.         container.childNodes[0].addEventListener('click', function(e) {
  310.  
  311.             if(sessionStorage.disable_image_spoiler === 'false'){
  312.                 sessionStorage.disable_image_spoiler = 'true';
  313.                 e.target.innerHTML = 'Restore Spoilers'
  314.                 unspoiler_page();
  315.             }
  316.  
  317.             else{
  318.                 sessionStorage.disable_image_spoiler = 'false';
  319.                 e.target.innerHTML = '♻️page(click to undo)'
  320.             }
  321.         })
  322.  
  323.     }
  324.  
  325.     if(!valid_board(boards))
  326.         return;
  327.  
  328.     enable_general_setting();
  329.  
  330.     if(localStorage.disable_image_spoiler === 'true'){
  331.         //remove spoilers
  332.         if( window.active_page === 'thread' ||
  333.             window.active_page === 'index'){
  334.             unspoiler_page();
  335.         }
  336.     }
  337.  
  338.     else
  339.     {
  340.         if( window.active_page === 'thread'){
  341.             enable_thread_setting();
  342.         }
  343.        
  344.         //var thread = document.getElementsByClassName("post_anchor")[0].id;
  345.         if( sessionStorage.disable_image_spoiler === 'true'){
  346.             unspoiler_page();
  347.         }
  348.     }
  349.  
  350. }
  351.  
  352. /*
  353.   ___         _
  354.  | __|_ _  __| |
  355.  | _|| ' \/ _` |
  356.  |___|_||_\__,_|
  357.                
  358. */
Add Comment
Please, Sign In to add comment