-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlewd.js
83 lines (70 loc) · 2.44 KB
/
lewd.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
var baseSearchUrl = 'https://trace.moe/?url=';
var nekoImageEndpoint = 'https://nekos.life/api/v2/img/';
var nekoEndpoints = 'https://nekos.life/api/v2/endpoints';
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
getJSON(nekoEndpoints,
function(err, data) {
if (err == null) {
var content = data[11];
var index = content.indexOf("<") + 1;
var sub = content.substring(index).replace(">", "").replace("'8ball',", "").replace("'nekoapi_v3.1',", "").replace("'v3',", "");
var splitted = sub.split(',');
var sorted = splitted.map(x => x.trim()).sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
var picker = document.querySelector(".picker");
sorted.forEach(function(element){
var option = document.createElement("option");
option.text = element.replace("'", "").replace("'", "");
picker.add(option);
})
}
});
document.addEventListener('keydown', handleKey);
document.addEventListener("click", nextImage);
function nextImage(e) {
if(e.target.id != "pick" && e.target.id != "source") {
var picker = document.querySelector(".picker");
var value = picker.options[picker.selectedIndex].value;
showNextImage(picker, value);
}
}
function handleKey(e) {
var picker = document.querySelector(".picker");
var value = picker.options[picker.selectedIndex].value;
if((e.key == "ArrowUp" || e.key == "w" || e.key == "W") && picker.selectedIndex != 0) {
picker.selectedIndex = picker.selectedIndex - 1;
e.returnValue = false;
}
if((e.key == "ArrowDown" || e.key == "s" || e.key == "S") && picker.selectedIndex != picker.length-1) {
picker.selectedIndex = picker.selectedIndex +1 ;
e.returnValue = false;
}
if(e.key == "ArrowRight" || e.key == "ArrowLeft" || e.key == "d" || e.key == "D" || e.key == "A" || e.key == "a") {
showNextImage(picker, value);
}
}
function showNextImage(picker, value){
var link = document.querySelector(".btn");
getJSON(nekoImageEndpoint + value,
function(err, data) {
if (err == null) {
var img = document.querySelector('.img');
img.src = data.url;
link.href = baseSearchUrl + data.url;
}
});
}