forked from baudehlo/node-phantom-simple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.js
208 lines (185 loc) · 5.68 KB
/
bridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var webpage = require('webpage');
var webserver = require('webserver').create();
var system = require('system');
var pages = {};
var page_id = 1;
var callback_stack = [];
phantom.onError = function (msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
});
}
system.stderr.writeLine(msgStack.join('\n'));
phantom.exit(1);
}
function page_open (res, page, args) {
page.open.apply(page, args.concat(function (success) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify(success));
// console.log("Close1");
res.close();
}))
}
function include_js (res, page, args) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.write('"success"');
// console.log("Calling includeJs");
var response = page.includeJs.apply(page, args.concat(function () {
// console.log("Came back...");
try {
res.write('');
// console.log("Close2");
res.close();
}
catch (e) {
if (!/cannot call function of deleted QObject/.test(e)) { // Ignore this error
page.onError(e);
}
}
}));
}
var service = webserver.listen('127.0.0.1:0', function (req, res) {
// console.log("Got a request of type: " + req.method);
if (req.method === 'GET') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
// console.log("Sending back " + callback_stack.length + " callbacks");
res.write(JSON.stringify(callback_stack));
callback_stack = [];
// console.log("Close3");
res.close();
}
else if (req.method === 'POST') {
var request = JSON.parse(req.post);
var method = request.method;
var output = null;
var error = null;
if (request.page) {
if (method === 'open') { // special case this as it's the only one with a callback
return page_open(res, pages[request.page], request.args);
}
else if (method === 'includeJs') {
return include_js(res, pages[request.page], request.args);
}
try {
// console.log("Calling: page." + method + "(" + request.args + ")");
var output = pages[request.page][method].apply(pages[request.page], request.args);
// console.log("Got output: ", output);
}
catch (err) {
error = err;
}
}
else {
try {
output = global_methods[method].apply(global_methods, request.args);
}
catch (err) {
error = err;
}
}
res.setHeader('Content-Type', 'application/json');
if (error) {
res.statusCode = 500;
res.write(JSON.stringify(error));
}
else {
// console.log("Results: " + output);
res.statusCode = 200;
res.write(JSON.stringify(output || null));
}
// console.log("Close4")
res.close();
}
else {
throw "Unknown request type!";
}
});
var callbacks = [
'onAlert', 'onCallback', 'onClosing', 'onConfirm', 'onConsoleMessage', 'onError', 'onFilePicker',
'onLoadFinished', 'onLoadStarted', 'onNavigationRequested',
'onPrompt', 'onResourceRequested', 'onResourceReceived', 'onResourceError', 'onUrlChanged',
];
function setup_callbacks (id, page, ignore_callbacks, files_to_inject) {
ignore_callbacks = ignore_callbacks || [];
callbacks.forEach(function (cb) {
if (ignore_callbacks.indexOf(cb) >= 0) return;
page[cb] = function (parm) {
var args = Array.prototype.slice.call(arguments);
if ((cb==='onResourceRequested') && (parm.url.indexOf('data:image') === 0)) return;
// console.log("Callback: " + cb);
if (cb==='onClosing') { args = [] };
callback_stack.push({'page_id': id, 'callback': cb, 'args': args});
// console.log("Callback stack size now: " + callback_stack.length);
};
});
// Special case these
if (ignore_callbacks.indexOf('onPageCreated') < 0) {
page.onPageCreated = function (page) {
var new_id = setup_page(page);
callback_stack.push({'page_id': id, 'callback': 'onPageCreated', 'args': [new_id]})
};
}
if (ignore_callbacks.indexOf('onInitialized') < 0) {
page.onInitialized = function() {
if (Array.isArray(files_to_inject) && files_to_inject.length > 0) {
files_to_inject.forEach(function(file) {
page.injectJs(file);
});
}
var args = Array.prototype.slice.call(arguments);
callback_stack.push({'page_id': id, 'callback': 'onInitialized', 'args': args});
};
}
}
function setup_page (page, ignore_callbacks, files_to_inject) {
var id = page_id++;
page.getProperty = function (prop) {
return page[prop];
}
page.setProperty = function (prop, val) {
return page[prop] = val;
}
page.setFunction = function (name, fn) {
page[name] = eval('(' + fn + ')');
return true;
}
pages[id] = page;
setup_callbacks(id, page, ignore_callbacks, files_to_inject);
return id;
}
var global_methods = {
createPage: function (args) {
var page = webpage.create();
var id = setup_page(page, args.ignore_callbacks, args.files_to_inject);
return { page_id: id };
},
injectJs: function (filename) {
return phantom.injectJs(filename);
},
exit: function (code) {
return phantom.exit(code);
},
addCookie: function (cookie) {
return phantom.addCookie(cookie);
},
clearCookies: function () {
return phantom.clearCookies();
},
deleteCookie: function (name) {
return phantom.deleteCookie(name);
},
getProperty: function (prop) {
return phantom[prop];
},
setProperty: function (prop, value) {
phantom[prop] = value;
return true;
},
}
console.log("Ready [" + system.pid + "]");