Skip to content

Commit

Permalink
feat: ✨ add puppeteer options
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelo committed Oct 21, 2024
1 parent 3bcbd59 commit 793d860
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ eslint.config.js
LICENSE
tsconfig.json
bin
output.json
output.json
server
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helptheweb/scanner",
"version": "2.3.1",
"version": "2.4.0",
"description": "service to scan websites and get accessibility violations",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
18 changes: 5 additions & 13 deletions src/generateReport.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { AxePuppeteer } from '@axe-core/puppeteer';
import puppeteer from 'puppeteer';

import locateChrome from 'locate-chrome';
import puppeteer, { type LaunchOptions } from 'puppeteer';

// Launches Puppeteer and runs the Axe Reporter for a SINGLE URL
export const generateReport = async (url: string): Promise<any> => {

const executablePath: string = await new Promise(resolve => locateChrome((arg: any) => resolve(arg))) || '';
export const generateReport = async (url: string, puppeteerLaunchOptions?: LaunchOptions): Promise<any> => {

const browser = await puppeteer.launch({
executablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
...puppeteerLaunchOptions
});
const page = await browser.newPage();

Expand All @@ -23,13 +18,10 @@ export const generateReport = async (url: string): Promise<any> => {
}

// If the supplied URL is an array of URLs, parse through it
export const generateMultipleReports = async (urls: string[]): Promise<any[]> => {

const executablePath: string = await new Promise(resolve => locateChrome((arg: any) => resolve(arg))) || '';
export const generateMultipleReports = async (urls: string[], puppeteerLaunchOptions?: LaunchOptions): Promise<any[]> => {

const browser = await puppeteer.launch({
executablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
...puppeteerLaunchOptions
});
const page = await browser.newPage();
let combinedResults = [];
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import { generateReport, generateMultipleReports } from './generateReport';
import { displayResults } from './displayResults';
import { getUrlsFromSitemap } from './getUrlsFromSitemap';
import type { ReportInterface } from './types';
import { type LaunchOptions } from 'puppeteer';

export const scanner = async (url: string): Promise<ReportInterface[]> => {
export const scanner = async (url: string, puppeteerLaunchOptions?: LaunchOptions): Promise<ReportInterface[]> => {

let output: ReportInterface[] = [];

if (url && URL.canParse(url)) {
if (url.includes('.xml')) {
const fullSitemap: string[] = await getUrlsFromSitemap(url);
const reports = await generateMultipleReports(fullSitemap);
const reports = await generateMultipleReports(fullSitemap, puppeteerLaunchOptions);
for (let report of reports) {
const formattedResults: ReportInterface = displayResults(report);
output.push(formattedResults);
}
} else {
const report = await generateReport(url);
const report = await generateReport(url, puppeteerLaunchOptions);
const formattedResults: ReportInterface = displayResults(report);
output.push(formattedResults);
}
Expand Down

0 comments on commit 793d860

Please sign in to comment.