Skip to content

Commit b29af21

Browse files
Salman ParachaSalman Paracha
authored andcommitted
removing the tabs permission and reverting to window.postMessage
1 parent 5ea51c8 commit b29af21

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

demos/use_cases/chatgpt-preference-model-selector/public/manifest.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"manifest_version": 3,
3-
"name": "RouteGPT (beta)",
4-
"version": "0.1.0",
3+
"name": "RouteGPT",
4+
"version": "0.1.1",
55
"description": "RouteGPT: Smart Model Routing for ChatGPT.",
66
"permissions": [
7-
"storage",
8-
"tabs"
7+
"storage"
98
],
109
"host_permissions": [
1110
"https://chatgpt.com/*",

demos/use_cases/chatgpt-preference-model-selector/src/components/PreferenceBasedModelSelector.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,10 @@ export default function PreferenceBasedModelSelector() {
220220
console.log('[PBMS] Saved tuples:', tuples);
221221
}
222222
});
223+
// Send message to background script to apply the default model
224+
window.parent.postMessage({ action: 'applyModelSelection', model: defaultModel }, "*");
223225

224-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
225-
chrome.tabs.sendMessage(tabs[0].id, { action: 'applyModelSelection', model: defaultModel });
226-
});
227-
226+
// Close the modal after saving
228227
window.parent.postMessage({ action: 'CLOSE_PBMS_MODAL' }, '*');
229228
};
230229

demos/use_cases/chatgpt-preference-model-selector/src/scripts/content.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ Based on your analysis, provide your response in the following JSON formats if y
311311
});
312312

313313
let desiredModel = null;
314+
314315
function patchDom() {
315316
if (!desiredModel) return;
316317

@@ -320,27 +321,42 @@ Based on your analysis, provide your response in the following JSON formats if y
320321
const span = btn.querySelector('div > span');
321322
const wantLabel = `Model selector, current model is ${desiredModel}`;
322323

323-
if (span && span.textContent !== desiredModel) span.textContent = desiredModel;
324+
if (span && span.textContent !== desiredModel) {
325+
span.textContent = desiredModel;
326+
}
327+
324328
if (btn.getAttribute('aria-label') !== wantLabel) {
325329
btn.setAttribute('aria-label', wantLabel);
326330
}
327331
}
328332

333+
// Observe DOM mutations and reactively patch
329334
const observer = new MutationObserver(patchDom);
330335
observer.observe(document.body || document.documentElement, {
331-
subtree: true, childList: true, characterData: true, attributes: true
336+
subtree: true,
337+
childList: true,
338+
characterData: true,
339+
attributes: true
332340
});
333341

342+
// Set initial model from storage (optional default)
334343
chrome.storage.sync.get(['defaultModel'], ({ defaultModel }) => {
335344
if (defaultModel) {
336345
desiredModel = defaultModel;
337346
patchDom();
338347
}
339348
});
340349

341-
chrome.runtime.onMessage.addListener(msg => {
342-
if (msg.action === 'applyModelSelection' && msg.model) {
343-
desiredModel = msg.model;
350+
// ✅ Only listen for messages from iframe via window.postMessage
351+
window.addEventListener('message', (event) => {
352+
const data = event.data;
353+
if (
354+
typeof data === 'object' &&
355+
data?.action === 'applyModelSelection' &&
356+
typeof data.model === 'string'
357+
) {
358+
359+
desiredModel = data.model;
344360
patchDom();
345361
}
346362
});

0 commit comments

Comments
 (0)