removing the tabs permission and reverting to window.postMessage (#524)

Co-authored-by: Salman Paracha <salmanparacha@MacBook-Pro-329.local>
This commit is contained in:
Salman Paracha 2025-07-11 12:07:32 -07:00 committed by GitHub
parent 5ea51c8cbd
commit 4e2355965b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 13 deletions

View file

@ -1,11 +1,10 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "RouteGPT (beta)", "name": "RouteGPT",
"version": "0.1.0", "version": "0.1.1",
"description": "RouteGPT: Smart Model Routing for ChatGPT.", "description": "RouteGPT: Smart Model Routing for ChatGPT.",
"permissions": [ "permissions": [
"storage", "storage"
"tabs"
], ],
"host_permissions": [ "host_permissions": [
"https://chatgpt.com/*", "https://chatgpt.com/*",

View file

@ -220,11 +220,10 @@ export default function PreferenceBasedModelSelector() {
console.log('[PBMS] Saved tuples:', tuples); console.log('[PBMS] Saved tuples:', tuples);
} }
}); });
// Send message to background script to apply the default model
window.parent.postMessage({ action: 'applyModelSelection', model: defaultModel }, "*");
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { // Close the modal after saving
chrome.tabs.sendMessage(tabs[0].id, { action: 'applyModelSelection', model: defaultModel });
});
window.parent.postMessage({ action: 'CLOSE_PBMS_MODAL' }, '*'); window.parent.postMessage({ action: 'CLOSE_PBMS_MODAL' }, '*');
}; };

View file

@ -311,6 +311,7 @@ Based on your analysis, provide your response in the following JSON formats if y
}); });
let desiredModel = null; let desiredModel = null;
function patchDom() { function patchDom() {
if (!desiredModel) return; if (!desiredModel) return;
@ -320,17 +321,25 @@ Based on your analysis, provide your response in the following JSON formats if y
const span = btn.querySelector('div > span'); const span = btn.querySelector('div > span');
const wantLabel = `Model selector, current model is ${desiredModel}`; const wantLabel = `Model selector, current model is ${desiredModel}`;
if (span && span.textContent !== desiredModel) span.textContent = desiredModel; if (span && span.textContent !== desiredModel) {
span.textContent = desiredModel;
}
if (btn.getAttribute('aria-label') !== wantLabel) { if (btn.getAttribute('aria-label') !== wantLabel) {
btn.setAttribute('aria-label', wantLabel); btn.setAttribute('aria-label', wantLabel);
} }
} }
// Observe DOM mutations and reactively patch
const observer = new MutationObserver(patchDom); const observer = new MutationObserver(patchDom);
observer.observe(document.body || document.documentElement, { observer.observe(document.body || document.documentElement, {
subtree: true, childList: true, characterData: true, attributes: true subtree: true,
childList: true,
characterData: true,
attributes: true
}); });
// Set initial model from storage (optional default)
chrome.storage.sync.get(['defaultModel'], ({ defaultModel }) => { chrome.storage.sync.get(['defaultModel'], ({ defaultModel }) => {
if (defaultModel) { if (defaultModel) {
desiredModel = defaultModel; desiredModel = defaultModel;
@ -338,9 +347,16 @@ Based on your analysis, provide your response in the following JSON formats if y
} }
}); });
chrome.runtime.onMessage.addListener(msg => { // ✅ Only listen for messages from iframe via window.postMessage
if (msg.action === 'applyModelSelection' && msg.model) { window.addEventListener('message', (event) => {
desiredModel = msg.model; const data = event.data;
if (
typeof data === 'object' &&
data?.action === 'applyModelSelection' &&
typeof data.model === 'string'
) {
desiredModel = data.model;
patchDom(); patchDom();
} }
}); });