-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
181 lines (166 loc) · 4.72 KB
/
main.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
//https://stackoverflow.com/questions/469357/html-text-input-allows-only-numeric-input
function setInputFilter(textbox, inputFilter) {
["input", "keydown", "keyup", "mousedown", "mouseup", "select", "contextmenu", "drop"].forEach(function (event) {
textbox.addEventListener(event, function () {
if (inputFilter(this.value)) {
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
}
});
});
}
document.onload = function()
{
setInputFilter(document.getElementById("depthText"), function (value) { return /^-?\d*$/.test(value); });
container = document.getElementById('mynetwork');
};
//API
var nameID = {};
var explored = [];
var nextID = 0;
//chart data
var nodes = [];
var edges = [];
var data = {
nodes: [],
edges: []
};
var container;
var options = {
nodes: {
shape: 'dot'
}
};
var network;
function exportNet()
{
var pageData = {};
var maxDepth = parseInt(document.getElementById("depthText").value);
var initUser = document.getElementById("initial").value;
pageData.maxDepth = maxDepth;
pageData.initUser = initUser;
pageData.netData = data;
pageData.nameID = nameID;
document.getElementById("io").value = JSON.stringify(pageData);
}
function start()
{
data = {
nodes: [],
edges: []
};
var io = document.getElementById("io").value;
if (io.length != 0)
{
var pageData = JSON.parse(io);
document.getElementById("depthText").value = pageData.maxDepth;
document.getElementById("initial").value = pageData.initUser;
data = pageData.netData;
nameID = pageData.nameID;
}
container = document.getElementById('mynetwork');
network = new vis.Network(container, data, options);
network.on('click', handleClick);
if (io.length != 0) return;
var maxDepth = parseInt(document.getElementById("depthText").value);
var initUser = document.getElementById("initial").value;
pushVenmoUser(initUser, 0, maxDepth);
}
function pushVenmoUser(userPage, depth, maxDepth)
{
if (depth >= maxDepth) return;
if (explored.includes(userPage)) return;
explored.push(userPage);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function (){processUser(depth, maxDepth, xhttp);};
xhttp.open("GET", "venmo.php?user=" + userPage);
xhttp.send();
}
function processUser(depth, maxDepth, xhttp)
{
if (xhttp.readyState == 4 && xhttp.status == 200) {
//console.log(xhttp.responseText);
var userdata = JSON.parse(xhttp.responseText);
var rootNode = getIDfromName(userdata.url, userdata.displayName);
var transactions = userdata.transactions;
var toExplore = [];
for (var i = 0; i<transactions.length; i++)
{
var tid = getIDfromName(transactions[i].url, transactions[i].displayName);
if (!toExplore.includes(transactions[i].url)) toExplore.push(transactions[i].url);
addEdge(rootNode, tid);
}
network.setData(data);
console.log(toExplore);
for (var i = 0; i<toExplore.length; i++)
{
pushVenmoUser(toExplore[i], depth+1, maxDepth);
}
}
}
function handleClick(properties)
{
//just open the first one clicked
var id = properties.nodes[0];
if (!id) return;
var url = "https://venmo.com/" + getUrlFromID(id);
window.open(url);
}
function getUrlFromID(id)
{
for (var name in nameID) {
if (nameID[name] == id) return name;
}
return false;
}
function getIDfromName(name, label)
{
var id = nameID[name];
if (nameID[name] == undefined) {
data.nodes.push({
id: nextID,
label: label
});
nameID[name] = nextID;
id = nextID;
nextID++;
}
return id;
}
function addEdge(from, to)
{
var exists = false;
var existIndex = -1;
var existValue = undefined;
for (var i = 0; i < data.edges.length; i++)
{
if (data.edges[i].from == from && data.edges[i].to == to)
{
exists = true;
existIndex = i;
existValue = data.edges[i].value;
break;
}
}
if (!exists)
{
data.edges.push({
from: from,
to: to,
value: 1
})
}
else
{
data.edges.splice(existIndex, 1);
data.edges.push({
from: from,
to: to,
value: existValue + 1
});
}
}