diff --git a/lib/Actions.js b/lib/Actions.js index 533b18e..13b189d 100644 --- a/lib/Actions.js +++ b/lib/Actions.js @@ -198,7 +198,7 @@ Actions.prototype = { return vow.reject(new Error('Unknown action type: ' + action.type)); } - actionPromise = actionInstance.perform(); + actionPromise = vow.resolve(actionInstance.perform()); if (action.transform) { actionPromise = actionPromise.then(result => this._parser.transform(result, action.transform)); @@ -272,9 +272,9 @@ Actions.prototype = { */ scroll: function(interval) { debug('scroll %s px', interval); - return this._env.evaluateJs(interval, /* @covignore */ function(interval) { + return vow.resolve(this._env.evaluateJs(interval, /* @covignore */ function(interval) { document.body.scrollTop += interval; - }); + })); } }; diff --git a/lib/Paginator.js b/lib/Paginator.js index 6a54f55..e72f198 100644 --- a/lib/Paginator.js +++ b/lib/Paginator.js @@ -182,10 +182,9 @@ Paginator.prototype = { */ _paginateScroll: function () { var previousHeight; - return this._env - .evaluateJs(/* @covignore */ function () { + return vow.resolve(this._env.evaluateJs(/* @covignore */ function () { return document.body.scrollHeight; - }) + })) .then(function (height) { previousHeight = height; }) @@ -213,12 +212,12 @@ Paginator.prototype = { * @private */ _getRealNextPageIndex: function () { - return this._env.evaluateJs(this._scope, /* @covignore */ function (selector) { + return vow.resolve(this._env.evaluateJs(this._scope, /* @covignore */ function (selector) { var nodes = Sizzle(selector); return nodes.map(function (node) { return parseInt(node.textContent); }); - }).then(function (pages) { + })).then(function (pages) { var pageIndex; if (!Array.isArray(pages) || (pageIndex = pages.indexOf(this._currentPage + 1)) === -1) { throw new Error('Cannot detect page index for pagination'); @@ -241,7 +240,7 @@ Paginator.prototype = { selector = this._scope + ':eq(' + realPageIndex + ')'; }, this) .then(function () { - return this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml); + return vow.resolve(this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml)); }, this) .then(function (html) { previousPageHtml = html; @@ -250,14 +249,14 @@ Paginator.prototype = { var paginationPromise; switch (this._strategy) { case this.STRATEGY.NEW_PAGE: - paginationPromise = this._env.waitForPage(this._timeout); + paginationPromise = vow.resolve(this._env.waitForPage(this._timeout)); break; case this.STRATEGY.AJAX: paginationPromise = this._hasPageDataChanged(previousPageHtml); break; case null: paginationPromise = vow.any([ - this._env.waitForPage(this._timeout).then(function () { + vow.resolve(this._env.waitForPage(this._timeout)).then(function () { this._strategy = this.STRATEGY.NEW_PAGE; debug('Set strategy to %s', this._strategy); }, this), @@ -293,7 +292,7 @@ Paginator.prototype = { _paginateShowMore: function () { var selector = this._scope; var previousPageHtml; - return this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml) + return vow.resolve(this._env.evaluateJs(this._pageScope, this._getPaginatePageHtml)) .then(html => { previousPageHtml = html; }) diff --git a/lib/Parser.js b/lib/Parser.js index f4bdd9b..46ef1e9 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -149,7 +149,7 @@ Parser.prototype = { this._preActions = options.actions || null; this._paginator && this._paginator.reset(); var results; - return this._env.prepare() + return vow.resolve(this._env.prepare()) .then(() => { if (this._preActions) { return this._actions.performActions(this._preActions); @@ -169,9 +169,8 @@ Parser.prototype = { } }) .then( - () => this._env.tearDown(), - e => this._env - .snapshot('error') + () => vow.resolve(this._env.tearDown()), + e => vow.resolve(this._env.snapshot('error')) .always(this._env.tearDown, this._env) .then(() => { throw e @@ -193,16 +192,14 @@ Parser.prototype = { const milestones = stages.milestones; const edgeCases = stages.edgeCases; - return this._env - .prepare() + return vow.resolve(this._env.prepare()) .then(() => milestones.reduce((promise, milestone) => { return promise.then(() => this.passMilestone(milestone, edgeCases)); }, Promise.resolve())) .then( - () => this._env.tearDown(), - e => this._env - .snapshot('error') - .always(() => this._env.tearDown()) + () => vow.resolve(this._env.tearDown()), + e => vow.resolve(this._env.snapshot('error')) + .always(() => vow.resolve(this._env.tearDown())) .then(() => { throw e }) @@ -576,10 +573,9 @@ Parser.prototype = { offset = offset || 0; var maxItems = rule.maxItems || null; var collection = rule.collection[0]; - return this._env - .evaluateJs(this._getSelector(), /* @covignore */ function(selector) { + return vow.resolve(this._env.evaluateJs(this._getSelector(), /* @covignore */ function(selector) { return Sizzle(selector).length; - }) + })) .then((nodesCount) => { debug('parsing %s nodes', nodesCount); if (!nodesCount) { @@ -639,7 +635,7 @@ Parser.prototype = { .then(function(results) { if (this.clearDom) { debug('clear parsed dom for %s', selector); - return this._env.evaluateJs(selector, /* @covignore */ function(selector) { + return vow.resolve(this._env.evaluateJs(selector, /* @covignore */ function(selector) { var parsedElement = Sizzle(selector)[0]; if (!parsedElement) { return; @@ -649,7 +645,7 @@ Parser.prototype = { //parsedElement.setAttribute('class', ''); parsedElement.style.height = boundingRect.height + 'px'; parsedElement.style.width = boundingRect.width + 'px'; - }).then(function() { + })).then(function() { return results; }); } @@ -660,8 +656,7 @@ Parser.prototype = { _injectBrowserRule: function(rule, offset, actionsResults) { debug('._injectBrowserRule()'); let internalGooseResults, internalGooseError; - return this._env - .injectBrowserEnv() + return vow.resolve(this._env.injectBrowserEnv()) .then(() => this._env.evaluateJs(rule, offset, this._scopes, function(rule, offset, scopes) { __gooseParse(rule, offset, scopes); })) @@ -747,7 +742,7 @@ Parser.prototype = { filter.value = rule.prop; } debug('._parseSimpleRule() has called for selector %s with filter %o', selector, filter); - return this._env.evaluateJs(selector, filter, /* @covignore */ function(selector, filter) { + return vow.resolve(this._env.evaluateJs(selector, filter, /* @covignore */ function(selector, filter) { var nodes = Sizzle(selector); return nodes.map(function(node) { switch (filter.type) { @@ -768,7 +763,7 @@ Parser.prototype = { return node.textContent; } }); - }).then(function(results) { + })).then(function(results) { if (!results) { throw new Error('Error during querying selector: ' + selector); } diff --git a/lib/tools/wait.js b/lib/tools/wait.js index 3a9be34..a824220 100644 --- a/lib/tools/wait.js +++ b/lib/tools/wait.js @@ -1,6 +1,7 @@ 'use strict'; -const Promise = require('vow').Promise; +const vow = require('vow'); +const Promise = vow.Promise; /** * Wait until function evalFunction expected in checkerFunction result @@ -39,8 +40,7 @@ module.exports = function wait (env, evalFunction, checkerFunction, args, timeou const evalArgs = args.slice(0); evalArgs.push(evalFunction); var intervalId = setInterval(() => { - env.evaluateJs - .apply(env, evalArgs) + vow.resolve(env.evaluateJs.apply(env, evalArgs)) .then(function() { if (checkerFunction.apply(null, arguments)) { clearTimeout(timeoutId); diff --git a/package.json b/package.json index e7b90ee..33f41c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "goose-parser", - "version": "0.3.0-alpha.2", + "version": "0.3.0-alpha.3", "description": "PhantomJS/Browser lib which allows to parse a webpage", "author": { "name": "Andrew Reddikh",