latestBGonStripChat

Enable users to seamlessly access and appreciate the latest backgrounds on SC.

Verzia zo dňa 18.09.2023. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         latestBGonStripChat
// @namespace    http://carllx.com/
// @version      0.1.3.1
// @description  Enable users to seamlessly access and appreciate the latest backgrounds on SC.
// @author       carllx
// @include        https://stripchat.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=stripchat.com
// @grant        none
// ==/UserScript==

(function () {
  "use strict";
  function replaceTimestamp(url) {
    const timestampRegex = /\d+(?=\/)/;
    const newTimestamp = Math.floor(Date.now() / 1000) - 19;
    return url.replace(timestampRegex, newTimestamp);
  }

  function updateSrcInHTML() {
    const users = [];
    console.error(`users.length:`);
    const aWrapperElements = document.querySelectorAll(".model-list-item-link");
    const viewportWidth = window.innerWidth;
    const viewportHeight = window.innerHeight;
    aWrapperElements.forEach((wrapElement) => {
      const rect = wrapElement.getBoundingClientRect();
      console.log();

      const isInView =
        rect.width > 0 &&
        rect.height > 0 &&
        rect.top >= 0 &&
        rect.bottom <= viewportHeight &&
        rect.left >= 0 &&
        rect.right <= viewportWidth;
      if (isInView) {
        const imgElement = wrapElement.querySelector(
          ".image-background__image"
        );
        const src = imgElement.getAttribute("src");

        const usernamePath = wrapElement.getAttribute("href"); //'/MiaBakker'
        // XXXXXXXXXXXX
        // const newSrc = replaceTimestamp(src);
        // imgElement.setAttribute("src", newSrc);
        // XXXXXXXXXXXX

        // XXXXXXXXXXXX
        users.push({ n: usernamePath, el: imgElement });
        // XXXXXXXXXXXX
      }
    });
    // XXXXXXXXXXXX
    console.log(`users.length:${users.length}`);
    getUserofCam(users);
    // XXXXXXXXXXXX
  }
  function executeFunctionEvery5Seconds(fn) {
    const intervalId = setInterval(fn, 3000);
    const eventListener = () => {
      clearInterval(intervalId);
      document.removeEventListener("stop", eventListener);
      // Additional code to handle the 'stop' event if needed
    };
    document.addEventListener("stop", eventListener);
  }
  //   return fetch(`https://stripchat.com/api/front/v2/models/username${n}/cam`)

  function getUserofCam(users) {
    //  users : [ {n:, el:},]

    const fetchUserData = async (user) => {
      const { n, el } = user;
      const resolvedEl = await Promise.resolve(el);

      return fetch(`https://stripchat.com/api/front/v2/models/username${n}/cam`)
        .then((res) => {
          if (res.ok) {
            return res.json();
          }
          throw new Error("Something went wrong");
        })
        .then((ob) => {
          const {snapshotTimestamp, favoritedCount }= ob.user.user.snapshotTimestamp;
          //   console.log(snapshotTimestamp);
          const url = resolvedEl.getAttribute("src");
          const timestampRegex = /\d+(?=\/)/;

          const newSrc = url.replace(timestampRegex, snapshotTimestamp);
          resolvedEl.setAttribute("src", newSrc);

          resolvedEl.onerror = function () {
            this.onerror = null;
            this.src = this.src;
            this.classList.remove('image-background__image--is-hidden')
          };
          return true;
        });
    };

    Promise.all(users.map(fetchUserData)).then((results) => {
      console.log("ok"); // log name property of first user
    });
  }

  executeFunctionEvery5Seconds(updateSrcInHTML);

  // Your code here...
})();