Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (111 loc) · 4.58 KB
/
Copy pathindex.js
File metadata and controls
129 lines (111 loc) · 4.58 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
var dragDrop = require('drag-drop/buffer')
var WebTorrent = require('webtorrent')
var es = require("event-stream")
var client = new WebTorrent()
var magnetUri = window.location.hash.split('#')[1];
if(magnetUri) {
client.add(magnetUri, function (torrent) {
// Got torrent metadata!
console.log('Torrent info hash:', torrent.infoHash)
var contentBlobs = {};
var htmlFiles = {};
var cssFiles = {};
var filesProcessedCounter = 0;
var processFiles = function() {
if(torrent.files.length == filesProcessedCounter) {
console.log('All file blobs processed, ready to transmute content files');
var indexCount = 0;
for(var fileName in htmlFiles){
var blob = htmlFiles[fileName];
if(fileName == 'index.html' || fileName == 'index.htm') {
indexCount = 1;
var reader = new FileReader();
reader.readAsText(blob);
reader.onload = function(e) {
var filtered_content = e.target.result;
global.target = e.target.result;
for(var k in contentBlobs){
console.info('WILL REPLACE', 'src="'+k+'"','src="'+contentBlobs[k]+'"');
filtered_content = filtered_content.replace('src="'+k+'"','src="'+contentBlobs[k]+'"');
}
for(var k in cssFiles) {
console.info('WILL REPLACE', 'href="'+k+'"','href="'+cssFiles[k]+'"');
filtered_content = filtered_content.replace('href="'+k+'"','href="'+global.URL.createObjectURL(cssFiles[k])+'"');
}
for(var k in htmlFiles) {
console.info('WILL REPLACE', 'href="'+k+'"','href="'+htmlFiles[k]+'"');
filtered_content = filtered_content.replace('href="'+k+'"','href="'+global.URL.createObjectURL(htmlFiles[k])+'"');
}
console.log('AFTER', filtered_content);
global.targetAfter = filtered_content;
var doc = document.open();
doc.write(filtered_content);
doc.close();
}
}
}
if(indexCount == 0) {
for(var fileName in contentBlobs){
// Add a link to the page
var a = document.createElement('a')
a.download = fileName
a.href = contentBlobs[fileName];
a.textContent = 'Download ' + fileName
document.body.appendChild(a)
}
}
}
}
torrent.files.forEach(function (file) {
console.log('HERE IS DIE FILE!!');
console.log(file);
window.thefile = file;
var fileExtension = file.name.split('.').pop();
switch(fileExtension) {
case 'htm':
case 'html': //Filter HTML for src= and href= links
//Filter for search !!
console.log('FOUND HTML!');
file.getBlob(function (err, blob) {
if (err) throw err
htmlFiles[file.name] = blob;
filesProcessedCounter++;
processFiles();
});
processFiles();
break;
case 'css': //Filter CSS for links to blobbify
console.log('FOUND CSS!');
file.getBlob(function (err, blob) {
if (err) throw err
cssFiles[file.name] = blob;
filesProcessedCounter++;
processFiles();
});
break;
default:
console.log('FOUND DEFAULT CONTENT!');
//Just get blob and blob URL and add to list of blob URL's
file.getBlobURL(function (err, url) {
if (err) throw err
contentBlobs[file.name] = url;
filesProcessedCounter++;
processFiles();
});
}
});
});
}
else {
document.getElementById('info').innerHTML += "<b>Drop your file(s) here and share it peer-2-peer.</b><br><br>If bundle contains index.html, it will be redered as a webpage (with images, stylesheets and javascript)<br>Folders not yet supported, keep everything at a 'root'-level.<br>Complex MVC frameworks, such as AngularJS, somewhat supported.";
document.getElementById('example').innerHTML = "<i>Example:</i><br><img src='example.png'>"
// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', function (files) {
client.seed(files, function onTorrent (torrent) {
// Client is seeding the file!
var url = window.location + '#' + torrent.infoHash;
document.getElementById('example').innerHTML = "";
document.getElementById('link').innerHTML += "<br>Share link: <a href='" + url + "'>"+url+"</a>";
})
})
}