// ==UserScript==
// @name ST QR Collection Importer
// @namespace Violentmonkey Scripts
// @match http://127.0.0.1:8000/*
// @grant none
// @version 1.0
// @author creamy
// @license MIT
// @description SillyTavern QuickReply preset collection importer.
// @downloadURL https://update.greasyfork.icu/scripts/523732/ST%20QR%20Collection%20Importer.user.js
// @updateURL https://update.greasyfork.icu/scripts/523732/ST%20QR%20Collection%20Importer.meta.js
// ==/UserScript==
(function() {
"use strict";
window.addEventListener("load", init);
function init() {
//main cont
const main = document.createElement("div");
main.setAttribute("id", "main");
main.setAttribute("class", "popup");
Object.assign(main.style, {
width: "auto",
height: "auto",
margin: "auto",
padding: "5px",
position: "absolute",
top: "5px",
right: "5px",
zIndex: "99999",
opacity: "70%"
})
const fALink = document.createElement("link");
fALink.setAttribute("rel", "stylesheet");
fALink.setAttribute("href", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css");
main.appendChild(fALink);
//logo button
const logo = document.createElement("div");
logo.innerHTML = '';
logo.setAttribute("id", "showbtn");
logo.setAttribute("class", "menu_button");
Object.assign(logo.style, {
fontSize: "1.5em",
float: "right"
})
//Update presets title
const updateLabel = document.createElement("div");
updateLabel.innerText = "Import QR preset collection";
Object.assign(updateLabel.style, {
textAlign: "center",
fontSize: "1.2em",
width: "200px",
display: "none"
})
//logo title container
const logoTitleCont = document.createElement("div");
logoTitleCont.appendChild(updateLabel);
logoTitleCont.appendChild(logo);
Object.assign(logoTitleCont.style, {
padding: "2px",
display: "flex",
gap: "5px",
alignItems: "center"
})
//input
const inputCol = document.createElement("input");
inputCol.setAttribute("id", "inputCol");
inputCol.setAttribute("name", "inputCol");
inputCol.setAttribute("type", "text");
inputCol.setAttribute("class", "text-pole")
Object.assign(inputCol.style, {
display: "none",
backgroundColor: "#3a7a41"
})
//button
const button = document.createElement("button");
button.setAttribute("id", "updatebtn");
button.setAttribute("type", "button");
button.setAttribute("class", "menu_button");
button.innerText = "Import";
Object.assign(button.style, {
display: "none",
})
//append to document
main.appendChild(logoTitleCont);
main.appendChild(inputCol);
main.appendChild(button);
document.querySelector("body").appendChild(main);
logo.addEventListener("click", function() {
if(updateLabel.style.display === "none") {
updateLabel.style.display = "block";
inputCol.style.display = "block";
button.style.display = "flex";
logo.innerHTML = '';
}
else {
updateLabel.style.display = "none";
inputCol.style.display = "none";
button.style.display = "none";
logo.innerHTML = '';
}
});
button.addEventListener("click", async function handleImportBtnClick() {
toastr.info("Importing... Please wait.");
const presets = await getPresets(inputCol.value)
importQRS(presets.map((preset) => preset.link))
});
}
const noCacheParam = `${Date.now()}_${Math.floor(Math.random() * 1000000)}`;
async function getPresets(inputLink) {
return new Promise(async (resolve, reject) => {
const corsProxyUrl = `https://corsproxy.io/?url=${inputLink}?nocache=${noCacheParam}`
const response = await fetch(corsProxyUrl)
if(response.status === 200) {
try {
const presetsFromLink = await response.json()
resolve([...presetsFromLink])
}
catch {
toastr.error("Import failed.")
}
}
else {
console.error(`Error with the link: ${inputLink}`)
toastr.error(`Error with the link: ${inputLink}`)
}
})
}
//Credits: rentry.org/stscript
async function importQRS(QR_JSON_URLS) {
//put your json urls here
//const QR_JSON_URLS = [];
/**
* DO NOT FUCK AROUND WITH THE STUFF BELOW
* UNLESS YOU KNOW WHAT YOU ARE DOING
*/
/**
* Loads SillyTavern QuickReply API instance
*
* @returns {Promise}
*/
const loadQrApi = async () => {
const { quickReplyApi } = await import('./scripts/extensions/quick-reply/index.js');
return quickReplyApi;
}
/**
* Fetches JSON object from a given URL
*
* @param {string} url - The URL to get the JSON from
*
* @returns {Promise