Skip to content

Commit

Permalink
Added functionality to connect to remote Chromium instance
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsSteensma committed Aug 11, 2024
1 parent 1c403a5 commit 5dde49c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Dhalang::Screenshot.get_from_url("https://www.google.com", :jpeg, {navigationTim
Below table lists all possible configuration parameters that can be set:
| Key | Description | Default |
|--------------------|-----------------------------------------------------------------------------------------|---------------------------------|
| browserWebsocketUrl | Websocket url of remote chromium browser to use
| navigationTimeout | Amount of milliseconds until Puppeteer while timeout when navigating to the given page | 10000 |
| printToPDFTimeout | Amount of milliseconds until Puppeteer while timeout when calling Page.printToPDF | 0 (unlimited) |
| navigationWaitForSelector | If set, Dhalang will wait for the specified selector to appear before creating the screenshot or PDF | None |
Expand Down
2 changes: 2 additions & 0 deletions lib/Dhalang/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Dhalang
class Configuration
NODE_MODULES_PATH = Dir.pwd + '/node_modules/'.freeze
USER_OPTIONS = {
browserWebsocketUrl: '',
navigationTimeout: 10000,
printToPDFTimeout: 0, # unlimited
navigationWaitUntil: 'load',
Expand Down Expand Up @@ -48,6 +49,7 @@ class Configuration
private_constant :DEFAULT_JPEG_OPTIONS

private attr_accessor :page_url
private attr_accessor :browser_websocket_url
private attr_accessor :temp_file_path
private attr_accessor :temp_file_extension
private attr_accessor :user_options
Expand Down
18 changes: 13 additions & 5 deletions lib/js/dhalang.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const fs = require('fs')

/**
* @typedef {Object} UserOptions
* @property {string} browserWebsocketUrl - The websocket url of remote Chromium browser to use.
* @property {number} navigationTimeout - Maximum in milliseconds until navigation times out, we use a default of 10 seconds as timeout.
* @property {string} navigationWaitUntil - Determines when the navigation was finished, we wait here until the Window.load event is fired ( meaning all images, stylesheet, etc was loaded ).
* @property {string} navigationWaitForSelector - If set, specifies the selector Puppeteer should wait for to appear before continuing.
Expand Down Expand Up @@ -47,18 +48,25 @@ exports.getConfiguration = function () {

/**
* Launches Puppeteer and returns its instance.
* @param {UserOptions} configuration - The configuration to use.
* @param {Configuration} configuration - The configuration to use.
* @returns {Promise<Object>}
* The launched instance of Puppeteer.
*/
exports.launchPuppeteer = async function (configuration) {
module.paths.push(configuration.puppeteerPath);
const puppeteer = require('puppeteer');
const launchArgs = ['--no-sandbox', '--disable-setuid-sandbox'].concat(configuration.userOptions.chromeOptions).filter((item, index, self) => self.indexOf(item) === index);
return await puppeteer.launch({
args: launchArgs,
headless: configuration.userOptions.isHeadless
});

if (configuration.userOptions['browserWebsocketUrl'] !== "") {
return await puppeteer.connect( {
"browserWSEndpoint": configuration.userOptions.browserWebsocketUrl
})
} else {
return await puppeteer.launch({
args: launchArgs,
headless: configuration.userOptions.isHeadless
});
}
}

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/js/html-scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const scrapeHtml = async () => {
const configuration = dhalang.getConfiguration();

let browser;
let page;
try {
browser = await dhalang.launchPuppeteer(configuration);
const page = await browser.newPage();
page = await browser.newPage();
await dhalang.configure(page, configuration.userOptions);
await dhalang.navigate(page, configuration);
const html = await page.content();
Expand All @@ -17,8 +18,10 @@ const scrapeHtml = async () => {
console.error(error.message);
process.exit(1);
} finally {
if (browser) {
if (browser && configuration.userOptions['browserWebsocketUrl'] === "") {
browser.close();
} else {
page.close();
}
process.exit(0);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/js/pdf-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const createPdf = async () => {
const configuration = dhalang.getConfiguration();

let browser;
let page;
try {
browser = await dhalang.launchPuppeteer(configuration);
const page = await browser.newPage();
page = await browser.newPage();
await dhalang.configure(page, configuration.userOptions);
await dhalang.navigate(page, configuration);
const pdfOptions = await dhalang.getConfiguredPdfOptions(page, configuration);
Expand All @@ -21,8 +22,10 @@ const createPdf = async () => {
console.error(error.message);
process.exit(1);
} finally {
if (browser) {
if (browser && configuration.userOptions['browserWebsocketUrl'] === "") {
browser.close();
} else {
page.close();
}
process.exit();
}
Expand Down
7 changes: 5 additions & 2 deletions lib/js/screenshot-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const createScreenshot = async () => {
const configuration = dhalang.getConfiguration();

let browser;
let page;
try {
browser = await dhalang.launchPuppeteer(configuration);
const page = await browser.newPage();
page = await browser.newPage();
await dhalang.configure(page, configuration.userOptions);
await dhalang.navigate(page, configuration);

Expand All @@ -23,8 +24,10 @@ const createScreenshot = async () => {
console.error(error.message);
process.exit(1);
} finally {
if (browser) {
if (browser && configuration.userOptions['browserWebsocketUrl'] === "") {
browser.close();
} else {
page.close();
}
process.exit();
}
Expand Down

0 comments on commit 5dde49c

Please sign in to comment.