-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
598 lines (467 loc) · 21.6 KB
/
index.html
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>NFCA Gateway</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="" id="description_id">
<meta name="keywords" content="" id="keywords_id">
<meta name="author" content="" id="author_id">
<link rel="icon" type="image/x-icon" href="" id="favIcon_id">
<link rel="canonical" href="" id="canonical_id"/>
<!-- ethers.js -->
<script id="eth_js" type="text/javascript"></script>
<!-- qrcode.js -->
<script id="qr_js" type="text/javascript"></script>
<!-- contract.js -->
<script id="contract_js" type="text/javascript"></script>
<script>
/*
Gateway Info
https://nfca.cc --> To get alias
https://github.com/HermesAteneo/NFCA-Gateway --> Gateway repository
https://hermesateneo.medium.com --> by Hermes Ateneo & bros
///////////////////////////////////////////////////////////////////////////*/
//Variables
//////////////////////////////////////////////////////
var ALIAS
var FF
var ACCESS
var FNETWORK
var CID
var TYPE
var KEYWORDS
var DESCRIPTION
var netData
var abi
var ContractScan
var BASEURL = './'
var ChainId = 137 //Polygon mainnet
var URL_Pos = 2; //url position of gateway dir // 2-> Defaul. Local and first subdirectory // 1-> Subdomains // 3-> second subdirectory ...
var microTime = Date.now()
//Inline Set Alias
//////////////////////////////////////////////////////
//Set default Favicon
var iconBaseURL = BASEURL + 'src/icon/nfca.icon.white.squared.svg' + '?v=' + microTime
document.getElementById("favIcon_id").setAttribute("href", iconBaseURL )
//Load dynamic JS
eth_js.src = BASEURL + 'src/js/ethers-5.6.1.umd.min.js' + '?v=' + microTime;
qr_js.src = BASEURL + 'src/js/awesome-qr.js' + '?v=' + microTime;
contract_js.src = BASEURL + 'src/js/nfca.contract.js' + '?v=' + microTime;
//On Window Load
//////////////////////////////////////////////////////
window.onload = function() {
//Print version info
var nfca_version = "v."+ jsGatewayVersion +" Alpha";
var net_version = "mintable mainnet / " + nfca_version;
console.log("NFCA Gateway / " + net_version );
GetAliasFromURL()
//Inject Version string dinamically
var PrintVersion = document.getElementsByClassName('nfca-version');
for (var i = 0; i < PrintVersion.length; i++) {
PrintVersion[i].innerHTML = net_version;
}
//Show Gateway Home
if( ALIAS =="" ){ ContentError('home'); CheckGatewayVersion(true); return;}
//Inject ALIAS string dinamically
var PrintAlias = document.getElementsByClassName('print-alias');
for (var i = 0; i < PrintAlias.length; i++) {
PrintAlias[i].innerHTML = ALIAS;
}
Connect2Blockchain(jsNetData, jsABI)
//Connect to Blockchain
//////////////////////////////////////////////////////
var ConnError = 0;
function Connect2Blockchain(netData, abi){
NetData = FindNetData(netData, ChainId)
console.log("Chain & Contract: " + JSON.stringify( NetData ) )
ContractAddress = NetData[0].contract
ChainRPC = NetData[0].rpc
var provider = new ethers.providers.JsonRpcProvider(ChainRPC);
var contract = new ethers.Contract(ContractAddress, abi, provider);
ContractScan = NetData[0].scan + "/address/" + NetData[0].contract
contract.AliasIndex(ALIAS).then( index => {
index = parseInt(index)
console.log("Alias --> " + ALIAS + " - index--> " + index )
if(index == 0 && ALIAS != '0') { ContentError('404'); if(ConnError < 1){ CheckGatewayVersion(false); ConnError++; return; } return; }
//Get Alias data
contract.RecordsA(index).then( _record => {
CID = _record.CID
TYPE = _record.Type
KEYWORDS = _record.KeyWords
DESCRIPTION = _record.Description
if(TYPE == 'ipfs'){ FNETWORK= 'https://ipfs.io/ipfs/'; } //CID of ipfs network
if(TYPE == 'sia') { FNETWORK= 'https://siasky.net/'; } //CID of sia network
if(TYPE == 'arweave') { FNETWORK= 'https://arweave.net/'; } //CID of arweave network
if(TYPE == 'swarm') { FNETWORK= 'https://swarm-gateways.net/bzz:/'; } //CID of swarm network
if(TYPE == 'uri') { FNETWORK= ''; } //CID is URL
if(TYPE == 'redirect') {FNETWORK= ''; } //CID is URL
if(TYPE == 'html') { FNETWORK= ''; } //CID is HTML
if(TYPE == 'text') { FNETWORK= ''; } //CID is text
if(TYPE == 'qr') { FNETWORK= ''; } //CID returned as QR Code
if(TYPE == 'plain') { var URL = 'https://nfca.cc/' + ALIAS; document.location = URL; return; } //CID returned as plain text (only possible in nfca.cc server)
LoadAliasContent();
CheckGatewayVersion(true);
});
}).catch(error => { if(ConnError < 1){ CheckGatewayVersion(false); ConnError++; } ContentError('blockchain-error'); });
}//Connect2Blockchain
//Functions onload
//////////////////////////////////////////////////////
//Check Gateway Version
//////////////////////////////////////////////////////
function CheckGatewayVersion(ok){
var urlrepoinfo = 'https://raw.githubusercontent.com/HermesAteneo/NFCA-Gateway/main/src/js/nfca.contract.js';
fetch(urlrepoinfo).then(response => {
if (!response.ok){ return; }
else{ return response.text(); }//To then as data
})
.then(data => { eval(data)
if( jsGatewayVersion > this.jsGatewayVersion ){
console.log("*\nThere is a higher gateway version "+jsGatewayVersion+"\nUpdate from https://github.com/HermesAteneo/NFCA-Gateway\n*")
//Set data from online version to avoid outdated data
if(!ok){ Connect2Blockchain(jsNetData, jsABI) }
}
}).catch(error => { return; });
}
}//window.onload
//////////////////////////////////////////////////////
//Functions inline
//////////////////////////////////////////////////////
//Request Alias from URL
//////////////////////////////////////////////////////
function GetAliasFromURL(){
FF = ""; //Folder Files
//Get url path and params
var hostname = window.location.hostname //get hostname
var urlpathname = window.location.pathname.replace(new RegExp('////|///|//','g'), "/") //get alias as directory
var urlparam = window.location.search;
//Get alias as param from var ?
if(urlparam.indexOf('?') != -1){
var urlpathname = urlparam.split('?')[1]
var parts = urlpathname.split('/')
ALIAS = decodeURIComponent( parts[0] )
for( x = parts.length - URL_Pos + 1; x>=1; x--){
if( parts[parts.length - x] != undefined ){
FF+= "/" + parts[parts.length - x];
}
}
}
//Get alias from directory name
else{
var parts = urlpathname.split('/')
ALIAS = decodeURIComponent( parts[URL_Pos] )
for( x = parts.length - URL_Pos -1 ; x>=1; x--){
if( parts[parts.length - x] != undefined ){
FF+= "/" + parts[parts.length - x];
}
}
}
//Control of no alias
if( ALIAS == undefined || ALIAS == "undefined" || ALIAS == null || ALIAS == "null" ){ ALIAS = ""; }
if( urlpathname.indexOf('/index.html') != -1 ){ ACCESS="local"; ALIAS = ""; }
if( window.location.pathname.indexOf('/index.html') != -1 || hostname.indexOf('arweave.') != -1 ){ ACCESS="local"; }
else if( hostname.indexOf('ipfs.') != -1 || hostname.indexOf('siasky.') != -1 || hostname.indexOf('skynet') != -1 || hostname.indexOf('fleek.') != -1 || hostname.indexOf('github.') != -1){ ACCESS="dserver"; }
else{ ACCESS="cserver"; }
console.log( "ACCESS: " + ACCESS);
console.log( "ALIAS: " + ALIAS);
console.log( "FF: " + FF);
if( ALIAS !="" ){ document.title = ALIAS; }
//Set canonical//////////////////
var CanonicalURL = 'https://nfca.cc/' + ALIAS
document.getElementById("canonical_id").setAttribute("href", CanonicalURL )
}
//Load Alias Content
//////////////////////////////////////////////////////
function LoadAliasContent(){
var URL = FNETWORK + CID + FF;
//If type is an URL need to make a redirection to avoid CORS blocks//
if(TYPE == 'redirect' ){
var URL = FNETWORK + CID + FF;
document.location = URL; return;
}
//If type is html, load the code
if(TYPE == 'html' ){
document.getElementById('html_content').innerHTML = '<style>body{margin: 0;}</style>';
document.getElementById('html_content').innerHTML += CID;
document.title = decodeURIComponent( getTitle(CID) );
//ContentLoaded();
return;
}
//If type is text, load the code
if(TYPE == 'text' ){
document.getElementById('qr_block').remove()
document.getElementById('obj').remove()
document.getElementById('text_content').innerHTML = '<h1>' + ALIAS + '</h1>' + CID;
ContentLoaded();
return;
}
//If type is qr, create a QRCode of CID
if(TYPE == 'qr' ){
document.getElementById('text_block').remove()
document.getElementById('obj').remove()
new AwesomeQR.AwesomeQR({ text: CID, size: 300, margin:0,
logoImage: BASEURL + 'src/icon/nfca.icon.black.rounded.svg' + '?v=' + microTime,
logoMargin: 5
})
.draw().then(dataURL => { document.getElementById('qr_content').innerHTML = "<img src='"+dataURL+"'/>"; });
ContentLoaded();
return;
}
//Set Meta content/////////
document.getElementById("keywords_id").setAttribute("content", KEYWORDS );
document.getElementById("description_id").setAttribute("content", DESCRIPTION );
//If type is ipfs,arweave, sia ...
console.log("URL: " + URL)
fetch(URL).then(response => {
console.log("Response.ok: " + response.ok );
console.log("Response.status: " + response.status );
console.log("Response.statusText : " + response.statusText );
if (response.ok){ return response.text(); }
ContentError(response.status); return;
})
.then(data => { ShowIPFS( data ); })
.catch((error) => { ContentError(''); return; });
function ShowIPFS(data) {
var ContentTitle = decodeURIComponent( getTitle(data) );
var httpErrors = ["400", "401", "403", "404", "408", "410", "500", "502", "503", "504", "This page cannot be found, yet", "invalid ipfs path: invalid path"];
var wasError = false;
httpErrors.forEach(function(error) {
if(ContentTitle.indexOf(error) !== -1){ console.log("errors " + error);
wasError=error; return;
}
});
//If title include error/////////////
if(wasError!=false){ ContentError(wasError); return; }
//Ok, load content /////////////////
else{
//Load title//////////////
document.title = ContentTitle;
//Load Favicon///////////
var favIconURL = decodeURIComponent( getFavIcon(data) );
if( favIconURL.includes("http://") || favIconURL.includes("https://") ){
document.getElementById("favIcon_id").setAttribute("href", favIconURL );
}
else if( favIconURL.includes("data:") || favIconURL.includes("/src/") ){
document.getElementById("favIcon_id").setAttribute("href", favIconURL );
}
else{
document.getElementById("favIcon_id").setAttribute("href", FNETWORK + CID + '/' + favIconURL );
}
//Load content////////////
obj.src = URL;
obj.onload = function(){ ContentLoaded(); document.getElementById('no_obj').style.display = 'none'; }//obj onLoad
}
}
}//function LoadAliasContent
//Other Functions
//////////////////////////////////////////////////////
function getTitle(e){
var t=/<title [^>]+>(.*?)<\/title>/g.exec(e);if(t&&t[1])return t[1].replace(/<\/?title>/g," ").replace(/\s+/," ");
if(t == undefined){
var t=/<title>(.*?)<\/title>/g.exec(e);if(t&&t[1])return t[1].replace(/<\/?title>/g," ").replace(/\s+/," ");
}
if(t == undefined){ return ALIAS; }
}
function getFavIcon(e){
var rel=/<link[^>]*?rel=(["\'])?((?:.(?!\1|>))*.?)\1?/g.exec(e);
if(rel == undefined){ return iconBaseURL; }
if(rel[2]=="icon" || rel[2]=="shortcut icon"){
var f=/<link[^>]*?href=(["\'])?((?:.(?!\1|>))*.?)\1?/g.exec(e);
if(f == undefined){ return iconBaseURL; }
return f[2];
}
return iconBaseURL;
}
function ContentLoaded(){
document.getElementById('error-layer').remove()
document.getElementById('loader-layer').remove()
}
function ContentError(status){
document.getElementById('no_obj').style.display = 'none';
document.getElementById('loader-layer').style.display = 'none';
document.getElementById('error-layer').style.display = 'block';
document.getElementById('error-status').innerHTML = status;
if(status == "blockchain-error"){
document.getElementById('error-layer').style.display = 'none';
document.getElementById('blockchain-layer').style.display = 'block';
}
if(status == "home"){
document.getElementById('gateway_tx').innerHTML = 'NFCA Gateway';
document.getElementById('gateway_sl').innerHTML = '<b style="font-weight: 100;">Decentralized censorship resistant alias & content</b>';
document.getElementById('gateway_get').innerHTML = '$ Get an alias $';
}
if(ACCESS=="local"){ document.getElementById('gateway_ex').innerHTML = '<a class="tx-se pointer grow" href="./index.html?dalias">DAlias</a> <br> NFCA search engine'; }
if(ACCESS=="cserver"){ document.getElementById('gateway_ex').innerHTML = '<a class="tx-se pointer grow" href="./dalias">DAlias</a> <br> NFCA search engine'; }
if(ACCESS=="dserver"){ document.getElementById('gateway_ex').innerHTML = '<a class="tx-se pointer grow" href="?dalias">DAlias</a> <br> NFCA search engine'; }
}
function FindNetData(_data, _network) {
var NetworksArray = _data.networks;
for (var i = 0; i < NetworksArray.length; i++) {
if (NetworksArray[i].id == _network) {
return( NetworksArray[i].netdata );
}
}
}
function ToHome(){
window.location.href = "https://nfca.cc"
}
function ToRegister(){
var _link= ""
if( ALIAS != "" ) { _link = "###"+ALIAS }
window.location.href = "https://nfca.cc/get-alias" + _link
}
function ToDev(){
var _link= ""
if( ACCESS == "local" ) { _link = "/index.html" }
window.location.href = "./dev" + _link
}
function ToScan(){
window.location.href = ContractScan
}
</script>
</head>
<body id="html_content" >
<div id="no_obj">
<div id="qr_block" class="expanded-html flex-container">
<div class="flex-box">
<div id="qr_content"></div>
</div>
</div>
<div id="text_block" class="expanded-html flex-container-top">
<div class="flex-box">
<div id="text_content"></div>
</div>
</div>
</div>
<iframe id="obj" frameBorder="0"></iframe>
<div id="blockchain-layer" style="display: none;">
<div class="loader flex-container">
<div class="flex-box">
<div class="nfca-slash-logo"><img src="./src/icon/nfca.icon.black.rounded.svg" width="100%"></div>
</div>
</div>
<div class="loader flex-container">
<div class="flex-box">
<h2>Blockchain connection error </h2>
<p class="pointer tx-pink"> NFCA smart contract disconnection </p>
<p class="pointer grow" onclick="ToScan()"> See in blockchain scanner </p>
</div>
</div>
</div>
<div id="loader-layer">
<div class="loader flex-container">
<div class="flex-box">
<div class="nfca-slash-logo"><img src="./src/icon/nfca.icon.black.rounded.svg" width="100%"></div>
</div>
</div>
<div class="loader flex-container">
<div class="flex-box">
<h2>Loading content of </h2>
<h1 class="print-alias tx-gold tx-thin"></h1>
</div>
</div>
</div>
<div id="error-layer" style="display: none;">
<div class="loader flex-container">
<div class="flex-box">
<div class="tx-right"><span class="tx-thin bg-fill bg-black rounded tx-green b nfca-version" style="font-size: small;"></span></div>
<h1 id="gateway_tx" class="bg-black bg-fill rounded nfca-anim tx-pink"><span class="">Error <span id="error-status">404</span> </span> <span class="tx-thin"> loading content of</span> </h1>
<h2 id="gateway_sl" class="bg-black bg-fill rounded-right">
<div class="f-left" style="padding-top:2.5px; padding-right:10px;">
<svg width="30" height="30" viewBox="0 0 30 30" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="30" height="30" fill="black"/>
<path d="M17.4385 3H21L12.5455 27H9L17.4385 3Z" fill="white"/>
</svg>
</div>
<span class="print-alias tx-gold f-left"></span> <span class="tx-thin"> content not found</span>
</h2>
<p id="gateway_get" class="pointer grow tx-gateway-get" onclick="ToRegister()" style="margin-top:30px;"> $ Try to get this alias $ </p>
<hr style="height:1px;border:none;color:#555;background-color:#555; width:60px; margin-top:30px;" />
<p id="gateway_ex" class="pointer grow"></p>
</div>
</div>
</div>
<style>
body{ font-family: Segoe, "Segoe UI", "Verdana", "Ubuntu", "Georgia"; margin: 0; overflow-y:hidden;}
#obj{ width: 100%; height: 100vh; background-color: #fff; }
body, #loader-layer, .loader{ overflow: hidden; }
.expanded{ position: absolute; top:0; left:0; right:0; bottom:0; }
.expanded-html{ overflow-y: auto; position: absolute; width: 100%; height: 100%; margin: auto; }
#text_content h1{ text-align: center;}
#text_content{ max-width: 500px; margin: auto; text-align: justify;}
.loader{ position: absolute; top:0; left:0; right:0; bottom:0; color: #fff; padding: auto; background-color: rgba(0,0,0,0.85); }
.loader h1{ padding: auto;}
.flex-container-top{
display: flex;
flex-direction: column;
align-items: center;
}
.flex-container{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.flex-box {
margin: 5px;
text-align: center;
}
.nfca-anim{position: relative; opacity: 0.8; animation: animatedBox 6s linear infinite;}
@keyframes animatedBox {
0% { transform: scale(1); }
50% { transform: scale(1.1); opacity: 0.99; }
100% { transform: scale(1); }
}
.nfca-slash-logo img{ filter: invert(0); opacity: 0.2; width: 50vw; height: 50vh; animation: animatedLogoImg 6s linear infinite;}
.nfca-slash-logo { position: relative; }
@keyframes animatedLogoImg {
0% { transform: scale(1); }
50% { transform: scale(1.1); opacity: 0.99; }
95% { transform: rotate(0deg);}
100% { transform: scale(1); transform: rotate(180deg); }
}
.tx-gold { color: #ffb700; }
.tx-pink { color: #FF41B4; }
.tx-thin { font-weight: 300;}
.tx-left{ text-align: left; }
.tx-right{ text-align: right; }
.tx-purple { color: #A463F2; }
.tx-green { color: #19A974; }
.tx-black { color: #000; }
.tx-se{
background: -webkit-linear-gradient(left,#4285F4, #DB4437, #F4B400, #4285F4, #0F9D58, #DB4437);
background: -o-linear-gradient(right, #4285F4, #DB4437, #F4B400, #4285F4, #0F9D58, #DB4437);
background: -moz-linear-gradient(right, #4285F4, #DB4437, #F4B400, #4285F4, #0F9D58, #DB4437);
background: linear-gradient(to right, #4285F4, #DB4437, #F4B400, #4285F4, #0F9D58, #DB4437);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: bold;
font-size: 50px;
text-decoration: none;
}
.tx-gateway-get{font-weight: 700;}
.bg-gold { background-color: #ffb700; }
.bg-pink { background-color: #FF41B4; }
.bg-purple { background-color: #5E2CA5; }
.bg-green { background-color: #19A974; }
.bg-black { background-color: #000; }
.bg-fill { padding: 4px 20px 7px 20px; }
.rounded{ border-radius: 100px !important; }
.rounded-left{
border-top-left-radius: 100px !important;
border-bottom-left-radius: 100px !important;
}
.rounded-right{
border-top-right-radius: 100px !important;
border-bottom-right-radius: 100px !important;
}
.grow { -moz-osx-font-smoothing: grayscale; -webkit-backface-visibility: hidden; backface-visibility: hidden; -webkit-transform: translateZ( 0 ); transform: translateZ( 0 ); transition: -webkit-transform .25s ease-out; transition: transform .25s ease-out; transition: transform .25s ease-out, -webkit-transform .25s ease-out; }
.grow:hover, .grow:focus { -webkit-transform: scale( 1.05 ); transform: scale( 1.05 ); }
.grow:active { -webkit-transform: scale( .90 ); transform: scale( .90 ); }
.f-left{float:left;}
.f-right{float:right;}
.b{ font-weight: bold;}
.pointer{ cursor: pointer;}
</style>
</body>
</html>