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

@ -220,11 +220,10 @@ export default function PreferenceBasedModelSelector() {
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) => {
chrome.tabs.sendMessage(tabs[0].id, { action: 'applyModelSelection', model: defaultModel });
});
// Close the modal after saving
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;
function patchDom() {
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 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) {
btn.setAttribute('aria-label', wantLabel);
}
}
// Observe DOM mutations and reactively patch
const observer = new MutationObserver(patchDom);
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 }) => {
if (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 => {
if (msg.action === 'applyModelSelection' && msg.model) {
desiredModel = msg.model;
// ✅ Only listen for messages from iframe via window.postMessage
window.addEventListener('message', (event) => {
const data = event.data;
if (
typeof data === 'object' &&
data?.action === 'applyModelSelection' &&
typeof data.model === 'string'
) {
desiredModel = data.model;
patchDom();
}
});