Skip to content

Commit

Permalink
clients(lr): modify puppeteer connector to work with new tab targets (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Dec 9, 2023
1 parent 59f8456 commit f7d7cde
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions clients/lightrider/lightrider-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,23 @@ async function getPageFromConnection(connection) {
'chrome',
pptrConnection,
[] /* contextIds */,
false /* ignoreHTTPSErrors */,
undefined /* defaultViewport */,
undefined /* process */,
undefined /* closeCallback */,
// @ts-expect-error internal property
targetInfo => targetInfo._targetId === mainTargetInfo.targetId
false /* ignoreHTTPSErrors */
);

const pages = await browser.pages();
const page = pages.find(p => p.mainFrame()._id === frameTree.frame.id);
if (!page) throw new Error('Could not find relevant puppeteer page');

// @ts-expect-error Page has a slightly different type when importing the browser module directly.
// We should be able to find the relevant page instantly, but just in case
// the relevant tab target comes a bit delayed, check every time a new
// target is seen.
const targetPromise = browser.waitForTarget(async (target) => {
const page = await target.page();
if (page && page.mainFrame()._id === frameTree.frame.id) return true;
return false;
});
const page = await Promise.race([
targetPromise.then(target => target.page()),
new Promise((_, reject) => {
setTimeout(() => reject(new Error('Could not find relevant puppeteer page')), 5000);
}),
]);
return page;
}

Expand Down

0 comments on commit f7d7cde

Please sign in to comment.