mirror of
https://github.com/astefanutti/decktape.git
synced 2024-12-12 13:49:13 +03:00
Manipulate CSS rules to force screen CSS media type
This commit is contained in:
parent
ee7559cb48
commit
3d6b5afeff
75
decktape.js
75
decktape.js
@ -1,6 +1,8 @@
|
|||||||
const fs = require('fs'),
|
const fs = require('fs'),
|
||||||
os = require('os'),
|
hummus = require('hummus'),
|
||||||
parser = require('./libs/nomnom');
|
os = require('os'),
|
||||||
|
parser = require('./libs/nomnom'),
|
||||||
|
puppeteer = require('puppeteer');
|
||||||
|
|
||||||
const plugins = loadAvailablePlugins('./plugins/');
|
const plugins = loadAvailablePlugins('./plugins/');
|
||||||
|
|
||||||
@ -120,11 +122,9 @@ var options = parser.parse(process.argv.slice(2));
|
|||||||
|
|
||||||
(async() => {
|
(async() => {
|
||||||
|
|
||||||
const puppeteer = require('puppeteer');
|
// TODO: support passing args to the the Chromium instance
|
||||||
const browser = await puppeteer.launch();
|
const browser = await puppeteer.launch({ headless: true });
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
|
||||||
const hummus = require('hummus');
|
|
||||||
const pdfWriter = hummus.createWriter(options.filename);
|
const pdfWriter = hummus.createWriter(options.filename);
|
||||||
|
|
||||||
page.onLoadStarted = function () {
|
page.onLoadStarted = function () {
|
||||||
@ -146,7 +146,6 @@ page.onLoadFinished = function (status) {
|
|||||||
console.log('Loading page finished with status: ' + status);
|
console.log('Loading page finished with status: ' + status);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Must be set before the page is opened
|
|
||||||
page.on('console', (...args) => console.log(args));
|
page.on('console', (...args) => console.log(args));
|
||||||
|
|
||||||
page.onError = function (msg, trace) {
|
page.onError = function (msg, trace) {
|
||||||
@ -156,23 +155,45 @@ page.onError = function (msg, trace) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
openUrl(page, options.url)
|
page.goto(options.url, { waitUntil: 'load', timeout: 60000 })
|
||||||
.then(delay(options.loadPause))
|
.then(removeCssPrintStyles)
|
||||||
.then(createPlugin)
|
.then(delay(options.loadPause))
|
||||||
.then(configurePlugin)
|
.then(createPlugin)
|
||||||
.then(configurePrinter)
|
.then(configurePlugin)
|
||||||
.then(exportSlides)
|
.then(configurePrinter)
|
||||||
.then(function (plugin) {
|
.then(exportSlides)
|
||||||
pdfWriter.end();
|
.then(plugin => {
|
||||||
process.stdout.write('\nPrinted ' + plugin.exportedSlides + ' slides\n');
|
pdfWriter.end();
|
||||||
browser.close();
|
process.stdout.write(`\nPrinted ${plugin.exportedSlides} slides\n`);
|
||||||
process.exit();
|
browser.close();
|
||||||
})
|
process.exit();
|
||||||
.catch(function (error) {
|
})
|
||||||
console.log(error);
|
.catch(error => {
|
||||||
browser.close();
|
console.log(error);
|
||||||
process.exit(1);
|
browser.close();
|
||||||
});
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Can be removed when Puppeteer supports setting media type in rendering emulation
|
||||||
|
// See: https://github.com/GoogleChrome/puppeteer/issues/312
|
||||||
|
function removeCssPrintStyles() {
|
||||||
|
return page.evaluate(_ => {
|
||||||
|
// if (!document.styleSheets) return;
|
||||||
|
for (let j = 0; j < document.styleSheets.length; j++) {
|
||||||
|
const sheet = document.styleSheets[j];
|
||||||
|
if (!sheet.rules) continue;
|
||||||
|
for (let i = sheet.rules.length - 1; i >= 0; i--) {
|
||||||
|
if (sheet.rules[i].cssText.indexOf('@media print') !== -1) {
|
||||||
|
sheet.deleteRule(i);
|
||||||
|
} else if (sheet.rules[i].cssText.indexOf('@media screen') !== -1) {
|
||||||
|
const rule = sheet.rules[i].cssText;
|
||||||
|
sheet.deleteRule(i);
|
||||||
|
sheet.insertRule(rule.replace('@media screen', '@media all'), i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function createPlugin() {
|
async function createPlugin() {
|
||||||
var plugin;
|
var plugin;
|
||||||
@ -271,10 +292,6 @@ async function exportSlide(plugin) {
|
|||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
function openUrl(page, url) {
|
|
||||||
return page.goto(url, { waitUntil: 'networkidle' });
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadAvailablePlugins(pluginPath) {
|
function loadAvailablePlugins(pluginPath) {
|
||||||
return fs.readdirSync(pluginPath).reduce(function (plugins, plugin) {
|
return fs.readdirSync(pluginPath).reduce(function (plugins, plugin) {
|
||||||
var matches = plugin.match(/^(.*)\.js$/);
|
var matches = plugin.match(/^(.*)\.js$/);
|
||||||
|
Loading…
Reference in New Issue
Block a user