Skip to content

Instantly share code, notes, and snippets.

@rynomad
Last active April 14, 2023 21:06
Show Gist options
  • Save rynomad/082a600270246947971110317020b9a6 to your computer and use it in GitHub Desktop.
Save rynomad/082a600270246947971110317020b9a6 to your computer and use it in GitHub Desktop.
GPT invoker for jasmine test runner for ChatMonkey
// ==UserScript==
// @name jasmine invoker
// @namespace Violentmonkey Scripts
// @match https://chat.openai.com/*
// @grant GM.openInTab
// @grant GM.communicator
// @grant GM.gpt
// @inject-into content
// ==/UserScript==
(async function () {
'use strict';
let testSuite = null;
const {code, bot} = await GM.gpt();
const communicator = GM.communicator()
code.addListener('code', (codeElement) => {
codeElement.addAutomation((language, contents) => {
// console.log("codeElement ready", language, contents)
if (language === 'javascript' && !isJasmineTestSuite(contents)) {
codeElement.addAction('run here', () => runOnUrl(location.href, contents));
codeElement.addAction('run on url', () => {
const url = prompt("Enter URL to run the code on:");
runOnUrl(url, contents);
});
} else if (language === 'javascript' && isJasmineTestSuite(contents)) {
testSuite = contents;
}
});
});
function isUserscript(content) {
return content.includes('// ==UserScript==');
}
function isBookmarklet(content) {
return content.trim().startsWith('javascript:');
}
function isJasmineTestSuite(content) {
return content.includes('describe(') && content.includes('it(');
}
async function runOnUrl(url, content) {
const hash = uuid();
GM.openInTab(url + "#" + hash, {popup: true});
await new Promise((resolve) => setTimeout(resolve, 2000));
let response = await communicator.request(hash, {
code: content,
test: testSuite
});
while (!response.payload.success) {
const errorAsString = JSON.stringify(response.payload, null, 2);
const errorText = `There was an error, please try to fix the file.
error:
${errorAsString}
please output the full corrected file in a code block`;
const { codeElements } = await bot.request(errorText);
const newCode = bot.combineCodeElementsWithClass(['language-javascript'], codeElements);
response = await communicator.request(hash, {
code: newCode,
test: testSuite
});
}
alert('Code passed');
}
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment