Skip to content

feat: add playwright.utils.injectJQuery#1337

Merged
B4nan merged 4 commits intoapify:masterfrom
barjin:feat/plwtJquery
Apr 28, 2022
Merged

feat: add playwright.utils.injectJQuery#1337
B4nan merged 4 commits intoapify:masterfrom
barjin:feat/plwtJquery

Conversation

@barjin
Copy link
Copy Markdown
Member

@barjin barjin commented Apr 20, 2022

closes #1336

JQuery requires the global document object, which is not available when Page.addInitScript-s are added. This solution, therefore, introduces the new injectFile option waitForDOM, which ensures the script does not get injected until the DOM load.

The implementation of waitForDOM needs rework. Loading bigger scripts (as JQuery, for instance) takes a longer time and introduces "race condition," as there is no way of waiting for the script load to finish. This makes testing + usage a guessing game.

@barjin barjin marked this pull request as ready for review April 23, 2022 11:38
@barjin
Copy link
Copy Markdown
Member Author

barjin commented Apr 23, 2022

Swapping addInitScript/evaluateOnNewDocument for frameNavigated listener in injectFile seems to work. The waitForDOM option mentioned before is not implemented after all.

Comment thread test/puppeteer_utils.test.js Outdated
@guillim
Copy link
Copy Markdown

guillim commented Apr 23, 2022

tks for the work, will be useful here :)

Comment thread src/playwright_utils.js Outdated
import Request from './request'; // eslint-disable-line no-unused-vars

const jqueryPath = require.resolve('jquery');
const readFilePromised = util.promisify(fs.readFile);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's fs.promises now. no need to promisify anymore

Comment thread src/playwright_utils.js Outdated
Comment on lines +59 to +61
? Promise.all([page.on('framenavigated', async () => {
await page.evaluate(contents);
}), evalP])
Copy link
Copy Markdown
Member

@mnmkng mnmkng Apr 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Playwright have some special async handling for the page.on listener? If not, this will cause an unhandled rejection if the evaluate throws. Plus AFAIK page.on does not return a promise, so the Promise.all is redundant.

Comment thread src/playwright_utils.js Outdated
};

/**
* Injects the [jQuery](https://jquery.com/) library into a Puppeteer page.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Injects the [jQuery](https://jquery.com/) library into a Puppeteer page.
* Injects the [jQuery](https://jquery.com/) library into a Playwright page.

Comment thread src/playwright_utils.js Outdated
*
* **Example usage:**
* ```javascript
* await Apify.utils.puppeteer.injectJQuery(page);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* await Apify.utils.puppeteer.injectJQuery(page);
* await Apify.utils.playwright.injectJQuery(page);

@B4nan B4nan changed the title feat: Playwright.injectJQuery feat: add playwright.utils.injectJQuery Apr 28, 2022
@B4nan B4nan merged commit 133de35 into apify:master Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inject JQuery for Playwright

4 participants