Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
added passing variables to function for page.evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
firedfox committed Apr 6, 2012
1 parent afb0707 commit 0bb8cff
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/modules/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,30 @@ exports.create = function (opts) {
this._appendScriptElement(scriptUrl);
};

/**
* evaluate a function in the page
* @param {function} func the function to evaluate
* @param {...} args function arguments
* @return {*} the function call result
*/
page.evaluate = function (func, args) {
if (!(func instanceof Function || typeof func == 'string' || func instanceof String)) {
throw "Wrong use of WebPage#evaluate";
return;
}
var str = 'function() { return (' + func.toString() + ')(';
for (var i = 1, l = arguments.length; i < l; i++) {
var arg = arguments[i];
if (/object|string/.test(typeof arg) && !(arg instanceof RegExp)) {
str += 'JSON.parse(' + JSON.stringify(JSON.stringify(arg)) + '),';
} else {
str += arg + ',';
}
}
str = str.replace(/,$/, '') + '); }';
return this.evaluateJavaScript(str);
}

// Copy options into page
if (opts) {
page = copyInto(page, opts);
Expand Down
2 changes: 1 addition & 1 deletion src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ QVariantMap WebPage::paperSize() const
return m_paperSize;
}

QVariant WebPage::evaluate(const QString &code)
QVariant WebPage::evaluateJavaScript(const QString &code)
{
QString function = "(" + code + ")()";
return m_mainFrame->evaluateJavaScript(function, QString("phantomjs://webpage.evaluate()"));
Expand Down
2 changes: 1 addition & 1 deletion src/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public slots:
void openUrl(const QString &address, const QVariant &op, const QVariantMap &settings);
void release();

QVariant evaluate(const QString &code);
QVariant evaluateJavaScript(const QString &code);
bool render(const QString &fileName);
bool injectJs(const QString &jsFilePath);
void _appendScriptElement(const QString &scriptUrl);
Expand Down
59 changes: 59 additions & 0 deletions test/webpage-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,65 @@ describe("WebPage object", function() {
});

});

it("should pass variables to functions properly", function() {
var testPrimitiveArgs = function() {
var samples = [
true,
0,
"`~!@#$%^&*()_+-=[]\{}|;':\",./<>?",
undefined,
null
];
for (var i = 0; i < samples.length; i++) {
if (samples[i] !== arguments[i]) {
console.log("FAIL");
}
}
};

var testComplexArgs = function() {
var samples = [
{a:true, b:0, c:"string"},
function() { return true; },
[true, 0, "string"],
/\d+\w*\//
];
for (var i = 0; i < samples.length; i++) {
if (typeof samples[i] !== typeof arguments[i]
|| samples[i].toString() !== arguments[i].toString()) {
console.log("FAIL");
}
}
};

var message;
runs(function() {
page.onConsoleMessage = function (msg) {
message = msg;
}
});

waits(0);

runs(function() {
page.evaluate(function() {
console.log("PASS");
});
page.evaluate(testPrimitiveArgs,
true,
0,
"`~!@#$%^&*()_+-=[]\{}|;':\",./<>?",
undefined,
null);
page.evaluate(testComplexArgs,
{a:true, b:0, c:"string"},
function() { return true; },
[true, 0, "string"],
/\d+\w*\//);
expect(message).toEqual("PASS");
});
});
});

describe("WebPage construction with options", function () {
Expand Down

0 comments on commit 0bb8cff

Please sign in to comment.