Skip to content

Commit 73b2bb6

Browse files
committed
[picker] Improve reliability of element picker launch
Avoid relying on flaky `load` events and arbitrary timeout. Related issue: uBlockOrigin/uBlock-issues#4200
1 parent 68fecbb commit 73b2bb6

3 files changed

Lines changed: 67 additions & 35 deletions

File tree

‎src/js/epicker-ui.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,9 @@ let pickerContentPort;
906906
async function bootPicker(ev) {
907907
const msg = ev.data || {};
908908
if ( msg.what !== 'epickerStart' ) { return; }
909-
const epickerPassword = await vAPI.messaging.send('elementPicker', {
910-
what: 'elementPickerPassword'
911-
});
912-
if ( msg.password !== epickerPassword ) { return; }
909+
const epickerPairing = await epickerPairingPromise;
910+
if ( Boolean(epickerPairing?.password) === false ) { return; }
911+
if ( msg.password !== epickerPairing.password ) { return; }
913912
if ( Array.isArray(ev.ports) === false ) { return; }
914913
if ( ev.ports.length === 0 ) { return; }
915914
pickerContentPort = ev.ports[0];
@@ -926,6 +925,10 @@ async function bootPicker(ev) {
926925

927926
globalThis.addEventListener('message', ev => { bootPicker(ev); }, { once: true });
928927

928+
const epickerPairingPromise = vAPI.messaging.send('elementPicker', {
929+
what: 'elementPickerPairing'
930+
});
931+
929932
/******************************************************************************/
930933

931934
})();

‎src/js/messaging.js‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import µb from './background.js';
6868
/******************************************************************************/
6969
/******************************************************************************/
7070

71+
const browser = globalThis.browser ?? globalThis.chrome;
72+
7173
// Default handler
7274
// privileged
7375

@@ -829,7 +831,21 @@ vAPI.messaging.listen({
829831
{
830832
// >>>>> start of local scope
831833

832-
const epickerPassword = self.crypto.randomUUID();
834+
// Pairing content script with extension page (picker user interface):
835+
// 1. Content script requests pairing
836+
// 2. Main process receives request from content script and stores data
837+
// required to message back content script directly
838+
// 3. Extension page (picker user interface) requests pairing
839+
// 4. Main process receives request from extension page: message back
840+
// pending content script that the extension page is paired
841+
// 5. Content script established direct communication with extension page
842+
// using unique one-time pairing password
843+
// This avoids having to rely on `onload` events to establish communication.
844+
const contextPairing = {
845+
password: '',
846+
tabId: 0,
847+
frameId: 0,
848+
};
833849

834850
const onMessage = function(request, sender, callback) {
835851
// Async
@@ -863,8 +879,20 @@ const onMessage = function(request, sender, callback) {
863879
let response;
864880

865881
switch ( request.what ) {
866-
case 'elementPickerPassword':
867-
response = epickerPassword;
882+
case 'elementPickerPairing':
883+
if ( contextPairing.password === '' ) {
884+
contextPairing.password = self.crypto.randomUUID();
885+
contextPairing.tabId = sender.tabId;
886+
contextPairing.frameId = sender.frameId;
887+
response = { password: contextPairing.password };
888+
} else {
889+
response = { password: contextPairing.password };
890+
browser.tabs.sendMessage(contextPairing.tabId, {
891+
what: 'elementPickerPairing',
892+
password: contextPairing.password,
893+
}, { frameId: contextPairing.frameId });
894+
contextPairing.password = '';
895+
};
868896
break;
869897
case 'elementPickerEprom':
870898
µb.epickerArgs.eprom = request;

‎src/js/scriptlets/epicker.js‎

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ if ( vAPI === null ) { return; }
2929
if ( vAPI.pickerFrame ) { return; }
3030
vAPI.pickerFrame = true;
3131

32-
const pickerUniqueId = self.crypto.randomUUID().replaceAll('-', '').slice(16);
33-
32+
const browser = globalThis.browser ?? globalThis.chrome;
33+
const pickerUniqueId = `z${self.crypto.randomUUID().replaceAll('-', '').slice(16)}`;
3434
const reCosmeticAnchor = /^#(\$|\?|\$\?)?#/;
35-
3635
const netFilterCandidates = [];
3736
const cosmeticFilterCandidates = [];
3837

@@ -1269,13 +1268,13 @@ const pickerCSSStyle = [
12691268

12701269

12711270
const pickerCSS = `
1272-
[${CSS.escape(pickerUniqueId)}] {
1271+
[${pickerUniqueId}] {
12731272
${pickerCSSStyle}
12741273
}
1275-
[${CSS.escape(pickerUniqueId)}][loaded] {
1274+
[${pickerUniqueId}][loaded] {
12761275
visibility: visible !important;
12771276
}
1278-
[${CSS.escape(pickerUniqueId)}][clicked] {
1277+
[${pickerUniqueId}][clicked] {
12791278
pointer-events: none !important;
12801279
}
12811280
`;
@@ -1287,10 +1286,10 @@ let pickerBootArgs;
12871286
let pickerFramePort = null;
12881287

12891288
const bootstrap = async ( ) => {
1290-
const pickerPassword = await vAPI.messaging.send('elementPicker', {
1291-
what: 'elementPickerPassword'
1289+
const pickerPairing = await vAPI.messaging.send('elementPicker', {
1290+
what: 'elementPickerPairing'
12921291
});
1293-
if ( typeof pickerPassword !== 'string' ) { return; }
1292+
if ( Boolean(pickerPairing?.password) === false ) { return; }
12941293
pickerBootArgs = await vAPI.messaging.send('elementPicker', {
12951294
what: 'elementPickerArguments'
12961295
});
@@ -1315,26 +1314,28 @@ const bootstrap = async ( ) => {
13151314
const iframe = document.createElement('iframe');
13161315
iframe.setAttribute(pickerUniqueId, '');
13171316
shadowRoot.append(iframe);
1317+
const onMessage = msg => {
1318+
browser.runtime.onMessage.removeListener(onMessage);
1319+
if ( msg.what !== 'elementPickerPairing' ) { return; }
1320+
if ( msg.password !== pickerPairing.password ) { return; }
1321+
iframe.setAttribute('loaded', '');
1322+
const channel = new MessageChannel();
1323+
pickerFramePort = channel.port1;
1324+
pickerFramePort.onmessage = ev => {
1325+
onDialogMessage(ev.data || {});
1326+
};
1327+
pickerFramePort.onmessageerror = ( ) => {
1328+
quitPicker();
1329+
};
1330+
iframe.contentWindow.postMessage(
1331+
{ what: 'epickerStart', password: pickerPairing.password },
1332+
url.href,
1333+
[ channel.port2 ]
1334+
);
1335+
resolve(iframe);
1336+
};
1337+
browser.runtime.onMessage.addListener(onMessage);
13181338
document.documentElement.append(pickerContainer);
1319-
iframe.addEventListener('load', ( ) => {
1320-
self.setTimeout(( ) => {
1321-
iframe.setAttribute('loaded', '');
1322-
const channel = new MessageChannel();
1323-
pickerFramePort = channel.port1;
1324-
pickerFramePort.onmessage = ev => {
1325-
onDialogMessage(ev.data || {});
1326-
};
1327-
pickerFramePort.onmessageerror = ( ) => {
1328-
quitPicker();
1329-
};
1330-
iframe.contentWindow.postMessage(
1331-
{ what: 'epickerStart', password: pickerPassword },
1332-
url.href,
1333-
[ channel.port2 ]
1334-
);
1335-
resolve(iframe);
1336-
}, 147);
1337-
}, { once: true });
13381339
iframe.contentWindow.location = url.href;
13391340
});
13401341
};

0 commit comments

Comments
 (0)