Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add playwright.utils.injectJQuery #1337

Merged
merged 4 commits into from
Apr 28, 2022
Merged

feat: add playwright.utils.injectJQuery #1337

merged 4 commits into from
Apr 28, 2022

Conversation

barjin
Copy link
Contributor

@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
Contributor 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.

test/puppeteer_utils.test.js Outdated Show resolved Hide resolved
@guillim
Copy link

guillim commented Apr 23, 2022

tks for the work, will be useful here :)

import Request from './request'; // eslint-disable-line no-unused-vars

const jqueryPath = require.resolve('jquery');
const readFilePromised = util.promisify(fs.readFile);
Copy link
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 on lines 59 to 61
? Promise.all([page.on('framenavigated', async () => {
await page.evaluate(contents);
}), evalP])
Copy link
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.

};

/**
* Injects the [jQuery](https://jquery.com/) library into a Puppeteer page.
Copy link
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.

*
* **Example usage:**
* ```javascript
* await Apify.utils.puppeteer.injectJQuery(page);
Copy link
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