Created
May 9, 2024 00:22
-
-
Save superskirv/ed8d12d5bdf28cfed55de946c31163e6 to your computer and use it in GitHub Desktop.
Adds a drop down menu for all versions on a model page. Good for models that have more than 5 versions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Civitai Add Versions Dropdown | |
// @namespace https://civitai.com/user/superskirv | |
// @version 0.2a | |
// @description Adds versions drop down menu to page. Reload page to see menu. The page has to many dynamic elements and needs to be reloaded once per visit. | |
// @author Super.Skirv and ChatGPT 3.5 | |
// @match https://civitai.com/models/* | |
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Get the HTML code of the current webpage | |
var htmlCode = document.documentElement.outerHTML; | |
// JSON data containing id, modelId, and name | |
var pattern = /{"id":(\d+),"modelId":(\d+),"name":"([^"]+)",/g; | |
// JSON data array to store extracted information | |
var jsonData = []; | |
// Iterate over matches and extract information | |
var match; | |
while ((match = pattern.exec(htmlCode)) !== null) { | |
var id = parseInt(match[1]); | |
var modelId = parseInt(match[2]); | |
var name = match[3]; | |
jsonData.push({"id": id, "modelId": modelId, "name": name}); | |
} | |
// Create dropdown menu | |
var dropdownMenu = document.createElement('select'); | |
dropdownMenu.id = 'myDropdown'; | |
// Populate dropdown menu with options | |
jsonData.forEach(function(data) { | |
var option = document.createElement('option'); | |
option.value = data.id; | |
option.text = data.name; | |
dropdownMenu.appendChild(option); | |
}); | |
// Find the parent element of the target element | |
var targetElement = document.querySelector('.mantine-Stack-root.mantine-mwqi5l'); | |
var parentElement = targetElement.parentElement; | |
// Insert the dropdown menu before the target element | |
parentElement.insertBefore(dropdownMenu, targetElement); | |
// Redirect to corresponding URL when an option is selected | |
dropdownMenu.addEventListener('change', function() { | |
var selectedOption = dropdownMenu.options[dropdownMenu.selectedIndex]; | |
var selectedId = selectedOption.value; | |
// Find the corresponding modelId for the selected option | |
var selectedData = jsonData.find(function(data) { | |
return data.id == selectedId; | |
}); | |
if (selectedData) { | |
var modelId = selectedData.modelId; | |
var id = selectedData.id; | |
// Redirect to the corresponding URL | |
window.location.href = 'https://civitai.com/models/' + modelId + '?modelVersionId=' + id; | |
} | |
}); | |
// Select the first option by default | |
dropdownMenu.selectedIndex = 0; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment