Skip to content

Commit

Permalink
refactor: extract the script injection hack to puppeteer-utils
Browse files Browse the repository at this point in the history
- introduce a new util module `puppetter-utils`
- the mode contains an `addScripts` function to inject script paths to a browser page,
  annotating the inserted script elements with the `data-ace` attribute.
- refactor `checker-chromium` to inject the scripts using this new method. The scripts
  inserted by Ace are now defined in a single array, easier to read and configure.
  • Loading branch information
rdeltour committed Oct 28, 2017
1 parent fb91a69 commit 708a803
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 64 deletions.
75 changes: 11 additions & 64 deletions src/checker/checker-chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,26 @@ const fs = require('fs');
const path = require('path');
const pMap = require('p-map');
const puppeteer = require('puppeteer');
const axe2ace = require('../report/axe2ace.js');
const winston = require('winston');

const PATH_TO_AXE = path.join(path.dirname(require.resolve('axe-core')), 'axe.min.js');
if (!fs.existsSync(PATH_TO_AXE)) {
winston.verbose(PATH_TO_AXE);
throw new Error('Can’t find aXe');
}

const PATH_TO_H5O = path.join(path.dirname(require.resolve('h5o')), 'dist/outliner.min.js');
if (!fs.existsSync(PATH_TO_H5O)) {
winston.verbose(PATH_TO_H5O);
throw new Error('Can’t find h5o');
}

const PATH_TO_AXE_PATCH_GETSELECTOR = path.join(__dirname, '../scripts/axe-patch-getselector.js');
if (!fs.existsSync(PATH_TO_AXE_PATCH_GETSELECTOR)) {
winston.verbose(PATH_TO_AXE_PATCH_GETSELECTOR);
throw new Error('Can’t find axe-patch-getselector script');
}

const PATH_TO_AXE_PATCH_ARIALOOKUPTABLE = path.join(__dirname, '../scripts/axe-patch-arialookuptable.js');
if (!fs.existsSync(PATH_TO_AXE_PATCH_ARIALOOKUPTABLE)) {
winston.verbose(PATH_TO_AXE_PATCH_ARIALOOKUPTABLE);
throw new Error('Can’t find axe-patch-arialookuptable script');
}

const PATH_TO_ACE_AXE = path.join(__dirname, '../scripts/ace-axe.js');
if (!fs.existsSync(PATH_TO_ACE_AXE)) {
winston.verbose(PATH_TO_ACE_AXE);
throw new Error('Can’t find ace-axe script');
}
const axe2ace = require('../report/axe2ace.js');
const utils = require('./puppeteer-utils');

const PATH_TO_ACE_EXTRACTION = path.join(__dirname, '../scripts/ace-extraction.js');
if (!fs.existsSync(PATH_TO_ACE_EXTRACTION)) {
winston.verbose(PATH_TO_ACE_EXTRACTION);
throw new Error('Can’t find ace-extraction script');
}
const scripts = [
path.resolve(require.resolve('axe-core'), '../axe.min.js'),
path.resolve(require.resolve('h5o'), '../dist/outliner.min.js'),
require.resolve('../scripts/axe-patch-getselector.js'),
require.resolve('../scripts/axe-patch-arialookuptable.js'),
require.resolve('../scripts/ace-axe.js'),
require.resolve('../scripts/ace-extraction.js'),
];

async function checkSingle(spineItem, epub, browser) {
winston.verbose(`- Processing ${spineItem.relpath}`);
try {
const page = await browser.newPage();
await page.goto(spineItem.url);
// page.on('console', msg => console.log(msg.text));

// BEGIN HACK
// Used to differentiate original `script` elements from the one
// added by Puppeteer.
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach(script => script.setAttribute('data-ace-orig', ''));
});
// END HACK
await page.addScriptTag({ path: PATH_TO_AXE });
await page.addScriptTag({ path: PATH_TO_AXE_PATCH_GETSELECTOR });
await page.addScriptTag({ path: PATH_TO_AXE_PATCH_ARIALOOKUPTABLE });
await page.addScriptTag({ path: PATH_TO_H5O });
await page.addScriptTag({ path: PATH_TO_ACE_AXE });
await page.addScriptTag({ path: PATH_TO_ACE_EXTRACTION });
// BEGIN HACK
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach(script => {
if (script.hasAttribute('data-ace-orig')) {
script.removeAttribute('data-ace-orig');
} else {
script.setAttribute('data-ace', '');
}
});
});
// END HACK

await utils.addScripts(scripts, page);

const results = await page.evaluate(() => new Promise((resolve, reject) => {
/* eslint-disable */
Expand Down
32 changes: 32 additions & 0 deletions src/checker/puppeteer-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

async function addScripts(paths, page) {
// BEGIN HACK
// Used to differentiate original `script` elements from the one
// added by Puppeteer.
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach(script => script.setAttribute('data-ace-orig', ''));
});
/* eslint-disable no-restricted-syntax, no-await-in-loop */
for (const path of paths) {
await page.addScriptTag({ path });
}
/* eslint-enable no-restricted-syntax, no-await-in-loop */
// BEGIN HACK
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach((script) => {
if (script.hasAttribute('data-ace-orig')) {
script.removeAttribute('data-ace-orig');
} else {
script.setAttribute('data-ace', '');
}
});
});
// END HACK
}

module.exports = {
addScripts,
};

0 comments on commit 708a803

Please sign in to comment.