thisvid infinite scroll and filter

[s]try to take over the world![/s] infinite scroll, filter: public, private, duration

Fra 30.01.2024. Se den seneste versjonen.

// ==UserScript==
// @name         thisvid infinite scroll and filter
// @license      MIT
// @namespace    http://tampermonkey.net/
// @version      2.0.0
// @description  [s]try to take over the world![/s] infinite scroll, filter: public, private, duration
// @author       smartacephale
// @match        https://thisvid.com/
// @match        https://thisvid.com/latest-updates/*
// @match        https://thisvid.com/tags/*
// @match        https://thisvid.com/categories/*
// @match        https://thisvid.com/*/?q=*
// @match        https://thisvid.com/members/*
// @match        https://thisvid.com/videos/*
// @icon         data:image/jpeg;base64,/9j/4QCwRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAADEBAgAcAAAASgAAADIBAgAUAAAAZgAAABMCAwABAAAAAQAAAGmHBAABAAAAegAAAAAAAABBQ0QgU3lzdGVtcyBEaWdpdGFsIEltYWdpbmcAMjAwODowMToxNCAwNDoxMzowMwADAJCSAgADAAAAODYAAAKgBAABAAAAZAAAAAOgBAABAAAAZAAAAAAAAAAAAAAA/8AAEQgAZABkAwEhAAIRAQMRAf/bAIQAAgEBAQEBAgEBAQICAgIDBQMDAgIDBgQEAwUHBgcHBwYHBggJCwkICAoIBgcKDQoKCwwMDQwHCQ4PDgwPCwwMDAEDAwMEAwQIBAQIEgwKDBISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhIS/8QAoAAAAAcBAQEAAAAAAAAAAAAAAQMEBQYHCAIJABAAAgECBAMGAgUJBwUBAAAAAQIDBBEABQYhBxIxCBMiQVFhcYEUIzKRoQkVFkJSksHh8FNUVWKx0fEXJFaTlNIBAAIDAQEAAAAAAAAAAAAAAAMEAQIFBgARAAICAQMDAwIFBQAAAAAAAAECAAMRBBIhMUFRBRMiMnEUI2GR8BUzscHR/9oADAMBAAIRAxEAPwDfkVGqHkjkLEXbc3ucK0iVAEWXmdTflUWxojlsy2AYlz7PMj02CmZZpyTOpdaOC7SybEiygefK25sLjrgibiB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h1DNR3WN8/X6YASoUsEkuqsQBcqAT0O2G+j7HnZXgzdpIOz1oxJIJfDKuTUwY/ZO/g9+nTDaaeulQFEo7sxPMPr+yj2dKhnlXhBkcLBBIDTUyxbhT+yBf7PnfqfXHC9k7s457E1DnPB3IquBI427iqpllUkdNmv8Ahhh6k29IBHYN1kv4Z9mXgvo2XL58h0bAkOT5dNS5fQT/AF1NQRVVTHNOsUbgrGXeFfGtnUFgjKGN3HRfBXh9wsgzHK9GZbPDHqVFfMXqamSoknMUQCFXkLGMgVEwvGVJErAk3womlpPO2Gsdh3ijU/C3R2eVGT0mbUUkseS0ggoo45DCsMayOwWycoILMSb35j1vYW4yHhZpPJcozHJ8kOYUFLVUCxPDl9dNTkKGMlldGDqefxEqQSd77CzK1JmQpyMmP+Sai1ZovKaXRum9YZjTZdlNPDRUlOsgIihjiRES/LvZVAudza5ubkq/+o/Eb/zzMv31/wDzgvtLLT//2Q==
// @grant        GM_addStyle
// @grant        GM_addElement
// @run-at       document-end
// ==/UserScript==
(function () {
  // biome-ignore lint/suspicious/noRedundantUseStrict: <explanation>
  'use strict';

  console.clear();
  console.log('\n\ntapermonkey-thisvidscript\n\n');

  // biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
  class Utils {
    static $ = (x, e = document.body) => e.querySelector(x);
    static $$ = (x, e = document.body) => e.querySelectorAll(x);

    static findElementsBetweenIds = (startId, endId) => {
      const startElement = document.getElementById(startId);
      const endElement = document.getElementById(endId);

      if (!startElement || !endElement) {
        return [];
      }

      const elementsBetween = [];
      let currentElement = startElement.nextElementSibling;

      while (currentElement && currentElement !== endElement) {
        elementsBetween.push(currentElement);
        currentElement = currentElement.nextElementSibling;
      }

      return elementsBetween;
    };

    static parseHTML = (str) => {
      const temp = document.createElement('html');
      temp.innerHTML = str;
      return temp;
    };

    static fetchHtml = async (url) =>
      fetch(url)
        .then((r) => r.text())
        .then((d) => parseHTML(d));

    static getRandomRgb = () => {
      const n = Math.round(0xffffff * Math.random());
      return `rgb(${n >> 16},${(n >> 8) & 255},${n & 255})`;
    };

    static parseCSSUrl = (s) => s.match(/(?<=\(").*(?="\))/)[0];

    static timeToSeconds = (t) =>
      (t.match(/\d+/gm) || ['0'])
        .reverse()
        .map((s, i) => parseInt(s) * 60 ** i)
        .reduce((a, b) => a + b) || 0;

    static circularShift = (n, c = 6, s = 1) => (n + s) % c || c;

    static isElementInViewport = (el) => {
      const rect = el.getBoundingClientRect();
      return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <=
        (window.innerHeight ||
          document.documentElement
            .clientHeight) /* or $(window).height() */ &&
        rect.right <=
        (window.innerWidth ||
          document.documentElement.clientWidth) /* or $(window).width() */
      );
    };

    static toggleClass = (element, className, condition) => {
      if (condition) {
        element.classList.add(className);
      } else {
        element.classList.remove(className);
      }
    };

    static parseDom = (html) => {
      return new DOMParser().parseFromString(html, 'text/html').body.firstChild;
    };

    static parseIntegerOr = (n, or) =>
      Number.isInteger(parseInt(n)) ? parseInt(n) : or;
  }

  const {
    $,
    $$,
    findElementsBetweenIds,
    fetchHtml,
    parseHTML,
    parseCSSUrl,
    circularShift,
    isElementInViewport,
    timeToSeconds,
    getRandomRgb,
    toggleClass,
    parseDom,
    parseIntegerOr,
  } = Utils;

  class Tick {
    constructor(interval) {
      this.interval = interval;
    }

    start(callback) {
      if (this.ticker) {
        this.stop();
      }
      callback();
      this.ticker = setInterval(callback, this.interval);
    }

    stop() {
      clearInterval(this.ticker);
      this.ticker = false;
    }
  }

  class ReactiveLocalStorage {
    constructor(data) {
      if (data) {
        Object.assign(this, data);
        this.observeProps(this);
      }
    }

    getLS(prop) {
      return JSON.parse(localStorage.getItem(prop));
    }

    setLS(prop, value) {
      localStorage.setItem(prop, JSON.stringify(value));
    }

    toObservable(obj, prop) {
      const lsvalue = this.getLS(prop);
      let value = lsvalue !== null ? lsvalue : obj[prop];

      Object.defineProperty(obj, prop, {
        get() {
          return value;
        },
        set(newValue) {
          this.setLS(prop, newValue);
          value = newValue;
        },
      });
    }

    observeProps(obj) {
      for (const [key, _] of Object.entries(obj)) {
        this.toObservable(obj, key);
      }
    }
  }

  const SCROLL_RESET_DELAY = 500;
  const ANIMATION_DELAY = 750;

  // biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
  class DomManager {
    static thumbIsPrivate(t) {
      return t.firstElementChild.classList.contains('private');
    }

    static filterPrivate(container = document.body) {
      // biome-ignore lint/complexity/noForEach: <explanation>
      data.forEach((v) => toggleClass(v.element, 'filtered-private',
        state.filterPrivate && thumbIsPrivate(v.element)));
    }

    static filterPublic(container = document.body) {
      // biome-ignore lint/complexity/noForEach: <explanation>
      data.forEach((v) => toggleClass(v.element, 'filtered-public',
        state.filterPublic && !thumbIsPrivate(v.element)));
    }

    static filterByDuration(container = document.body) {
      const {
        filterDurationFrom: from,
        filterDurationTo: to,
        filterDuration,
      } = state;
      // biome-ignore lint/complexity/noForEach: <explanation>
      data.forEach((v) => toggleClass(v.element, 'filtered-duration',
        filterDuration && (v.duration < from || v.duration > to)));
    }

    static runFilters(container) {
      if (state.filterPrivate) filterPrivate(container);
      if (state.filterPublic) filterPublic(container);
      if (state.filterDuration) filterByDuration(container);
    }

    static createThumbsContainer() {
      return GM_addElement('div', { class: 'thumbs-items' });
    }

    static handleLoadedHTML = (htmlPage, mount, useStateContainer = true) => {
      const thumbs = $$('.tumbpu', htmlPage);

      const container = !useStateContainer
        ? createThumbsContainer()
        : containerGlobal;

      for (const thumbElement of thumbs) {
        const url = thumbElement.getAttribute('href');
        if (!url || data.has(url)) {
          thumbElement.remove();
        } else {
          data.set(url, {
            element: thumbElement,
            duration: timeToSeconds($('.thumb > .duration', thumbElement).textContent)
          });
          const img = $('img', thumbElement);
          const privateEl = $('.private', thumbElement);
          if (privateEl) {
            img.src = parseCSSUrl(privateEl.style.background);
            privateEl.style.background = '#000';
          } else {
            img.src = img.getAttribute('data-original');
          }
          img.classList.add('tracking');
          container.appendChild(thumbElement);
        }
      }

      DomManager.runFilters(container);
      mount.before(container);
    };
  }

  class PreviewManager {
    constructor() {
      this.tick = new Tick(ANIMATION_DELAY);
    }

    iteratePreviewImages(src) {
      return src.replace(/(\d)\.jpg$/, (_, n) => `${circularShift(parseInt(n))}.jpg`);
    }

    animatePreview = (e) => {
      const { target: el, type } = e;
      if (el.tagName === 'IMG' && el.classList.contains('tracking')) {
        if (type === 'mouseout') {
          this.tick.stop();
          if (el.getAttribute('orig')) el.src = el.getAttribute('orig');
        }
        if (type === 'mouseover') {
          if (!el.getAttribute('orig')) el.setAttribute('orig', el.src);
          this.tick.start(() => {
            el.src = this.iteratePreviewImages(el.src);
          });
        }
      }
    };

    listen(e) {
      e.addEventListener('mouseout', this.animatePreview);
      e.addEventListener('mouseover', this.animatePreview);
    }
  }

  class PaginationPageManager {
    constructor() {
      this.pagination = $('.pagination');
      this.pagination.style.opacity = 0;
      handleLoadedHTML(document.body, this.pagination);
      previewManager.listen(this.pagination.parentElement);

      this.offsetLast = this.getOffsetLast();
      this.paginationGenerator = this.createNextPageGenerator();
      this.generatorDone = false;

      this.resetScroller();
      this.tick = new Tick(SCROLL_RESET_DELAY);
      this.fixScrollViewPort();

      this.ui = new UI(true);
      this.setPagIndex = (offset) =>
        this.ui.setPagIndex(offset, this.offsetLast);
      this.setPagIndex(this.getCurrentOffset());
    }

    fixScrollViewPort() {
      this.tick.start(() => {
        if (this.generatorDone) this.tick.stop();
        if (isElementInViewport(this.pagination)) this.generatorConsumer();
      });
    }

    async generatorConsumer() {
      const {
        value: { url, offset } = {},
        done,
      } = this.paginationGenerator.next();
      this.generatorDone = done;
      if (!done) {
        const nextPageHTML = await fetchHtml(url);
        const prevScrollPos = document.documentElement.scrollTop;
        handleLoadedHTML(nextPageHTML, this.pagination);
        this.setPagIndex(offset);
        window.scrollTo(0, prevScrollPos);
      }
    }

    getCurrentOffset() {
      return parseInt(window.location.pathname.split(/(\d+\/)$/)[1] || '1');
    }

    getOffsetLast() {
      return parseInt(
        $('.pagination-next').previousElementSibling.firstElementChild
          .textContent,
      );
    }

    createNextPageGenerator() {
      let { origin, pathname, search } = window.location;
      let offset;
      [pathname, offset = '1'] = pathname.split(/(\d+\/)$/);
      offset = parseInt(offset);
      pathname = pathname === '/' ? '/latest-updates/' : pathname;
      const offsetLast = this.getOffsetLast();
      function* nextPageGenerator() {
        for (let c = offset + 1; c <= offsetLast; c++) {
          const url = `${origin}${pathname}${c}/${search}`;
          console.log(url);
          yield { url, offset: c };
        }
      }

      return nextPageGenerator();
    }

    resetScroller = () => {
      this.infiniteScrollTriggered = false;
      window.dispatchEvent(new CustomEvent('scroll'));
    };

    infiniteScroll = () => {
      const inViewport = isElementInViewport(this.pagination);
      if (inViewport === this.infiniteScrollTriggered) return;
      this.infiniteScrollTriggered = inViewport;
      if (inViewport) this.generatorConsumer();
    };
  }

  class Router {
    constructor() {
      this.route();
    }

    route() {
      const { pathname } = window.location;
      if ($('.pagination-next')) {
        this.handlePaginationPage();
      } else if (/\/members\/\d+\/$/.test(pathname)) {
        this.handleMemberPage();
      } else if (/\/tag\//.test(pathname) || /\/?q=.*/.test(pathname)) {
        this.handlePageWithVideosButNoPagination();
      } else if (/\/videos\//.test(pathname)) {
        //this.handlePageWithVideosButNoPagination()
      }
    }

    handlePageWithVideosButNoPagination() {
      const vid = $('.tumbpu');
      if (!vid) return;
      handleLoadedHTML(document.body, vid.parentElement);
      previewManager.listen(vid.parentElement);
      const ui = new UI(false);
    }

    handlePaginationPage() {
      this.paginationManager = new PaginationPageManager();
    }

    handleMemberPage() {
      const privates = $('#list_videos_private_videos_items');
      if (privates) {
        const mistakes = findElementsBetweenIds(
          'list_videos_private_videos_items',
          'list_videos_favourite_videos',
        );
        for (const m of mistakes) privates.appendChild(m);
        handleLoadedHTML(privates, privates, false);
        const ui = new UI(false);
      }

      const favorites = $('#list_videos_favourite_videos');
      if (favorites) {
        const mountTo = favorites.firstElementChild.nextElementSibling;
        handleLoadedHTML(favorites, mountTo, false);
      }

      if (privates || favorites) {
        previewManager.listen((privates || favorites).parentElement);
      }
    }
  }

  class UI {
    templateHTML = (haspag) => `
<div id="tapermonkey-app">
  <div class="subbox">
    <input type="checkbox" id="filterPrivate" name="filterPrivate" ${state.filterPrivate ? 'checked' : ''
      }/>
    <label for="filterPrivate">filter private</label>
    <input type="checkbox" id="filterPublic" name="filterPublic" ${state.filterPublic ? 'checked' : ''
      }/>
    <label for="filterPublic">filter public</label>
    ${haspag ? '<span id="pagIndex">0/0</span>' : ''}
  </div>
  <div class="subbox">
    <input type="checkbox" id="filterl" name="filterl" ${state.filterDuration ? 'checked' : ''
      } />
    <label for="filterl">filter duration seconds</label>

    <input type="number" placeholder="min sec" step="10"
      min="0" max="100000" id="minL" value=${state.filterDurationFrom} />
    <input type="number" placeholder="max sec" step="10"
      min="0" max="100000" id="maxL" value=${state.filterDurationTo} />
  </div>
</div>`;

    constructor(haspag = true) {
      document.body.appendChild(parseDom(this.templateHTML(haspag)));
      this.tapermonkeyAppTemplate = document.querySelector('#tapermonkey-app');
      this.control();
    }

    setPagIndex(index, total) {
      $('#pagIndex').innerText = `${index}/${total}`;
    }

    control() {
      this.tapermonkeyAppTemplate.addEventListener('click', (e) => {
        const { id, checked, value } = e.target;
        if (id === 'filterPublic') {
          state.filterPublic = checked;
          filterPublic();
        }
        if (id === 'filterPrivate') {
          state.filterPrivate = checked;
          filterPrivate();
        }
        if (id === 'filterl') {
          state.filterDuration = checked;
          filterByDuration();
        }
        if (id === 'minL') {
          state.filterDurationFrom = parseIntegerOr(value, state.filterDurationFrom);
          filterByDuration();
        }
        if (id === 'maxL') {
          state.filterDurationTo = parseIntegerOr(value, state.filterDurationTo);
          filterByDuration();
        }
      });
    }
  }

  const state = new ReactiveLocalStorage({
    filterDurationFrom: 0,
    filterDurationTo: 100000,
    filterDuration: false,
    filterPrivate: false,
    filterPublic: false,
    infiniteScrollTriggered: false,
  });

  const data = new Map();
  const { filterPrivate, filterPublic, filterByDuration, handleLoadedHTML, createThumbsContainer, thumbIsPrivate } = DomManager;
  const containerGlobal = createThumbsContainer();
  const previewManager = new PreviewManager();
  const router = new Router();

  const tampermonkeyCSS = `
    #tapermonkey-app {
      background: #151515;
      padding: 10px;
      position: fixed;
      z-index: 9999;
      bottom: 10px;
      right: 10px;
      border-radius: 15px;
      width: max-content;
      box-shadow: 20px 20px 60px #000000, -20px -20px 60px #000000;
    }
    #tapermonkey-app .subbox {
      background: #2c2c2c;
      border-radius: 5px;
      padding: 4px;
      margin: 6px;
    }
    #tapermonkey-app .subbox input[type=number] {
      padding-left: 10px;
      width: 5rem;
      background: #26282b;
    }
    #tapermonkey-app .subbox input[type=checkbox] {
      margin-left: 5px;
    }
    #tapermonkey-app .subbox label {
      user-select: none;
    }
    #pagIndex {
    color: #969696;}
    #tapermonkey-app .subbox * {
      padding-left: 8px;
      float: none;
      width: auto;
      font-family: monospace;
      font-size: 0.8rem;
    }

    .tracking { content-visibility: auto; }
    .filtered-private, .filtered-duration, .filtered-public
            { display: none !important; } `;

  GM_addStyle(tampermonkeyCSS);
})();