Skip to content

Commit

Permalink
Merge v0.3.1 into core
Browse files Browse the repository at this point in the history
Conflicts:
	package-lock.json
	package.json
  • Loading branch information
tungs committed Dec 16, 2021
2 parents 736c8e2 + 657d8d2 commit e9a55bf
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 29 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const path = require('path');
const defaultDuration = 5;
const defaultFPS = 60;
const { overwriteRandom } = require('./lib/overwrite-random');
const { getBrowserFrames, stringArrayFind } = require('./lib/utils');
const { getBrowserFrames, stringArrayFind, getPageViewportSize, setPageViewportSize } = require('./lib/utils');

module.exports = async function (config) {
config = Object.assign({}, config || {});
Expand Down Expand Up @@ -150,13 +150,13 @@ module.exports = async function (config) {
if (config.viewport || scaleArg) {
config.viewport = Object.assign(
{
width: page.viewport().width,
height: page.viewport().height,
width: getPageViewportSize(page).width,
height: getPageViewportSize(page).height,
deviceScaleFactor: scaleArg ? Number(scaleArg.split('=')[1]) || 1 : 1
},
config.viewport
);
await page.setViewport(config.viewport);
await setPageViewportSize(page, config.viewport);
}
await overwriteRandom(page, unrandom, log);
await timeHandler.overwriteTime(page);
Expand Down
4 changes: 2 additions & 2 deletions lib/capture-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
const makeCanvasCapturer = require('./make-canvas-capturer');

const canvasToBuffer = async function (page, canvasSelector, type, quality) {
var dataUrl = await page.evaluate((canvasSelector, type, quality) =>
var dataUrl = await page.evaluate(({ canvasSelector, type, quality }) =>
document.querySelector(canvasSelector).toDataURL(type, quality)
, canvasSelector, type, quality);
, { canvasSelector, type, quality });
var data = dataUrl.slice(dataUrl.indexOf(',') + 1);
return new Buffer(data, 'base64');
};
Expand Down
4 changes: 2 additions & 2 deletions lib/capture-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

const { getSelectorDimensions, makeFilePathConverter, makeFileDirectoryIfNeeded } = require('./utils.js');
const { getSelectorDimensions, getPageViewportSize, makeFilePathConverter, makeFileDirectoryIfNeeded } = require('./utils.js');

module.exports = function (config) {
var page = config.page;
Expand All @@ -47,7 +47,7 @@ module.exports = function (config) {
log('Warning: no element found for ' + config.selector);
}
}
var viewport = page.viewport();
var viewport = getPageViewportSize(page);
var x = config.xOffset || config.left || 0;
var y = config.yOffset || config.top || 0;
var right = config.right || 0;
Expand Down
20 changes: 10 additions & 10 deletions lib/immediate-canvas-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,26 @@ module.exports = function (config) {
if (config.alwaysSaveCanvasData) {
goToTime = async function (browserFrames, time) {
// Goes to a certain time. Can't go backwards
return page.evaluate((ms, canvasSelector, type, quality) => {
window._timeweb_processUntilTime(ms);
return page.evaluate(({ ms, canvasSelector, type, quality }) => {
window.timeweb.processUntilTime(ms);
var canvasElement = document.querySelector(canvasSelector);
window._timesnap_canvasData = canvasElement.toDataURL(type, quality);
}, time, canvasSelector, canvasCaptureMode, quality);
}, { ms: time, canvasSelector, type: canvasCaptureMode, quality });
};
} else {
goToTime = oldGoToTime;
}

const goToTimeAndAnimateForCapture = async function (browserFrames, time) {
// Goes to a certain time. Can't go backwards
return page.evaluate((ms, canvasSelector, type, quality) => {
window._timeweb_processUntilTime(ms);
return window._timeweb_runFramePreparers(ms, function () {
window._timeweb_runAnimationFrames();
return page.evaluate(({ ms, canvasSelector, type, quality }) => {
window.timeweb.processUntilTime(ms);
return window.timeweb.runFramePreparers(ms, function () {
window.timeweb.runAnimationFrames();
var canvasElement = document.querySelector(canvasSelector);
window._timesnap_canvasData = canvasElement.toDataURL(type, quality);
});
}, time, canvasSelector, canvasCaptureMode, quality);
}, { ms: time, canvasSelector, type: canvasCaptureMode, quality });
};

var goToTimeAndAnimate;
Expand All @@ -81,8 +81,8 @@ module.exports = function (config) {
goToTimeAndAnimate = async function (browserFrames, time) {
// Goes to a certain time. Can't go backwards
return page.evaluate(function (ms) {
window._timeweb_processUntilTime(ms);
return window._timeweb_runFramePreparers(ms, window._timeweb_runAnimationFrames);
window.timeweb.processUntilTime(ms);
return window.timeweb.runFramePreparers(ms, window.timeweb.runAnimationFrames);
}, time);
};
}
Expand Down
4 changes: 3 additions & 1 deletion lib/overwrite-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

const { evaluateOnNewDocument } = require('./utils.js');

// unrandomizer seed constants
// default seed values are only used if all of the seed values end up being 0
const defaultSeed1 = 10;
Expand Down Expand Up @@ -61,7 +63,7 @@ const overwriteRandom = function (page, unrandom, log) {
};

const overwritePageRandom = function (page, seed1 = 0, seed2 = 0, seed3 = 0, seed4 = 0) {
return page.evaluateOnNewDocument(function (config) {
return evaluateOnNewDocument(page, function (config) {
(function (exports) {
let shift1 = 23;
let shift2 = 17;
Expand Down
9 changes: 5 additions & 4 deletions lib/overwrite-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@
*/
const fs = require('fs');
const path = require('path');
const { evaluateOnNewDocument } = require('./utils.js');

const timewebLib = fs.readFileSync(
path.join(require.resolve('timeweb/dist/timeweb.js')),
{ encoding: 'utf8' }
);
const overwriteTime = async function (page) {
return page.evaluateOnNewDocument(timewebLib);
return evaluateOnNewDocument(page, timewebLib);
};

const goToTime = async function (browserFrames, time) {
// Goes to a certain time. Can't go backwards
return Promise.all(browserFrames.map(function (frame) {
return frame.evaluate(function (ms) {
window._timeweb_processUntilTime(ms);
window.timeweb.processUntilTime(ms);
}, time);
}));
};
Expand All @@ -53,8 +54,8 @@ const goToTimeAndAnimate = async function (browserFrames, time) {
// Goes to a certain time. Can't go backwards
return Promise.all(browserFrames.map(function (frame) {
return frame.evaluate(function (ms) {
window._timeweb_processUntilTime(ms);
return window._timeweb_runFramePreparers(ms, window._timeweb_runAnimationFrames);
window.timeweb.processUntilTime(ms);
return window.timeweb.runFramePreparers(ms, window.timeweb.runAnimationFrames);
}, time);
}));
};
Expand Down
35 changes: 35 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ const getBrowserFrames = function (frame) {
return [frame].concat(...frame.childFrames().map(getBrowserFrames));
};

const evaluateOnNewDocument = function (page, fn, arg) {
if (page.evaluateOnNewDocument) {
if (arg !== undefined) {
page.evaluateOnNewDocument(fn, arg);
} else {
page.evaluateOnNewDocument(fn);
}
} else if (page.addInitScript) {
if (arg !== undefined) {
page.addInitScript(fn, arg);
} else {
page.addInitScript(fn);
}
}
};

const getPageViewportSize = function (page) {
if (page.viewport) {
return page.viewport();
} else if (page.viewportSize) {
return page.viewportSize();
}
};

const setPageViewportSize = async function (page, config) {
if (page.setViewport) {
return page.setViewport(config);
} else if (page.setViewportSize) {
return page.setViewportSize(config);
}
};

const getSelectorDimensions = async function (page, selector) {
return page.evaluate(function (selector) {
var el = document.querySelector(selector);
Expand Down Expand Up @@ -116,6 +148,9 @@ const makeFileDirectoryIfNeeded = function (filepath) {

module.exports = {
getBrowserFrames,
evaluateOnNewDocument,
getPageViewportSize,
setPageViewportSize,
getSelectorDimensions,
writeFile,
stringArrayFind,
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timesnap-core",
"version": "0.3.1-prerelease",
"version": "0.3.1",
"description": "Take screenshots of web pages at smooth intervals",
"repository": {
"type": "git",
Expand All @@ -18,7 +18,7 @@
},
"license": "BSD-3-Clause",
"dependencies": {
"timeweb": "^0.1.1",
"timeweb": "^0.2.0",
"sprintf-js": "1.1.1"
},
"devDependencies": {
Expand Down

0 comments on commit e9a55bf

Please sign in to comment.