Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (103 loc) · 3.31 KB
/
Copy pathindex.js
File metadata and controls
155 lines (103 loc) · 3.31 KB
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
const WebSocket = require('ws');
// To open chrome as child process
const { spawn } = require('child_process');
const req = require("request");
var sleep = require('system-sleep')
const fs = require("fs");
const port = 9222;
const waitFor = 1000;// milliseconds
const timeout = (60 + waitFor)*1000; //10 seconds
// /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
const chrome = spawn('chrome',[
'--remote-debugging-port='+port,
'--user-data-dir=temp',
process.argv[2]
]);
chrome.stdout.on('data', function(data) {
console.log(data.toString());
});
let interval = setTimeout(()=>{
chrome.kill()
clearTimeout(interval)
}, timeout)
// Asking for opened tabs
let interval2 = setTimeout(() => {
const log = fs.openSync("log.txt", 'a')
// Accessing chrome publish websocket address
req(`http://localhost:${port}/json`,function (error, response, body) {
const tab = JSON.parse(body)[0];
console.log("Enabled websockets")
console.log(tab)
const url = tab.webSocketDebuggerUrl;
const ws = new WebSocket(url);
ws.on('open', function open() {
ws.send(JSON.stringify({id: 3, method: 'Network.enable'}))
});
const group = {}
ws.on("close", function(){
fs.writeFileSync("group.json", JSON.stringify(group, null, 4))
// How many domains?
let domainCount = {}
const re = /^https?:\/\/([\w\.]+)\//
let js, css, other, img;
js = 0
css = 0
other = 0
img = 0
let userGeneratedRequeests = 0
for(var r in group)
{
const events = group[r]
for(const event of events){
if(event.method === "Network.requestWillBeSent"){
let url = event.params.request.url;
if(url.match(re))
domainCount[url.match(re)[1]] = 1
switch(event.params.type){
case "Script":
js++;
break;
case "Other":
other++;
break;
case "Stylesheet":
css++;
break;
case "Image":
img++;
break;
}
if(event.params.hasUserGesture){
userGeneratedRequeests++;
}
}
}
}
// How many web request ?
console.log("This session had", Object.keys(group).length, "different web requests")
console.log("\tScripts", js)
console.log("\tCSS", css)
console.log("\tOther", other)
console.log("\timage", img)
// How many domains ?
console.log("This session reachs", Object.keys(domainCount).length, "different domains:\n\t", Object.keys(domainCount).join("\n\t"))
// User generated requests
console.log("User generated requests",userGeneratedRequeests)
process.exit(0)
})
ws.on('message', function incoming(data) {
fs.writeSync(log, data + "\n")
const obj = JSON.parse(data)
if("params" in obj){
const id = obj.params.requestId
if(id){
if(!(id in group)){
group[id] = []
}
group[id].push(obj)
}
}
});
});
clearTimeout(interval2)
}, waitFor)