feat: add GX.OpenAI.Download script

This commit is contained in:
Glenn Y. Rolland 2023-12-29 17:26:54 +01:00
parent df08423d66
commit 1446ad2752

View file

@ -0,0 +1,83 @@
// ==UserScript==
// @name GX.OpenAI.Download
// @match https://platform.openai.com/playground?mode=chat*
// @version 1.0
// ==/UserScript==
(() => {
const CONSOLE_PREFIX = "GX.OpenAI.Download: ";
const ID_PREFIX = "gxOpenaiDownload-";
//
let bodyElem = document.querySelector("body");
function download(filename, text){
let link = document.createElement("a");
let file = new Blob([text], { type: 'text/plain' });
link.href = URL.createObjectURL(file);
link.download = filename;
link.click();
URL.revokeObjectURL(link.href);
}
function runExtract(){
console.log(CONSOLE_PREFIX + "Begin extract()");
let gyr_extract = function() {
let sysnodes = document.querySelectorAll(".chat-pg-instructions textarea.text-input");
let chatnodes = document.querySelectorAll(".chat-pg-message .text-input-with-focus");
let nodearr = [Array.from(sysnodes), Array.from(chatnodes)].flat();
let nodetxt = nodearr.map(elem => elem.textContent).join("\n\n");
return nodetxt;
}
let nodetxt = gyr_extract();
let nodetitle = nodetxt.split('\n')[0];
console.log(nodetxt);
let date = new Date();
let dateFormatted = date.toISOString().substring(0, 16);
let filename = "openai-playground--"+ nodetitle + "--" + dateFormatted +".txt";
download(filename, nodetxt);
console.log(CONSOLE_PREFIX + "Downloaded " + filename);
console.log(CONSOLE_PREFIX + "End extract()");
}
// .chat-pg-footer > .chat-pg-footer-inner (insertBefore)
function setupTools() {
let padElem = document.createElement('div');
padElem.id = ID_PREFIX + "pad";
padElem.style.position = 'fixed';
padElem.style.zIndex = "9999";
padElem.style.bottom = '0';
padElem.style.right = '0';
padElem.style.width = '100px';
padElem.style.height = '50px';
padElem.style.border = '1px solid black';
padElem.style.backgroundColor = 'rgba(0,0,0,0.5)';
padElem.style.display = "flex";
padElem.style.justifyContent = "center";
padElem.style.alignItems = "center";
let buttonElem = document.createElement("input");
buttonElem.value = "Download";
buttonElem.type = "button";
buttonElem.addEventListener("click", runExtract)
// Insertion des éléments dans le body
padElem.appendChild(buttonElem);
bodyElem.appendChild(padElem);
}
function initialize() {
console.log(CONSOLE_PREFIX + "Begin setup()");
let padElem = document.querySelector('#' + ID_PREFIX + "pad");
if (!padElem) {
setupTools();
}
console.log(CONSOLE_PREFIX + "End setup()");
}
initialize();
})();