Injectable client JS
Inject and use JavaScript libraries in the pages you visit
Preloaded injectables deprecation
Starting from web-node, injectables are not preloaded on disk anymore, please use CDN or external URLs only if you want to inject 3rd party scripts in the page.
Injectable modules
These modules come preloaded on all agent disks. They can be injected in visited pages for easier DOM manipulation (mainly for scraping and automation purposes).
Use Puppeteer's page.addScriptTag() to add them in the current web page, then call Puppeteer's page.evaluate() to use them inside the page.
../injectables/jquery-2.2.3.min.js
— jQuery, 84kB../injectables/jquery-3.0.0.min.js
— jQuery, 85kB../injectables/underscore-1.8.3.min.js
— Underscore, 17kB../injectables/lodash-core-4.13.1.min.js
— Lodash, core build, 12kB../injectables/lodash-full-4.13.1.min.js
— Lodash, full build, 67kB
The modules below are only available in Package 5 and above:
../injectables/jquery-3.3.1.min.js
— jQuery, 85kB../injectables/lodash-core-4.17.5.min.js
— Lodash, core build, 13kB../injectables/lodash-full-4.17.5.min.js
— Lodash, full build, 72kB../injectables/psl-1.1.24.min.js
— Public Suffix List, 119kB../injectables/moment-2.21.0.min.js
— Moment.js, 51kB../injectables/moment-with-locales-2.21.0.min.js
— Moment.js, 315kB../injectables/moment-timezone-0.5.14-2017c.min.js
— Moment.js Timezone, 6kB../injectables/moment-timezone-with-data-0.5.14-2017c.min.js
— Moment.js Timezone, 181kB
For example, to use jQuery in a page that doesn't include it already, you could do this using Puppeteer:
const browser = await puppeteer.launch({
// This is needed to run Puppeteer in a Phantombuster container
args: ["--no-sandbox"]
})
const page = await browser.newPage()
await page.addScriptTag({ path: "../injectables/jquery-3.0.0.min.js" })
You can also inject arbitrary scripts from any URL, for example from CDNs or your own server.
For example if you want to manipulate time and date strings from within the page, this will work perfectly fine:
const browser = await puppeteer.launch({
// This is needed to run Puppeteer in a Phantombuster container
args: ["--no-sandbox"]
})
const page = await browser.newPage()
await page.addScriptTag({
url: "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"
})
JQuery example
Here is a variant of our Developer quick start's example that uses JQuery:
"phantombuster command: nodejs"
"phantombuster package: 5"
"phantombuster flags: save-folder"
const Buster = require("phantombuster")
const puppeteer = require("puppeteer")
const buster = new Buster()
;(async () => {
const browser = await puppeteer.launch({
// This is needed to run Puppeteer in a Phantombuster container
args: ["--no-sandbox"]
})
const page = await browser.newPage()
await page.goto("https://news.ycombinator.com")
await page.waitForSelector("#hnmain")
// we inject JQuery into the page
await page.addScriptTag({ path: "../injectables/jquery-3.0.0.min.js" })
const hackerNewsLinks = await page.evaluate(() => {
const data = []
// and enjoy all its features
$.each(
$("a.storylink"),
(index, element) => {
data.push({
title: element.text,
url: element.href,
})
}
)
return data
})
await buster.setResultObject(hackerNewsLinks)
await page.close()
await browser.close()
process.exit()
})()
Updated about 2 years ago