-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
151 lines (135 loc) · 4.87 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="utf-8">
<title>Turkish Dictionary</title>
<style>
dd,
dl,
li {
margin-left: 0px;
padding-left: 0px;
}
dt {
color: blue;
}
.trdict-prop {
color: orange;
font-style: italic;
}
#trdict-entry-list {
list-style-type: upper-roman;
}
ol,
ul,
dd,
dt {
text-align: left;
}
ol,
ul {
padding-left: 20px;
}
.trdict-example {
font-style: italic;
}
.error {
color: red;
}
</style>
</head>
<body>
<form method="GET" action="">
<input id="search" name="search" type="search" autofocus>
<input type="submit" value="Ara">
</form>
<output id="output"></output>
<script>
const outputElement = document.getElementById("output");
function getSearch() {
const location = new URL(window.location);
const search = location.searchParams.get("search");
console.log('search=', search);
return search;
}
function showResponse(response) {
if (response.error) {
const error = document.createElement('span');
error.classList.add("error");
error.textContent = response.error;
outputElement.appendChild(error);
return;
}
const entries = response;
const entryList = document.createElement("ul");
entryList.classList.add("trdict-entry-list");
for (const entry of entries) {
console.log('entry = ', entry);
const entryLi = document.createElement("li");
const dl = document.createElement("dl");
const dt = document.createElement("dt");
dt.textContent = entry['madde'];
dl.appendChild(dt);
const dd = document.createElement("dd");
dl.appendChild(dd);
const ol = document.createElement("ol");
dl.appendChild(ol);
const definitionList = entry["anlamlarListe"]
for (const definition of definitionList) {
if (!("anlam" in definition) || !definition["anlam"]) {
continue
}
const definitionLi = document.createElement("li");
const definitionText = definition["anlam"];
if (definition["ozelliklerListe"]) {
const properties = definition["ozelliklerListe"]
const propsElement = document.createElement("span");
propsElement.classList.add("trdict-prop");
for (const [i, prop] of Object.entries(properties)) {
if ("tam_adi" in prop) {
const propText = prop['tam_adi'];
propsElement.textContent += propText;
if (i < properties.length - 1) {
propsElement.textContent += ', '
}
}
}
definitionLi.append(propsElement, ' ');
}
definitionLi.append(definitionText);
ol.appendChild(definitionLi);
let examples = definition['orneklerListe'];
if (!examples) {
examples = [];
}
for (const example of examples) {
if (example['ornek']) {
const exampleElement = document.createElement("span");
exampleElement.classList.add("trdict-example");
exampleElement.textContent = example['ornek'];
ol.append('"', exampleElement, '"');
}
}
}
entryLi.appendChild(dl);
entryList.appendChild(entryLi);
}
outputElement.appendChild(entryList);
}
async function queryWord(word) {
const response = await fetch('https://sozluk.gov.tr/gts?ara=' + word);
const json = await response.json();
console.log(json);
return json;
}
async function lookupWord() {
const search = getSearch();
if (!search) return;
const result = await queryWord(search);
showResponse(result);
}
lookupWord();
</script>
</body>
</html>