-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (116 loc) · 4.33 KB
/
script.js
File metadata and controls
139 lines (116 loc) · 4.33 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
let reset, searchbtn, searchTerm;
const getMaxChars = () => {
const width = window.innerWidth || document.body.clientWidth;
let maxChars;
if (width < 414) maxChars = 90;
if (width >= 600 && width < 1400) maxChars = 130;
if (width >= 1400) maxChars = 180;
return maxChars;
};
const Search = async(searchTerm) => {
try {
this.searchTerm = searchTerm;
this.maxChars = getMaxChars();
// console.log(maxChars)
this.noOfResults = 20;
this.wiki = `https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=${this.searchTerm}&gsrlimit=${this.noOfResults}&prop=pageimages|extracts&exchars=${this.maxChars}&exintro&explaintext&exlimit=max&format=json&origin=*`
this.url = encodeURI(this.wiki);
this.res = await fetch(this.url);
this.data = await this.res.json()
this.dataObj = this.data.query.pages;
// console.log(dataObj)
this.dataArray = [];
for (let x in this.dataObj) {
this.dataArray.push(this.dataObj[x]);
}
return this.dataArray;
} catch (err) {
return { title: 'could not find any result.', failed: true }
}
// console.log(dataArray)
// dataArray.forEach(element => {
// console.log(element.title)
// });
}
// Search('aasd').then(data => {
// data.forEach(element => {
// console.log(element.thumbnail)
// });
// })
window.addEventListener('load', () => {
reset = document.getElementById('reset');
searchbtn = document.getElementById('search');
searchTerm = document.getElementById('searchTerm');
// user inputing, reset button appear
form = document.querySelector('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
searchbtn.click();
})
searchTerm.oninput = () => {
let resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = ''
reset.classList.add('showrst')
document.querySelectorAll('.initial').forEach(el => {
el.classList.remove('initial');
})
}
// user click reset button, input is cleared
reset.addEventListener('click', (e) => {
searchTerm.value = '';
reset.classList.remove('showrst')
});
// user click search button
searchbtn.addEventListener('click', () => {
let keywords = searchTerm.value.trim();
// reset.classList.remove('showrst');
displayResults(keywords);
})
})
//create div//
function createDiv(classname) {
div = document.createElement('div');
div.className = classname;
return div;
}
//display results
displayResults = async(keywords) => {
try {
let resultsDiv = document.getElementById('results');
let results = await Search(keywords);
// console.log(results)
if (!results.failed) {
results.forEach(element => {
// console.log(element)
// result = createDiv('result');
result = document.createElement('a');
result.className = 'result';
result.setAttribute('href', `https://en.wikipedia.org/?curid=${element.pageid}`);
// result.dataset.id = element.pageid;
title = createDiv('title');
title.textContent = element.title;
abstract = createDiv('abstract');
summary = createDiv('summary');
summary.textContent = element.extract;
if (element.thumbnail) {
imgDiv = createDiv('img');
img = document.createElement('img');
img.setAttribute('src', element.thumbnail.source);
imgDiv.appendChild(img);
abstract.appendChild(imgDiv)
}
//
abstract.appendChild(summary)
result.appendChild(title);
result.appendChild(abstract);
resultsDiv.appendChild(result);
});
} else {
error = createDiv('error');
error.textContent = results.title;
resultsDiv.appendChild(error);
}
} catch (err) {
console.log('err');
}
}