Civitai script

Save information when a page is loaded in a same place, then we download content, generate json with alls informations

Stan na 16-04-2023. 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 Civitai script
// @namespace http://tampermonkey-script-exemple
// @version 1.2
// @description Save information when a page is loaded in a same place, then we download content, generate json with alls informations
// @match https://civitai.com/models/*
// @grant GM_getValue
// @grant GM_setValue
// @author viatana35
// @license MIT
// ==/UserScript==

async function findLinks() {
  const links = document.querySelectorAll('a');
  const results = [];

  links.forEach(link => {
    if (link.classList.contains('mantine-UnstyledButton-root', 'mantine-Button-root') &&
        link.type === 'button' &&
        link.getAttribute('data-button') === 'true' &&
        link.getAttribute('download') === '') {

      const hrefParts = link.href.split('/');
      const lastPart = hrefParts[hrefParts.length - 1];

      results.push({ href: link.href, id: lastPart });
    }
  });

  return results[0];
}

async function saveLinks() {
  const currentLinks = await findLinks();
  const storedLinksJSON = await GM_getValue('storedLinks', '[]');
  const storedLinks = JSON.parse(storedLinksJSON);

  if (storedLinks.some(link => link.id === currentLinks.id)) {
    console.log('Link already stored');
    return;
  }

  const newLinks = [...storedLinks, currentLinks];
  await GM_setValue('storedLinks', JSON.stringify(newLinks));
}

window.addEventListener('load', function() {
  saveLinks();
  
  // Creation of the button to download the stored content
  const header = document.querySelector('header');
  const downloadButton = document.createElement('button');
  downloadButton.className = 'mantine-Button-inner';
  downloadButton.textContent = 'Download stored content';
  downloadButton.addEventListener('click', async () => {
    const storedLinksJSON = await GM_getValue('storedLinks', '[]');
    const storedLinks = JSON.parse(storedLinksJSON);
    const data = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(storedLinks));
    const link = document.createElement('a');
    link.setAttribute('href', data);
    link.setAttribute('download', 'storedLinks.json');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
  header.appendChild(downloadButton);

  // Creation of the button to clear the stored content
  const clearButton = document.createElement('button');
  clearButton.className = 'mantine-Button-inner';
  clearButton.textContent = 'Erase Stored Content';
  clearButton.addEventListener('click', () => {
    if (confirm('Are you sure you want to erase stored content?')) {
      GM_setValue('storedLinks', '[]');
    }
  });
  header.appendChild(clearButton);

 // Creation of the button to delete this page
  const deletePageButton = document.createElement('button');
  deletePageButton.className = 'mantine-Button-inner';
  deletePageButton.textContent = 'Delete this page';
  deletePageButton.addEventListener('click', async () => {
    const currentLinks = await findLinks();
    const storedLinksJSON = await GM_getValue('storedLinks', '[]');
    const storedLinks = JSON.parse(storedLinksJSON);

    const index = storedLinks.findIndex(link => link.id === currentLinks.id);
    if (index > -1) {
      if (confirm('Are you sure you want to delete the stored content of this page?')) {
        storedLinks.splice(index, 1);
        await GM_setValue('storedLinks', JSON.stringify(storedLinks));
      }
    } else {
      alert('There is no stored content for this page.');
    }
  });
  header.appendChild(deletePageButton);

});