comparison vendor/jcalderonzumba/gastonjs/src/Client/browser.js @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
1:0b0e5f3b1e83 2:5311817fb629
26 this.owner = owner; 26 this.owner = owner;
27 this.width = width || 1024; 27 this.width = width || 1024;
28 this.height = height || 768; 28 this.height = height || 768;
29 this.pages = []; 29 this.pages = [];
30 this.js_errors = (typeof jsErrors === 'boolean') ? jsErrors : true; 30 this.js_errors = (typeof jsErrors === 'boolean') ? jsErrors : true;
31 this._resourceTimeout = false;
31 this._debug = false; 32 this._debug = false;
32 this._counter = 0; 33 this._counter = 0;
33 this.resetPage(); 34 this.resetPage();
34 } 35 }
35 36
36 /** 37 /**
37 * Resets the browser to a clean slate 38 * Resets the browser to a clean slate
38 * @return {Function}
39 */ 39 */
40 Browser.prototype.resetPage = function () { 40 Browser.prototype.resetPage = function () {
41 var _ref; 41 var _ref;
42 var self = this; 42 var self = this;
43 43
53 this.page.release(); 53 this.page.release();
54 } 54 }
55 phantom.clearCookies(); 55 phantom.clearCookies();
56 } 56 }
57 57
58 this.page = this.currentPage = new Poltergeist.WebPage; 58 this.page = this.currentPage = new Poltergeist.WebPage(null, this);
59 this.page.setViewportSize({ 59 this.page.setViewportSize({
60 width: this.width, 60 width: this.width,
61 height: this.height 61 height: this.height
62 }); 62 });
63 this.page.handle = "" + (this._counter++); 63 };
64 this.pages.push(this.page); 64
65 65 /**
66 return this.page.onPageCreated = function (newPage) { 66 * Adds given newly opened Poltergeist.WebPage to the list of available windows/frames
67 var page; 67 * consulted by methods getPageByHandle, window_handle, window_handles, switch_to_window and close_window.
68 page = new Poltergeist.WebPage(newPage); 68 *
69 page.handle = "" + (self._counter++); 69 * @param {WebPage} page
70 return self.pages.push(page); 70 */
71 }; 71 Browser.prototype.registerPage = function (page) {
72 if (!('handle' in page))
73 {
74 page.handle = "" + (this._counter++);
75 this.pages.push(page);
76 }
77 };
78
79 /**
80 * Removes given closed Poltergeist.WebPage from the list of available windows/frames
81 * consulted by methods getPageByHandle, window_handle, window_handles, switch_to_window and close_window.
82 *
83 * @param {Poltergeist.WebPage} page
84 */
85 Browser.prototype.unregisterPage = function (page) {
86 if (('handle' in page) && (page.handle !== null))
87 {
88 for (var i = this.pages.length; i--;) {
89 if (page === this.pages[i]) {
90 this.pages.splice(i,1);
91 break;
92 }
93 }
94 page.handle = null;
95 }
72 }; 96 };
73 97
74 /** 98 /**
75 * Given a page handle id, tries to get it from the browser page list 99 * Given a page handle id, tries to get it from the browser page list
76 * @param handle 100 * @param handle
196 */ 220 */
197 Browser.prototype.visit = function (serverResponse, url) { 221 Browser.prototype.visit = function (serverResponse, url) {
198 var prevUrl; 222 var prevUrl;
199 var self = this; 223 var self = this;
200 this.currentPage.state = 'loading'; 224 this.currentPage.state = 'loading';
225
226 if (this._resourceTimeout) {
227 this.currentPage._native.settings.resourceTimeout = this._resourceTimeout;
228 this.currentPage._native.onResourceTimeout = function (request) {
229 console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
230 };
231 }
232
201 prevUrl = this.currentPage.source === null ? 'about:blank' : this.currentPage.currentUrl(); 233 prevUrl = this.currentPage.source === null ? 'about:blank' : this.currentPage.currentUrl();
202 this.currentPage.open(url); 234 this.currentPage.open(url);
203 if (/#/.test(url) && prevUrl.split('#')[0] === url.split('#')[0]) { 235 if (/#/.test(url) && prevUrl.split('#')[0] === url.split('#')[0]) {
204 this.currentPage.state = 'default'; 236 this.currentPage.state = 'default';
205 return this.serverSendResponse({ 237 return this.serverSendResponse({
685 return this.execute(serverResponse, 'window.open()'); 717 return this.execute(serverResponse, 'window.open()');
686 }; 718 };
687 719
688 /** 720 /**
689 * Closes the window given by handle name if possible 721 * Closes the window given by handle name if possible
722 * NOTE: Closing a page in PhantomJS also closes new windows/frames opened from that page (QWebPage behaviour)
690 * @param serverResponse 723 * @param serverResponse
691 * @param handle 724 * @param handle
692 * @return {*} 725 * @return {*}
693 */ 726 */
694 Browser.prototype.close_window = function (serverResponse, handle) { 727 Browser.prototype.close_window = function (serverResponse, handle) {
1152 phantom.cookiesEnabled = flag; 1185 phantom.cookiesEnabled = flag;
1153 return this.serverSendResponse(true, serverResponse); 1186 return this.serverSendResponse(true, serverResponse);
1154 }; 1187 };
1155 1188
1156 /** 1189 /**
1190 * Sets proxy or unsets web proxy
1191 * @param {Object} serverResponse Phantomjs response object associated to the client request
1192 * @param {String} ip IP or host name, or null/'' to unset
1193 * @param {Number} port port number
1194 * @param {String} proxyType socks5 or anything else for http(s)
1195 * @param {String} user optional username for proxy authentication
1196 * @param {String} password optional password for proxy authentication
1197 * @return {*}
1198 * @see {@link https://github.com/ariya/phantomjs/pull/11829/commits/84c31822a2e5eba21fe42298ec27ec4ccab95667}
1199 */
1200 Browser.prototype.set_proxy = function (serverResponse, ip, port, proxyType, user, password) {
1201 phantom.setProxy(ip, port, proxyType, user, password);
1202 return this.serverSendResponse(true, serverResponse);
1203 };
1204
1205 /**
1157 * US19: DONE 1206 * US19: DONE
1158 * Sets a basic authentication credential to access a page 1207 * Sets a basic authentication credential to access a page
1159 * THIS SHOULD BE USED BEFORE accessing a page 1208 * THIS SHOULD BE USED BEFORE accessing a page
1160 * @param serverResponse 1209 * @param serverResponse
1161 * @param user 1210 * @param user
1237 return this.serverSendResponse(false, serverResponse); 1286 return this.serverSendResponse(false, serverResponse);
1238 } 1287 }
1239 }; 1288 };
1240 1289
1241 /** 1290 /**
1291 * Sets the timeout in milliseconds, after which any resource requested will stop
1292 * trying and proceed with other parts of the page
1293 * @param serverResponse
1294 * @param value
1295 * @return {*}
1296 */
1297 Browser.prototype.set_resource_timeout = function (serverResponse, value) {
1298 this._resourceTimeout = value;
1299 return this.serverSendResponse(true, serverResponse);
1300 };
1301
1302 /**
1242 * Sets the urlBlacklist for the given urls as parameters 1303 * Sets the urlBlacklist for the given urls as parameters
1243 * @return {boolean} 1304 * @return {boolean}
1244 */ 1305 */
1245 Browser.prototype.set_url_blacklist = function (serverResponse, blackList) { 1306 Browser.prototype.set_url_blacklist = function (serverResponse, blackList) {
1246 this.currentPage.urlBlacklist = Array.prototype.slice.call(blackList); 1307 this.currentPage.urlBlacklist = Array.prototype.slice.call(blackList);