-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (79 loc) · 2.4 KB
/
index.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
/*
Pickard module
@antouank 2014
*/
'use strict';
var Pickard,
chromeCommunication = require('./lib/chrome-communication'),
log = require('consologger'),
PORT = 9500;
// main Pickard object
Pickard = function(){
var thisPickard = this,
generateLoadPromise;
// increments on every Runtime.executionContextCreated event
this.contextId = 0;
// ------------------------------------------------------ Pickard.showDebug
thisPickard.showDebug = false;
// ------------------------------------------------------ Pickard.config
thisPickard.config = {
port: PORT
};
generateLoadPromise = function(){
thisPickard.pageLoadDeferred = Promise.defer();
thisPickard.pageLoadDeferred._timestamp = Date.now();
thisPickard.pageLoadDeferred
.promise
.then(function(){
thisPickard.showDebug && log.info('load promise resolved! making new one...');
generateLoadPromise();
});
};
// first load event promise is born!
generateLoadPromise();
// ------------------------------------------------------ Pickard.getNextPageLoad
thisPickard.getNextPageLoad = function(){
return thisPickard.pageLoadDeferred.promise;
};
// ------------------------------------------------------ Pickard.openPage
// open a page and get the .websocket and .evaluate functions on 'this'
thisPickard.openPage = function(address){
return chromeCommunication
.launch({
port: thisPickard.config.port,
address: address
})
.then(function(debugTab){
// ------------------------------------------------------ Pickard.websocket
// .websocket
thisPickard.websocket = debugTab.webSocketDebuggerUrl;
// here comes .evaluate .ws and .write
var promise =
chromeCommunication
.openSocket(thisPickard, thisPickard.websocket);
promise
.then(chromeCommunication.prepareSocket)
.then(function(){
thisPickard.showDebug && log.info('socket prepared');
})
.catch(function(err){
log.error(err);
});
return promise;
})
.catch(function(err){
throw err;
});
};
// ------------------------------------------------------ Pickard.exit
thisPickard.exit = function(){
chromeCommunication.exit();
};
return thisPickard;
};
process
.on('exit', function() {
chromeCommunication.exit();
});
// expose the module
module.exports = Pickard;