[mv3] Force real origin in postMessage so that use_dynamic_url can be used

Call to postMessage was failing in the zapper when using `use_dynamic_url` in
manifest. Manually overwriting the per-session dynamic hostname with the real
extension id fixes this.
This commit is contained in:
Raymond Hill 2025-03-31 16:30:17 -04:00
parent 0c6cbb7fcb
commit 6ffffb7b2b
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
2 changed files with 7 additions and 4 deletions

View file

@ -63,7 +63,8 @@
], ],
"matches": [ "matches": [
"<all_urls>" "<all_urls>"
] ],
"use_dynamic_url": true
} }
] ]
} }

View file

@ -365,7 +365,7 @@ const onFrameMessage = function(msg) {
// can remove the iframe. // can remove the iframe.
const bootstrap = async ( ) => { const bootstrap = async ( ) => {
const url = new URL(chrome.runtime.getURL('/zapper-ui.html')); const dynamicURL = new URL(chrome.runtime.getURL('/zapper-ui.html'));
return new Promise(resolve => { return new Promise(resolve => {
const frame = document.createElement('iframe'); const frame = document.createElement('iframe');
frame.setAttribute(zapperSecret, ''); frame.setAttribute(zapperSecret, '');
@ -381,9 +381,11 @@ const bootstrap = async ( ) => {
port.onmessageerror = ( ) => { port.onmessageerror = ( ) => {
quitZapper(); quitZapper();
}; };
const realURL = new URL(dynamicURL);
realURL.hostname = chrome.i18n.getMessage('@@extension_id');
frame.contentWindow.postMessage( frame.contentWindow.postMessage(
{ what: 'zapperStart' }, { what: 'zapperStart' },
url.origin, realURL.origin,
[ channel.port2 ] [ channel.port2 ]
); );
frame.contentWindow.focus(); frame.contentWindow.focus();
@ -392,7 +394,7 @@ const bootstrap = async ( ) => {
zapperFramePort: port, zapperFramePort: port,
}); });
}; };
frame.contentWindow.location = url.href; frame.contentWindow.location = dynamicURL.href;
}); });
}; };