-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
276 lines (243 loc) · 8.09 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
if(localStorage.getItem("token") == null){
document.getElementById("noSavedToken").hidden = false;
}else{
document.getElementById("savedToken").hidden = false;
}
function clearData(){
localStorage.clear();
document.getElementById("noSavedToken").hidden = false;
document.getElementById("savedToken").hidden = true;
}
let width = null;
let height = null;
let links = null;
let nodes = null;
let simulation = null;
let svg = null;
let link = null;
let node = null;
let zoom = null;
let token = null;
const reqDelay = 3388;
let reqInterval = null;
let linked = 0;
let zoomOffsetX = 0;
let zoomOffsetY = 0;
let data = {"nodes": [], "links": []};
/*for(let i = 0; i < graph.length; i++){
data.nodes.push({"id": userIDs[i], "name": userMap[userIDs[i]]});
for(let j = 0; j < graph[i].length; j++){
if(graph[i][j] < i) continue;
data.links.push({"source": userIDs[i], "target": userIDs[graph[i][j]]});
}
}*/
function makeGraph(){
if(localStorage.getItem("token") == null){
token = document.getElementById("token").value;
localStorage.setItem("token", token);
}else{
token = localStorage.getItem("token");
}
if(localStorage.getItem("data") != null){
data = JSON.parse(localStorage.getItem("data"));
}
if(localStorage.getItem("linked") != null){
linked = parseInt(localStorage.getItem("linked"));
}
document.getElementById("noSavedToken").hidden = true;
document.getElementById("savedToken").hidden = true;
document.getElementById("searchdiv").hidden = false;
document.getElementById("status").hidden = false;
document.getElementById("status").innerHTML = "Getting Friends...";
fetch("https://discord.com/api/v9/users/@me/relationships", {"headers": {"authorization": token}}).then(x => x.json()).then(j => function(){
for(let i = data.nodes.length; i < j.length; i++){
let name = j[i].user.username;
if(j[i].user.global_name != null){
name += " (" + j[i].user.global_name + ")";
}
data.nodes.push({"id": j[i].id, "name": name});
}
initSvg();
render();
updateToolTip();
reqInterval = setInterval(function(){
function getRelationships(id){
let clinks = [];
let cidx = 0;
fetch("https://discord.com/api/v9/users/" + id + "/relationships", {"headers": {"authorization": token}}).then(x => x.json()).then(j => function(){
for(let i = 0; i < j.length; i++){
let cid = j[i].id;
if(parseInt(id) > parseInt(cid)) continue;
console.log("huh");
//data.links.push({"source": id, "target": cid});
clinks.push({"source": id, "target": cid});
}
let updInterval = null;
updInterval = setInterval(function (){
if(cidx == clinks.length){
clearInterval(updInterval);
return;
}
console.log("sus");
data.links.push(clinks[cidx]);
cidx++;
render();
}, reqDelay*1.0 / (clinks.length + 3));
}());
}
if(linked < data.nodes.length){
document.getElementById("status").innerHTML = "Getting Friends for: " + data.nodes[linked].name + " | " + linked.toString() + "/" + data.nodes.length.toString();
getRelationships(data.nodes[linked].id);
linked++;
localStorage.setItem("data", JSON.stringify(data));
localStorage.setItem("linked", linked.toString());
//render();
}else{
clearInterval(reqInterval);
}
}, reqDelay);
}());
}
function initSvg(){
width = window.innerWidth;
height = window.innerHeight;
// Specify the color scale.
color = d3.scaleOrdinal(d3.schemeCategory10);
// The force simulation mutates links and nodes, so create a copy
// so that re-evaluating this cell produces the same result.
links = data.links.map(d => ({...d}));
nodes = data.nodes.map(d => ({...d}));
// Create a simulation with several forces.
simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width/10, height/10))
.on("tick", ticked);
// Create the SVG container.
svg = d3.select("#container").append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
zoom = d3.zoom().on("zoom", function () {
node.attr("transform", d3.zoomTransform(this))
link.attr("transform", d3.zoomTransform(this))
});
svg.call(zoom);
link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.3)
.selectAll()
node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll().data(nodes)
.join("circle")
.attr("r", 5);;
node.append("title")
.text(d => d.name);
// Set the position attributes of links and nodes each time the simulation ticks.
}
function render(){
links = data.links.map(d => ({...d}));
simulation.force("link").links(links);
simulation.alpha(1).restart().tick();
// Add a line for each link, and a circle for each node.
link = link.data(links)
.join("line").attr("stroke-width", 0.5)
// Add a drag behavior.
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.attr("transform", d3.zoomTransform(svg["_groups"][0][0]))
link.attr("transform", d3.zoomTransform(svg["_groups"][0][0]))
// Reheat the simulation when drag starts, and fix the subject position.
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
zoomOffsetX = (event.subject.x - event.subject.x / d3.zoomTransform(svg["_groups"][0][0]).k);
zoomOffsetY = (event.subject.y - event.subject.y / d3.zoomTransform(svg["_groups"][0][0]).k);
event.subject.fx = event.subject.x / d3.zoomTransform(svg["_groups"][0][0]).k + zoomOffsetX;
event.subject.fy = event.subject.y / d3.zoomTransform(svg["_groups"][0][0]).k + zoomOffsetY;
}
// Update the subject (dragged node) position during drag.
function dragged(event) {
event.subject.fx = event.x / d3.zoomTransform(svg["_groups"][0][0]).k + zoomOffsetX;
event.subject.fy = event.y / d3.zoomTransform(svg["_groups"][0][0]).k + zoomOffsetY;
}
// Restore the target alpha so the simulation cools after dragging ends.
// Unfix the subject position now that it’s no longer being dragged.
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
}
function ticked() {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
}
function searchnodes(){
let username = document.getElementById("search").value;
const circles = document.querySelectorAll('circle');
for(let i = 0; i < circles.length; i++){
let found = false;
if(username != ""){
if(circles[i].getAttribute("data-title") == null){
if(circles[i].childNodes[0].innerHTML.includes(username)){
found = true;
}
}else{
if(circles[i].getAttribute("data-title").includes(username)){
found = true;
}
}
}
if(found){
circles[i].setAttribute("fill", "red");
}else{
circles[i].setAttribute("fill", "black");
}
}
}
let clicked = false;
function showTooltip(event) {
let element = event.target;
let tooltipElement = document.getElementById('tooltip');
let title;
if (!element.dataset.title) {
let titleElement = element.querySelector('title');
title = titleElement.innerHTML;
event.target.setAttribute('data-title', title);
titleElement.parentNode.removeChild(titleElement);
} else {
title = element.dataset.title;
}
tooltipElement.innerHTML = title;
tooltipElement.style.display = 'block';
tooltipElement.style.left = event.pageX + 10 + 'px';
tooltipElement.style.top = event.pageY + 10 + 'px';
}
function hideTooltip() {
if(!clicked){
var tooltip = document.getElementById('tooltip');
tooltip.style.display = 'none';
}
}
function clickey(){
clicked = !clicked;
}
function updateToolTip(){
const tooltipTriggers = document.querySelectorAll('circle');
Array.from(tooltipTriggers).map(trigger => {
trigger.addEventListener('mousemove', showTooltip);
trigger.addEventListener('mouseout', hideTooltip);
trigger.addEventListener('click', clickey);
})
}