Civitai script

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

  1. // ==UserScript==
  2. // @name Civitai script
  3. // @namespace http://tampermonkey-script-exemple
  4. // @version 1.3.1.1
  5. // @description Save information when a page is loaded in a same place, then we download content, generate json with alls informations
  6. // @match https://civitai.com/models/*
  7. // @match https://civitai.com*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_registerMenuCommand
  11. // @author viatana35
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. function addCloseTabOption() {
  16. let closeTab = GM_getValue('closeTab', false);
  17.  
  18. GM_registerMenuCommand('Close Tab', () => {
  19. closeTab = !closeTab;
  20. GM_setValue('closeTab', closeTab);
  21.  
  22. const statusMessage = closeTab ? 'enabled' : 'disabled';
  23. console.log(`Close tab option is now ${statusMessage}.`);
  24. });
  25. console.log("closetab");
  26. return closeTab;
  27. }
  28.  
  29.  
  30. async function findLinks() {
  31. const links = document.querySelectorAll('a');
  32. const results = [];
  33.  
  34. links.forEach(link => {
  35. if (link.classList.contains('mantine-UnstyledButton-root', 'mantine-Button-root') &&
  36. link.type === 'button' &&
  37. link.getAttribute('data-button') === 'true' &&
  38. link.getAttribute('download') === '') {
  39.  
  40. const hrefParts = link.href.split('/');
  41. const lastPart = hrefParts[hrefParts.length - 1];
  42.  
  43. results.push({ href: link.href, id: lastPart });
  44. }
  45. });
  46.  
  47. return results[0];
  48. }
  49.  
  50. async function saveLinks() {
  51. const closeTab = addCloseTabOption();
  52. const currentLinks = await findLinks();
  53. const storedLinksJSON = await GM_getValue('storedLinks', '[]');
  54. const storedLinks = JSON.parse(storedLinksJSON);
  55.  
  56. if (storedLinks.some(link => link.id === currentLinks.id)) {
  57. console.log('Link already stored');
  58. if (closeTab) {
  59. window.close(); // Close the current tab
  60. }
  61. else
  62. {
  63. return;
  64. }
  65. }
  66.  
  67. const newLinks = [...storedLinks, currentLinks];
  68. await GM_setValue('storedLinks', JSON.stringify(newLinks));
  69. if (closeTab) {
  70. window.close(); // Close the current tab
  71. }
  72. }
  73.  
  74. function clickPreview() {
  75. // get all elements with class 'mantine-Alert-root'
  76. const alertElements = document.querySelectorAll('.mantine-Alert-root');
  77.  
  78. //if there are no alert elements, return
  79. if (!alertElements.length) {
  80. return false;
  81. }
  82. else {
  83. // loop through all alert elements
  84. alertElements.forEach(alertElement => {
  85. // find the span element within the alert element
  86. const spanElement = alertElement.querySelector('span');
  87.  
  88. // click on the span element, if found
  89. if (spanElement) {
  90. //if the texte of the span cntain : 'Notify me when it's available.'
  91. if(spanElement.textContent == 'Notify me when it\'s available.'){
  92. spanElement.click();
  93. }
  94. }
  95. });
  96. //make an alert to show that the element has been added to the waitlist )
  97. //alert('element added to waitlist');
  98. return true;
  99. }
  100. }
  101.  
  102.  
  103.  
  104.  
  105. window.addEventListener('load', function() {
  106. if (window.location.href.startsWith('https://civitai.com/models/'))
  107. {
  108. if (!clickPreview())
  109. {
  110. saveLinks();
  111. // Creation of the button to download the stored content
  112. const header = document.querySelector('header');
  113. const downloadButton = document.createElement('button');
  114. downloadButton.className = 'mantine-Button-inner';
  115. downloadButton.textContent = 'Download stored content';
  116. downloadButton.addEventListener('click', async () => {
  117. const storedLinksJSON = await GM_getValue('storedLinks', '[]');
  118. const storedLinks = JSON.parse(storedLinksJSON);
  119. const data = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(storedLinks));
  120. const link = document.createElement('a');
  121. link.setAttribute('href', data);
  122. link.setAttribute('download', 'storedLinks.json');
  123. document.body.appendChild(link);
  124. link.click();
  125. document.body.removeChild(link);
  126. });
  127. header.appendChild(downloadButton);
  128.  
  129. // Creation of the button to clear the stored content
  130. const clearButton = document.createElement('button');
  131. clearButton.className = 'mantine-Button-inner';
  132. clearButton.textContent = 'Erase Stored Content';
  133. clearButton.addEventListener('click', () => {
  134. if (confirm('Are you sure you want to erase stored content?')) {
  135. GM_setValue('storedLinks', '[]');
  136. }
  137. });
  138. header.appendChild(clearButton);
  139.  
  140. // Creation of the button to delete this page
  141. const deletePageButton = document.createElement('button');
  142. deletePageButton.className = 'mantine-Button-inner';
  143. deletePageButton.textContent = 'Delete this page';
  144. deletePageButton.addEventListener('click', async () => {
  145. const currentLinks = await findLinks();
  146. const storedLinksJSON = await GM_getValue('storedLinks', '[]');
  147. const storedLinks = JSON.parse(storedLinksJSON);
  148.  
  149. const index = storedLinks.findIndex(link => link.id === currentLinks.id);
  150. if (index > -1) {
  151. if (confirm('Are you sure you want to delete the stored content of this page?')) {
  152. storedLinks.splice(index, 1);
  153. await GM_setValue('storedLinks', JSON.stringify(storedLinks));
  154. }
  155. } else {
  156. alert('There is no stored content for this page.');
  157. }
  158. });
  159. header.appendChild(deletePageButton);
  160. }
  161. else
  162. {
  163. const closeTab = addCloseTabOption();
  164. if (closeTab) {
  165. window.close(); // Close the current tab
  166. }
  167. }
  168. }
  169. else
  170. {
  171. addCloseTabOption();
  172. }
  173.  
  174. });
  175.  
  176.