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

Changing webpage settings should get immediate result #13660

Merged
3 commits merged into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/modules/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,23 @@ function decorateNewPage(opts, page) {
} catch (e) {}

// deep copy
page.settings = JSON.parse(JSON.stringify(phantom.defaultPageSettings));
var pageSettings = JSON.parse(JSON.stringify(phantom.defaultPageSettings));
page.settings = {};
for (var p in pageSettings) {
(function (prop) {
Object.defineProperty(page.settings, prop, {
get: function() {
return pageSettings[prop];
},
set: function(val) {
if (pageSettings[prop] !== val && typeof(pageSettings[prop]) === typeof(val)) {
pageSettings[prop] = val;
page.applySettings(pageSettings);
}
}
});
})(p);
}

definePageSignalHandler(page, handlers, "onInitialized", "initialized");

Expand Down Expand Up @@ -277,30 +293,30 @@ function decorateNewPage(opts, page) {
var thisPage = this;

if (arguments.length === 1) {
this.openUrl(url, 'get', this.settings);
this.openUrl(url, 'get', pageSettings);
return;
} else if (arguments.length === 2 && typeof arg1 === 'function') {
this._onPageOpenFinished = function() {
thisPage._onPageOpenFinished = null; //< Disconnect callback (should fire only once)
arg1.apply(thisPage, arguments); //< Invoke the actual callback
}
this.openUrl(url, 'get', this.settings);
this.openUrl(url, 'get', pageSettings);
return;
} else if (arguments.length === 2) {
this.openUrl(url, arg1, this.settings);
this.openUrl(url, arg1, pageSettings);
return;
} else if (arguments.length === 3 && typeof arg2 === 'function') {
this._onPageOpenFinished = function() {
thisPage._onPageOpenFinished = null; //< Disconnect callback (should fire only once)
arg2.apply(thisPage, arguments); //< Invoke the actual callback
}
this.openUrl(url, arg1, this.settings);
this.openUrl(url, arg1, pageSettings);
return;
} else if (arguments.length === 3) {
this.openUrl(url, {
operation: arg1,
data: arg2
}, this.settings);
}, pageSettings);
return;
} else if (arguments.length === 4) {
this._onPageOpenFinished = function() {
Expand All @@ -310,7 +326,7 @@ function decorateNewPage(opts, page) {
this.openUrl(url, {
operation: arg1,
data: arg2
}, this.settings);
}, pageSettings);
return;
} else if (arguments.length === 5) {
this._onPageOpenFinished = function() {
Expand All @@ -321,7 +337,7 @@ function decorateNewPage(opts, page) {
operation: arg1,
data: arg2,
headers : arg3
}, this.settings);
}, pageSettings);
return;
}
throw "Wrong use of WebPage#open";
Expand Down
3 changes: 2 additions & 1 deletion src/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ public slots:
void stopJavaScript();

void clearMemoryCache();

void applySettings(const QVariantMap& defaultSettings);

signals:
void initialized();
Expand Down Expand Up @@ -527,7 +529,6 @@ private slots:
private:
QImage renderImage();
bool renderPdf(const QString& fileName);
void applySettings(const QVariantMap& defaultSettings);
QString userAgent() const;

/**
Expand Down
34 changes: 34 additions & 0 deletions test/module/webpage/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var assert = require('../../assert');
var webpage = require('webpage');

var page = webpage.create();
assert.typeOf(page, 'object');
assert.typeOf(page.settings, 'object');


// test default settings
var defaultPageSettings = JSON.parse(JSON.stringify(phantom.defaultPageSettings));
for (var p in defaultPageSettings) {
assert.equal(page.settings[p], defaultPageSettings[p]);
}

// test userAgent
var newAgent = 'test agent';
function getAcutalAgent() {
return page.evaluate(function() { return navigator.userAgent; });
}
assert.equal(getAcutalAgent(), page.settings.userAgent);
page.settings.userAgent = newAgent;
assert.equal(getAcutalAgent(), newAgent);
page.openUrl('http://google.com', 'get', defaultPageSettings);
assert.equal(getAcutalAgent(), defaultPageSettings.userAgent);

/* todo: should also check those:
* XSSAuditingEnabled
* javascriptCanCloseWindows
* javascriptCanOpenWindows
* javascriptEnabled
* loadImages
* localToRemoteUrlAccessEnabled
* webSecurityEnabled
*/