-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (100 loc) · 3.16 KB
/
app.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
let queryDatas = new XMLHttpRequest();
queryDatas.open('GET','datas');
queryDatas.onreadystatechange = function(){
if((queryDatas.readyState > 3) && (queryDatas.status == 200)){
parseResp(queryDatas.responseText);
}
}
queryDatas.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
queryDatas.send();
function shuffle(array) {
let currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function parseResp(resp){
let favs = resp.split(/\n/g);
document.querySelector('.total').innerHTML = ((favs.length - 1) / 3);
document.querySelector('.showed').innerHTML = ((favs.length - 1) / 3);
let favsCleaned = [];
for (i = 0; i + 1 < favs.length; i = (i+3) ) {
let urlArray = favs[(i+1)].split('/');
let siteName = '';
if(urlArray[2]!= 'feedproxy.google.com'){
siteName = urlArray[2];
}else{
siteName = urlArray[4];
}
favsCleaned.push({
'title' : favs[i],
'url' : favs[(i+1)],
'sitename' : siteName
});
}
favsCleaned = shuffle(favsCleaned);
createHtml(favsCleaned);
}
function createHtml(fc){
let main = document.querySelector('main');
for (i = 0; i < fc.length; i++){
if(fc[i].url){
let newItem = '';
newItem += '<a href="';
newItem += fc[i].url;
newItem += '">';
newItem += '<div class="img"><img id="img_' + i + '" src="icon.svg" /></div>';
newItem += '<div class="text"><p class="site-name">' + fc[i].sitename + '</p><p class="title">' + fc[i].title + '</p></div>';
newItem += '</a>';
main.insertAdjacentHTML('beforeend',newItem);
fetchIcon(i, fc[i].url);
}
}
}
function fetchIcon(i, url){
let urlArray = url.split('/');
let urlFavicon = urlArray[0] + '//' + urlArray[2] + '/favicon.ico';
let img = new Image();
img.onload = function(){
let selector = '#img_' + i;
document.querySelector(selector).src = urlFavicon;
}
img.src = urlFavicon;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
let searchField = document.querySelector('#search');
searchField.value = '';
function searchIn(e){
if(e.key == 'Enter'){
window.location = 'https://duckduckgo.com/?q=' + this.value;
}else{
let titles = document.querySelectorAll('.title');
let y = 0;
for(i = 0; i < titles.length; i++){
let title = titles[i].innerHTML.toLowerCase();
if(title.indexOf(this.value.toLowerCase()) >= 0){
titles[i].parentNode.parentNode.style.display = 'inherit';
y++;
}else{
titles[i].parentNode.parentNode.style.display = 'none';
}
}
if(!this.value){
let links = document.querySelectorAll('main a');
for(i = 0; i < titles.length; i++){
links[i].style.display = 'inherit';
}
y = titles.length;
}
document.querySelector('.showed').innerHTML = y;
}
}
searchField.addEventListener('keyup', searchIn);
searchField.focus();