-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.html
More file actions
257 lines (202 loc) · 5.84 KB
/
graph.html
File metadata and controls
257 lines (202 loc) · 5.84 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Domain Tool</title>
<script src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<style>
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #0b0b0b;
font-family: Arial;
}
#network {
width: 100%;
height: 100%;
}
#ui {
position: absolute;
top: 10px;
left: 10px;
z-index: 1000;
background: rgba(0,0,0,0.85);
padding: 10px;
border-radius: 8px;
color: white;
}
button {
cursor: pointer;
margin-top: 5px;
}
</style>
</head>
<body>
<div id="ui">
<input type="file" id="fileInput" accept=".json"><br>
<button id="loadBtn">Load JSON</button>
<button id="mapBtn">Load Map</button>
<button id="graphBtn">Load Graph</button>
<button id="searchBtn">Search</button>
<p>note: you need the crawl.json file generated by my private web crawler to use this. sorry!</p>
</div>
<div id="network"></div>
<script>
let network = null;
let dataset = null;
let nodeMap = new Map();
let degreeMap = new Map();
/* ---------------- INIT ---------------- */
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("loadBtn").onclick = loadJSON;
document.getElementById("mapBtn").onclick = loadMap;
document.getElementById("graphBtn").onclick = loadBarGraph;
document.getElementById("searchBtn").onclick = searchNode;
});
/* ---------------- LOAD JSON ---------------- */
function loadJSON() {
const input = document.getElementById("fileInput");
if (!input.files.length) {
alert("Pick a JSON file.");
return;
}
const reader = new FileReader();
reader.onload = e => {
dataset = JSON.parse(e.target.result);
preprocess(dataset);
alert("Loaded dataset");
};
reader.readAsText(input.files[0]);
}
/* ---------------- PREPROCESS ---------------- */
function preprocess(json) {
nodeMap.clear();
degreeMap.clear();
for (const d of json.domains) {
nodeMap.set(normalize(d.domain), d);
degreeMap.set(d.id, d.outbound_domains.length);
}
}
/* ---------------- NORMALIZE DOMAIN ---------------- */
function normalize(url) {
if (typeof url !== "string") return "";
return url
.toLowerCase()
.replace("https://", "")
.replace("http://", "")
.replace("www.", "")
.replace(/\/.*$/, "");
}
/* ---------------- MAP MODE ---------------- */
function loadMap() {
if (!dataset) return alert("Load JSON first");
const domains = dataset.domains;
const nodes = [];
const edges = [];
const edgeSet = new Set();
const max = Math.max(...degreeMap.values(), 1);
for (const d of domains) {
const importance = (degreeMap.get(d.id) || 0) / max;
nodes.push({
id: d.id,
label: d.domain,
value: importance * 10,
x: (Math.random() - 0.5) * 1200,
y: (Math.random() - 0.5) * 1200,
fixed: { x: true, y: true },
title: d.domain
});
for (const o of d.outbound_domains) {
const to = nodeMap.get(normalize(o));
if (!to) continue;
const key = d.id < to.id ? d.id + "|" + to.id : to.id + "|" + d.id;
if (edgeSet.has(key)) continue;
edgeSet.add(key);
edges.push({
id: key,
from: d.id,
to: to.id,
color: "rgba(120,120,120,0.25)"
});
}
}
renderGraph(nodes, edges);
}
/* ---------------- BAR GRAPH MODE ---------------- */
function loadBarGraph() {
if (!dataset) return alert("Load JSON first");
const container = document.getElementById("network");
container.innerHTML = "";
container.style.overflowY = "auto";
const sorted = [...dataset.domains]
.sort((a, b) => b.outbound_domains.length - a.outbound_domains.length);
const max = sorted[0]?.outbound_domains.length || 1;
let html = "";
for (const d of sorted) {
const val = d.outbound_domains.length;
const width = (val / max) * 600;
html += `
<div style="display:flex;align-items:center;margin:6px;color:white;">
<div style="width:240px;font-size:12px;">${d.domain}</div>
<div style="height:18px;width:${width}px;background:#4a90e2;"></div>
<div style="margin-left:10px;">${val}</div>
</div>`;
}
container.innerHTML = html;
}
/* ---------------- SEARCH MODE ---------------- */
function searchNode() {
if (!dataset) return alert("Load JSON first");
const q = prompt("Enter domain:");
if (!q) return;
const key = normalize(q);
const node = nodeMap.get(key);
if (!node) {
alert("Not found");
return;
}
let inbound = 0;
for (const d of dataset.domains) {
for (const o of d.outbound_domains) {
if (normalize(o) === key) inbound++;
}
}
alert(
"Domain: " + node.domain +
"\nOutbound: " + node.outbound_domains.length +
"\nInbound: " + inbound
);
}
/* ---------------- RENDER GRAPH ---------------- */
function renderGraph(nodes, edges) {
if (network) network.destroy();
const container = document.getElementById("network");
network = new vis.Network(
container,
{
nodes: new vis.DataSet(nodes),
edges: new vis.DataSet(edges)
},
{
autoResize: true,
physics: false,
nodes: {
shape: "dot",
scaling: { min: 3, max: 18 },
font: { color: "#fff", size: 10 }
},
edges: {
width: 0.25,
color: { color: "#666" }
},
layout: {
improvedLayout: false
}
}
);
}
</script>
</body>
</html>