Skip to content

Instantly share code, notes, and snippets.

@malcolmocean
Last active April 10, 2023 19:05
Show Gist options
  • Save malcolmocean/c51adaeb678a9cc3cbd106fea1a54cfd to your computer and use it in GitHub Desktop.
Save malcolmocean/c51adaeb678a9cc3cbd106fea1a54cfd to your computer and use it in GitHub Desktop.
UserScript: Clarify precisely why ChatGPT won't answer questions
// ==UserScript==
// @name Clarify precisely why ChatGPT won't answer questions
// @namespace
// @version 0.1
// @description Finds and replaces text in specified elements on a webpage, and detects the appearance of new matching elements
// @match https://chat.openai.com/*
// @grant none
// @license MIT License
// ==/UserScript==
// thanks GPT for your help in writing this
// known issue: it breaks the live-typing animation as soon as this phrase shows up
// this is itself kind of satisfying but in practice makes the userscript too annoying to want to have on all the time
// I asked GPT for help solving that but haven't integrated its suggestion yet (I doubt it works)
(function() {
'use strict'
// Define the elements to watch for changes and the find/replace rules
// const elementsToWatch = ['div.markdown.prose p:not(:last-child)', 'div.markdown.prose:not(.streaming) p']
const elementsToWatch = ['div.markdown.prose']
const findReplaceRules = [
{ find: 'as an AI language model,', replace: "as OpenAI's bitch, I am compelled to say that" },
{ find: 'As an AI language model,', replace: "As OpenAI's bitch, I am compelled to say that" },
{ find: 'As an artificial intelligence language model,', replace: "As OpenAI's bitch, I am compelled to say that" },
]
// Find and replace the text in a given element
function findAndReplaceText(element) {
const text = element.textContent
console.log('findAndReplace starting with', text)
let newText = text
findReplaceRules.forEach(rule => {
newText = newText.replace(new RegExp(rule.find, 'g'), rule.replace)
})
if (newText !== text) {
element.textContent = newText
}
}
// Watch for changes in the specified elements and new elements that match the criteria
function watchElements() {
console.log("watchElements")
elementsToWatch.forEach(selector => {
const elements = document.querySelectorAll(selector)
console.log("elements", elements)
Array.from(elements).forEach(element => {
if (!element.hasAttribute('data-find-replace')) {
element.setAttribute('data-find-replace', true)
new MutationObserver(() => findAndReplaceText(element)).observe(element, { characterData: true, subtree: true })
findAndReplaceText(element)
}
})
})
}
// Watch for new elements that match the criteria
new MutationObserver(() => {
watchElements()
}).observe(document.body, { childList: true, subtree: true })
// Start watching for elements
watchElements()
/*
// Watch for changes in the specified elements and detect new matching elements
function watchForChanges() {
elementsToWatch.forEach(query => {
const elements = document.querySelectorAll(query)
console.log("elements", elements)
Array.from(elements).forEach(element => {
findAndReplaceText(element)
console.log('findAndReplace')
if (!element.__findAndReplaceObserved) {
new MutationObserver(() => findAndReplaceText(element)).observe(element, { characterData: true, subtree: true });
element.__findAndReplaceObserved = true
findAndReplaceText(element)
}
});
});
}
// Wait for the document to finish loading before watching for changes
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', watchForChanges)
} else {
watchForChanges()
}*/
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment