Last active
March 12, 2024 05:10
-
-
Save Xeophon/f8504ceef097c0eebe143cd5cbc8dfa3 to your computer and use it in GitHub Desktop.
ChatGPT Personals
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 OpenAI Authorization Header Interceptor | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Intercept OpenAI chat requests, store Authorization header once, and exit | |
// @author Xeophon + ChatGPT | |
// @match https://chat.openai.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
console.log('[Tampermonkey] Script initialized.'); | |
if (localStorage.getItem('Authorization')) { | |
console.log('[Tampermonkey] Authorization header already stored. Exiting.'); | |
return; // Exit if the Authorization header is already stored | |
} | |
// Store a reference to the original fetch function | |
const originalFetch = window.fetch; | |
// Override the fetch function | |
window.fetch = async function(...args) { | |
const [url, requestInit] = args; | |
console.log('[Tampermonkey] Intercepted fetch call to:', url); | |
// Check if the URL matches the desired pattern | |
if (url.includes("https://chat.openai.com/backend-api/")) { | |
console.log('[Tampermonkey] URL matches desired pattern.'); | |
// Create a new Request object from the arguments to extract headers | |
const request = new Request(url, requestInit); | |
// Extract the Authorization header if it exists | |
if (request.headers.has('Authorization')) { | |
const authHeader = request.headers.get('Authorization'); | |
// Store in localStorage | |
localStorage.setItem('Authorization', authHeader); | |
console.log('[Tampermonkey] Authorization header stored in localStorage.'); | |
// Remove the fetch interceptor after storing the header | |
window.fetch = originalFetch; | |
} else { | |
console.log('[Tampermonkey] Authorization header not found in the request.'); | |
} | |
} | |
// Call the original fetch function with the arguments | |
return originalFetch.apply(this, args); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment