plano/demos/use_cases/chatgpt-preference-model-selector/src/scripts/pageFetchOverride.js
Salman Paracha c0748718f1
Salmanap/chrome extension routing (#511)
* first commit of the chatGPT selector

* stashing changes as checkpoint

* pending changes for chrome extension

* commiting a working version

* converting conversation into messages object

* working version of the extension

* working version with fixed styling and better tested

* fixed the issue that the drop down was too small, and fixed the issue where the route was not displayed on the screen

* updating folder with README.md

* fixes for default model, and to update the manifest.json file

* made changes to the dark mode. improved styles

* fix installation bug

* added dark mode

* fixed default model selection

* fixed the scrolling issue

* Update README.md

* updated content.js to update the labels even when default model

* fixed readme

* upddated the title of the packag

* removing the unnecessary permissions

---------

Co-authored-by: Salman Paracha <salmanparacha@MacBook-Pro-329.local>
Co-authored-by: cotran <cotran2@utexas.edu>
Co-authored-by: Shuguang Chen <54548843+nehcgs@users.noreply.github.com>
2025-07-08 16:16:32 -07:00

61 lines
1.8 KiB
JavaScript

(function() {
const TAG = '[ModelSelector][Page]';
console.log(`${TAG} installing fetch override`);
const origFetch = window.fetch;
window.fetch = async function(input, init = {}) {
const urlString = typeof input === 'string' ? input : input.url;
const urlObj = new URL(urlString, window.location.origin);
const pathname = urlObj.pathname;
console.log(`${TAG} fetch →`, pathname);
const method = (init.method || 'GET').toUpperCase();
if (method === 'OPTIONS') {
console.log(`${TAG} OPTIONS request → bypassing completely`);
return origFetch(input, init);
}
// Only intercept conversation fetches
if (pathname === '/backend-api/conversation') {
console.log(`${TAG} matched → proxy via content script`);
const { port1, port2 } = new MessageChannel();
// ✅ Remove non-cloneable properties like 'signal'
const safeInit = { ...init };
delete safeInit.signal;
// Forward the fetch details to the content script
window.postMessage({
type: 'ARCHGW_FETCH',
url: urlString,
init: safeInit
}, '*', [port2]);
// Return a stream response that the content script will fulfill
return new Response(new ReadableStream({
start(controller) {
port1.onmessage = ({ data }) => {
if (data.done) {
controller.close();
port1.close();
} else {
controller.enqueue(new Uint8Array(data.chunk));
}
};
},
cancel() {
port1.close();
}
}), {
headers: { 'Content-Type': 'text/event-stream' }
});
}
// Otherwise, pass through to the original fetch
return origFetch(input, init);
};
console.log(`${TAG} fetch override installed`);
})();