motherless | desktop reveal all galleries

desktop reveal all galleries

Stan na 11-04-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         motherless | desktop reveal all galleries
// @namespace    http://tampermonkey.net/
// @license MIT
// @version      2024-03-24
// @description  desktop reveal all galleries
// @author       smartacephale
// @match        https://motherless.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=motherless.com
// @grant        none
// ==/UserScript==

//====================================================================================================

function $(x, e = document.body) { return e.querySelector(x); }
function $$(x, e = document.body) { return e.querySelectorAll(x); }

function parseDOM(html) {
  const parsed = new DOMParser().parseFromString(html, 'text/html').body;
  return parsed.children.length > 1 ? parsed : parsed.firstElementChild;
}

const MOBILE_UA = 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36';

function fetchCustomUA(url, ua = MOBILE_UA) {
  const headers = new Headers({ "User-Agent": ua });
  return fetch(url, { headers });
}

function fetchHtml(url) { return fetchCustomUA(url).then((r) => r.text()).then((h) => parseDOM(h)); }

//====================================================================================================

const GALLERIES_TAG = '.media-related-galleries';
const GALLERY_TAG = '.gallery-container';
const GALLERY_MOB_TAG = '.ml-gallery-thumb';
const GROUP_TAG = '.group-minibio';

function displayAll() {
  // biome-ignore lint/complexity/noForEach: <explanation>
  $$(GROUP_TAG).forEach(e => { e.style = 'display: block !important' });
  // biome-ignore lint/complexity/noForEach: <explanation>
  $$(GALLERY_TAG).forEach(e => { e.style = 'display: block !important' });
}

function cloneAttributes(target, source) {
  // biome-ignore lint/complexity/noForEach: <explanation>
  [...source.attributes].forEach(attr => { target.setAttribute(attr.nodeName === "id" ? 'data-id' : attr.nodeName, attr.nodeValue) })
}

function replaceElementTag(e, tagName) {
  const newTagElement = document.createElement(tagName);
  newTagElement.className = e.className;
  newTagElement.href = e.href;
  newTagElement.style = e.style;
  cloneAttributes(newTagElement, e);
  newTagElement.innerHTML = e.innerHTML;
  e.parentNode.replaceChild(newTagElement, e);
}

function mobileGalleryToDesktop(e) {
  e.firstElementChild.nextElementSibling.nextElementSibling.remove();
  e.firstElementChild.appendChild(e.firstElementChild.nextElementSibling);
  e.className = 'thumb-container gallery-container';
  e.firstElementChild.className = 'desktop-thumb image medium';
  e.firstElementChild.firstElementChild.nextElementSibling.className = 'gallery-captions';
  replaceElementTag(e.firstElementChild.firstElementChild, 'a');
}

async function desktopAddMobGalleries() {
  const galleries = $(GALLERIES_TAG);
  if (galleries != null) {
    const galleriesContainer = galleries.firstElementChild.nextElementSibling.firstElementChild;
    const galleriesCount = galleries.querySelectorAll(GALLERY_TAG).length;
    const mobDom = await fetchHtml(window.location.href);
    const mobGalleries = $$(GALLERY_MOB_TAG, mobDom);
    for (const [i, x] of mobGalleries.entries()) {
      if (i > galleriesCount - 1) {
        mobileGalleryToDesktop(x);
        galleriesContainer.appendChild(x);
      }
    }
    displayAll();
  }
}

//====================================================================================================

(async () => {
  'use strict';
  await desktopAddMobGalleries();
})();