Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Improve injectFile, add test

Ondra Urbana3853e2926fe
diff --git a/src/puppeteer_utils.js b/src/puppeteer_utils.js
index d58c98df3..367cb6371 100644
--- a/src/puppeteer_utils.js
+++ b/src/puppeteer_utils.js
@@ -59,9 +59,6 @@ const hideWebDriver = async (page) => {
* Unlike Puppeteer's `addScriptTag` function, this function works on pages
* with arbitrary Cross-Origin Resource Sharing (CORS) policies.
*
- * Make sure that you're injecting the file after the page has loaded (after `page.goto()`). Otherwise,
- * the navigation will override the existing environment and the library will no longer be available.
- *
* @param {Page} page
* Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.
* @param {String} filePath File path
@@ -74,7 +71,7 @@ const injectFile = async (page, filePath) => {
const contents = await readFilePromised(filePath, 'utf8');
- return page.evaluate(contents);
+ return page.evaluateOnNewDocument(contents);
};
/**
@@ -86,10 +83,6 @@ const injectFile = async (page, filePath) => {
* other libraries included by the page that use the same variable name (e.g. another version of jQuery).
* This can affect functionality of page's scripts.
*
- * Also make sure that you're injecting jQuery after the page has loaded
- * (i.e. after [`page.goto()`](https://pptr.dev/#?product=Puppeteer&show=api-pagegotourl-options)).
- * Otherwise, the navigation will override the existing environment and the library will no longer be available.
- *
* **Example usage:**
* ```javascript
* await Apify.utils.puppeteer.injectJQuery(page);
@@ -121,10 +114,6 @@ const injectJQuery = (page) => {
* libraries included by the page that use the same variable name.
* This can affect functionality of page's scripts.
*
- * Also make sure that you're injecting Underscore after the page has loaded
- * (i.e. after [`page.goto()`](https://pptr.dev/#?product=Puppeteer&show=api-pagegotourl-options)).
- * Otherwise, the navigation will override the existing environment and the library will no longer be available.
- *
* **Example usage:**
* ```javascript
* await Apify.utils.puppeteer.injectUnderscore(page);
diff --git a/test/data/inject_file.js b/test/data/inject_file.js
new file mode 100644
index 000000000..f224de246
--- /dev/null
+++ b/test/data/inject_file.js
@@ -0,0 +1,3 @@
+(function inject(global) {
+ global.injectedVariable = 42;
+}(window));
diff --git a/test/puppeteer_utils.js b/test/puppeteer_utils.js
index 4c2c753d7..cf8e17893 100644
--- a/test/puppeteer_utils.js
+++ b/test/puppeteer_utils.js
@@ -1,3 +1,4 @@
+import path from 'path';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import Apify from '../build/index';
@@ -18,6 +19,24 @@ describe('Apify.utils.puppeteer', () => {
log.setLevel(ll);
});
+ it('injectFile()', async () => {
+ const browser = await Apify.launchPuppeteer({ headless: true });
+ try {
+ const page = await browser.newPage();
+ await Apify.utils.puppeteer.injectFile(page, path.join(__dirname, 'data', 'inject_file.js'));
+ // let result = await page.evaluate(() => window.injectedVariable);
+ // expect(result).to.be.eql(42);
+ await page.goto('about:chrome');
+ let result = await page.evaluate(() => window.injectedVariable);
+ expect(result).to.be.eql(42);
+ await page.goto('https://www.example.com');
+ result = await page.evaluate(() => window.injectedVariable);
+ expect(result).to.be.eql(42);
+ } finally {
+ browser.close();
+ }
+ });
+
it('injectJQuery()', async () => {
const browser = await Apify.launchPuppeteer({ headless: true });