-
Notifications
You must be signed in to change notification settings - Fork 665
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
Conversation
Puppeteer.injectFile survival fixed
Swapping |
tks for the work, will be useful here :) |
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); |
There was a problem hiding this comment.
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
src/playwright_utils.js
Outdated
? Promise.all([page.on('framenavigated', async () => { | ||
await page.evaluate(contents); | ||
}), evalP]) |
There was a problem hiding this comment.
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.
src/playwright_utils.js
Outdated
}; | ||
|
||
/** | ||
* Injects the [jQuery](https://jquery.com/) library into a Puppeteer page. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Injects the [jQuery](https://jquery.com/) library into a Puppeteer page. | |
* Injects the [jQuery](https://jquery.com/) library into a Playwright page. |
src/playwright_utils.js
Outdated
* | ||
* **Example usage:** | ||
* ```javascript | ||
* await Apify.utils.puppeteer.injectJQuery(page); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* await Apify.utils.puppeteer.injectJQuery(page); | |
* await Apify.utils.playwright.injectJQuery(page); |
playwright.utils.injectJQuery
closes #1336
JQuery requires the global
document
object, which is not available whenPage.addInitScript
-s are added. This solution, therefore, introduces the newinjectFile
optionwaitForDOM
, 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.