BigTSDMB / SDMB Style Tweaker/Fixer

// ==UserScript==
// @name         SDMB Style Tweaker/Fixer
// @version      0.14.1
// @description  Tweak the new style changes on the Straight Dope Message Board
// @author       BigTSDMB
// @homepage     https://openuserjs.org/scripts/BigTSDMB/SDMB_Style_TweakerFixer
// @updateURL    https://openuserjs.org/meta/BigTSDMB/SDMB_Style_TweakerFixer.meta.js
// @match        *://boards.straightdope.com/*
// @grant        none
// @run-at       document-start
// @require      https://raw.githubusercontent.com/uzairfarooq/arrive/master/src/arrive.js
// @license      MIT
// ==/UserScript==

if (location.protocol == 'http:') location.replace('https' + location.href.substr(4));

function changeStyle(css, id = 'sdmb-style-tweaker') {
  if (!document.documentElement) {
    var observer = new MutationObserver( () => {
      changeStyle(css, id);
      observer.disconnect();
    } );
    observer.observe(document, { childList: true } );
    return;
  }
  var style = document.getElementById(id);
  if (!style) {
    style = document.createElement('style');
    style.id = id;
    var base = document.head || document.documentElement;
    base.appendChild(style);
  }
  style.textContent = css;
}

function tweakNewStyle() {
  //tweaks for new style
  changeStyle(`
    .page { max-width:1920px; } /* per treis's suggestion -- maxes out at 1080p */
    .thead2, .thead2 a, .thead2 a strong, .thead {  /* restore post headers to (slightly darker) gray, but with black text */
        background: #e0e0e0 !important;
        color: #000 !important;
        font-weight: normal !important;
    }
    .postbitcontrol .hideonmobile, img[src^="images/SultanThemeVB3R/ads"] { display: none; }
    table.tborder.mytoolbar, #threadtools, #threadsearch, #displaymodes { /* make top bar purple instead, like original */
        background: #330099 !important;
        border-left: solid 1px; /* add divider since buttons are the same color */
        line-height: 19px /* height of .mytoolbar */; /* horizontally centers the buttons */
        padding-right: 4px !important; /* balances out padding a bit */
    }

    /* per Mind's Eye, Wondering -- Make page navigation easier by highlighting current page number */
     .pagenav td.alt1 > a.smallfont { font-weight: normal; color: #666 }
     .pagenav td.alt2 > .smallfont > strong {
         font-weight: bolder;
         color: #000;
         border-radius: 25%;
         background: #ACE; /* per GuanoLad's suggestion */
     }

    /* Fix Selection color*/
    ::selection { background: #ACE !important; color: #000 !important; } /* CANNOT COMBINE THESE! */
    ::-moz-selection { background: #ACE !important; color: #000 !important; }

    /* Fix blocks*/
    div[style*="display: none"] { display: none !important; } /* fix spoilers */
    pre { white-space: pre !important; } /* fix code blocks */
    .converttodiv div { /* quote-box styling. */
        width: auto !important; /* fixes overlapping boxes inside quotes */
        font-style: initial !important; /* Because modifying quotes by putting them in italics is against SDMB rules! */
        background: #e9e9e9 !important;
     }
    td .converttodiv { /* additional quote-box styling */
       border: 1px #999 inset !important;
       font-size: 15px /* same size as main text */;
       /* for unsourced quotes */
       background-clip: content-box;
       box-shadow: inset 0 0 0 6px #f8f8f8;
       background: #e9e9e9 !important;
     }

    /* Fix other font size discrepancies */
    li:not(.smallfont) { font-size: 15px !important; }

    /* Advanced editor CSS changes */
    .myreply .panelsurround .panel { width: auto !important; }
    td.panelsurround div.panel > div > table:not(.fieldset) { width: 100%; }
    #vB_Editor_001 > table > tbody > tr > td > #vB_Editor_001_textarea { width: 98% !important; }

    /* Code fix for Firefox support (theme uses -webkit in multi selectors. Bad!) */
    input[type="button" i], input[type="submit" i], input[type="reset" i], input[type="file" i], button {
        align-items: flex-start;
        text-align: center;
        cursor: default !important;
        color: buttontext !important;
        background: buttonface !important;
        box-sizing: border-box;
        padding: 2px 6px 3px !important;
        border-width: 1px;
        border-style: outset;
        border-color: buttonface;
        border-image: initial;
    }

    /* user info alignment */
    .userinfodetails { width: 100% }
    .userinfodetails[valign="top"] { min-width: 150px; }
    table.userinfotable > tbody > tr > td:not(.userinfodetails) { white-space: nowrap; }

    /* alignment issues with last post column */
    div.smallfont.tbitlastpost > br:nth-child(2) { display: none !important; }
    div.smallfont.tbitlastpost { padding-bottom: 10px; white-space: nowrap !important; }

    /* new changes -- FIXED!!!
    .pagenav td.alt1 > a.smallfont { font-weight: normal; color: #666 }
    a.newreplybutton[href], a.newthreadbutton[href] { color: #fff }/**/

    /* Editor button alignment */
    #vB_Editor_001_controls > table:last-child > tbody > tr::after { content: ""; display: table-cell; width: 100%; }

    /* hide forum jump until page is finished loading (needs JS) */
    .forumjump { display: none }

    /* block dummy YouTube video that was only hidden */
    div.videoWrapper { display: none }

    /* replace quote arrow with darker one, as it is hard to see (accessibility fix) */
    img.inlineimg[src*="images/SultanThemeVB3R/buttons/viewpost.gif"] {
        width: 15px !important; padding: 15px 0 0 0; height: 0;
        background: url("data:image/gif;base64,R0lGODlhDwANAKEDAHaJnK23wP///6HD5SH5BAEKAAMALAAAAAAPAA0AAAImnI85Ae0fDHu0xSoqGDhT3n0aIHgNSJViajqo07rTaEk0ECn6UQAAOw") no-repeat;
        overflow: hidden;
    }

    /* Non-essential CSS until end */
    div#topcontrol { bottom: 5px !important; } /* after removal of forumjumper DELETE IF USING CSS ONLY!*/
    #topcontrol > img { width: 16px; }
    div.postbitcontrol > a[href*="newreply"]:first-child > img { /* replace button with one with the word quote per original */
        width: 69px !important; padding: 20px 0 0 0; height: 0;
        background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEUAAAAUCAMAAADsgQBJAAAAqFBMVEXP0c5AQT/S1NHY2tfV19Tf4d7Q0s+wsq9GPkOMjYvi5ODGyMXq7Om4urezsbXb3dqqrKmmqKWgop+anJm/wb6EhoN3eXZlZ2TNz8vW09WJh4vm6OV9fnzo6udwcW5xb3Oin6RmZGhgXmJcXVtcWV1VVlRRUlBISUdDRULLzcqsqq7u8O3n6eZUUlaWlJhKS0lLSU1PR0zIxsrr7urx8/C4trp5d3vt7+t9bvTnAAAAAXRSTlMAQObYZgAAAbVJREFUeNql0AWC7SgQQFGqcIj7c9d2m9n/zj6EdpcTt5sEQm7//6PolpCTm7+a3JyQ6M8mUUQmPzc4XypneT6YeKPJhJx+zpyORq9OzS/Vvcu5P3bXyehTvJTGvDw1VM8MwzliPnUAbbgJwjZRLyTGe6hIWVXS4PHOmOPROFWMbl1AE1tjUpl1JliqXhSF7TJUeK/YwVgDP6o1536pNgBQc76H/V5zebG/gCLlnnpZUdwLFQurTAPwO7XgfKZSvtlJ0UDBa2iy2G63FbZgnypR777i6iT1NOSUtpC6SpquFZVQU9rBNM1Bp1jAtCynkKfe60rqPVQkY66ShAqToBlj0KYFaOw0TOvxuLapd/ZqXEKFehpyX6GJWtKBq8TQMGahoTloKnKoGWWM9uYvKwn1CPMqWFXVFti1UsPZWqEr6nwFkuUwzazYQGbLsWC9hXrmzJ0YMEawVwLADhCHLqPWArHZwSpHjLcApYhXsIcWg8NMPZMgMkSCgc1sC257bfG5Q5xlldtIGQt8kMyu1IP/0CPiUQviR6r5cHGlhEAhSPeohe5XBl1HsPsr0SEh4vBHnSD/AD0Cbu2uDoWuAAAAAElFTkSuQmCC") no-repeat;
        overflow: hidden;
    } /* this tecnique preserves the ALT text. Actually replacing the image does not. */
    div.postbitcontrol > a[href*="do=newpm&forward"] > img { /* replace bad forward button */
        width: 82px !important; padding: 20px 0 0 0; height: 0;
        background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAAUCAMAAAAp85rYAAAAn1BMVEUAAABAQT/X2NbT1dLS1NHQ0s9mZ2Xf4d7Nz8vFx8S+wL2ytLGnqaaeoJ2UlpOLjYqIioeDhYJ3eXdvcW5cXltSVFFKSEvc3ttgXmLq7Om6vLm2uLXa3NmusK1ubHDi5ODo6ufn6eZZV1uam5hSUFPBw8BJS0hISUdPR0xFQ0bm6OVAPkJpZ2vLzcrr7urx8/BNS0/u8O2BgYLt7+vp6+hih9qBAAAAAXRSTlMAQObYZgAAAcZJREFUeF6tkcWu5DAUBX2MYaZGZng08//fNiaps3ya7lpcJZXrkqUQ8vnnnUw+Cfn+eCt/P77J5CXYcniyDCeT9WRC1i+xvI3Zr9fX9Zps/ofD1IvBlJZ7n+w2m+tmQ66O8ojr75HwwiYfrPNJ68jKkqKerX6PhBc2+cWji0ta55JhhZ1JRjNuZriKolm0WvEZN848RN47O0oa9jQaJ5Uhh0ZFLYBSKSkr1DWESs1b26ifruvu3msJHKEcLnn7EvHFJK1zSVUi1oePsaiQKokmj1MkqkarQmTqJ1V5532IJgzbZ9KR0rtL9jrZW3LEfYyS0gB1L8H7PkTJUSPMEJqF7Jw7nyOhVKJ3+OQ+SOwtrSNzi07OY+SUUsi5hFFNk0Acs7rVX/fb4Zxr7zZNcu4YfHFqy511hFpyLOgUFaUHPSSMylDXtJKnkqbI6OKcO+9vSR2DL/o/bp1PFliYxSJpsPDJA7CjO2CmR5ncz4XzBzRJgXHycnjo4ijJLDqpZ3XS+4xJWNcgYgwtM6LdDYX3uxOqAsyxNaXq6+aTjFHGCH+J8jZmyznjnIiXmG67J9tYCC4ECd4LDQLC35sUASdEPN5JIMg/xkmXCJKFlHcAAAAASUVORK5CYII") no-repeat;
        overflow: hidden;
    }
    .bigusername { white-space: nowrap } /* don't wrap usernames, per complaint */
    .userinfotable { padding-top: 1px; }
    .userinfodetails { padding-top: 2px; }
    .newthreadbutton, .newreplybutton { /* better color match to old reply button */
        background: linear-gradient(to bottom, #9fbcee 0%, #314568 100%) !important;
    }
    font[color="Yellow"] { text-shadow: 0.2px 0px 0.8px #000, -0.2px 0px 0.8px #000; } //make yellow fonts legible
  `);
function whenLoaded() {
    /*if (location.pathname == '/sdmb/forumdisplay.php') { //Add moderators back to forum pages, per dalej42
      $('table.displayoptions').load(document.URL + '&styleid=12 form > table.tborder:gt(1)', () => {
        document.cookie = 'bbstyleid=16;path=/'; //reset cookie to to new style
        document.cookie = 'style=none;max-age=0';  //delete extra cookie
      });
      document.cookie = 'style=new'; //add extra cookie, just in case user stops loading page too early
    }/**/
    var forumjump = document.getElementsByClassName('forumjump')[0];
    if (forumjump) forumjump.classList.remove('forumjump'); //should not follow scroll on Desktop
    var useSig = document.getElementById('cb_signature');
    if (useSig) { useSig.checked = false; } //Do NOT use sigs by default, per SDMB rules!
    var dummyVideos = document.getElementsByClassName('videoWrapper');
    if (dummyVideos && dummyVideos[0]) { dummyVideos[0].remove(); } //stop loading of hidden dummy video
    if (location.pathname == '/sdmb/search.php') { //fix advanced search on high zoom levels
      changeStyle('html.range_3 .panelsurround .panel .bginput { width: initial !important }', 'search-fix')
    }
    //add higher resolution icons
      var icon = [];
      icon[0] = document.createElement('link');
      Object.assign(icon[0], { rel: 'icon', href: 'https://www.straightdope.com/wp-content/uploads/2017/11/sdico-1.png' });
      icon[1] = icon[0].cloneNode();
      icon[1].rel = 'apple-touch-icon-precomposed';
      icon[2] = document.createElement('meta');
      Object.assign(icon[2], { name: 'msapplication-TileImage', content: icon[0].href });
      for (let i = 0; i < icon.length; i++) { document.head.appendChild(icon[i]); }
      /**/
  }
  if (document.readyState !== "loading") { whenLoaded(); }
  addEventListener('DOMContentLoaded', whenLoaded);

  /* not useful in my design
  var multiquotes = document.getElementsByClassName('multiquotelink');
  for (let i = 0; i<multiquotes.length; i++) {
    multiquotes[i].parentNode.addEventListener('click', function () {
      mq_click(this.firstChild.id.substr(3));
    });
  }/**/
}

function tweakOldStyle() {
  document.cookie = 'style=old;max-age=0';
}

function tweak() {
  if (document.cookie.match('style=new') && document.cookie.match('bbstyleid=12')) { //if user stops page before mod names load
    document.cookie = 'bbstyleid=16;path=/';
    document.cookie = 'style=none;max-age=0';
    location.reload();
  }
  if (document.querySelector('link[href="images/SultanThemeVB3R/css/stvb3r.css"]')) {
    tweakNewStyle();
  } else {
    tweakOldStyle();
  }
}

//main script
tweakNewStyle();

//fix spoiler issues inside quotes as quickly as possible, ideally making it never visible
function runEarly() {
  changeStyle('div[style*="display: none"] { display: none !important; } ', 'spoiler-fix');
}
if (document.readyState !== "loading") {
   runEarly();
 } else if (document.arrive) {
   document.arrive('a', { onceOnly: true }, runEarly);
 } else {
   addEventListener('DOMContentLoaded', runEarly);
}

/**/