-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
111 lines (90 loc) · 3.17 KB
/
generate.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
function makeid() {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function processInput(bar, width, height){
//some lovable regex
var patternVideo = new RegExp(".+/av[0-9]{5,}"); //是视频
var patternVideoShort = new RegExp(".*acg\.tv\/[0-9]{5,}"); //是短链接式视频(形如“acg.tv/xxxxx”)
var isvideo = patternVideo.test(bar);
var issvideo = patternVideoShort.test(bar);
//to determine whether the regex ran successfully
if (isvideo === true || issvideo === true) { //是视频
var svc = "video";
var uid = bar.match(/\d{5,}/);
}
else { //gg
var svc = '';
var uid = '';
}
if (svc && uid) {
console.log(svc);
console.log(uid);
}
if (svc.replace(/(^\s*)|(\s*$)/g, "").length !=0 && uid[0].length !=0) {
//regex to match clips number (which p)
var patternVideoP = new RegExp(/\?p=(\d+)/g);
var whichP = patternVideoP.exec(bar);
if (whichP) {
arraynum = whichP[1];
} else {
arraynum = "1";
}
////debugging
console.log(whichP);
console.log(arraynum);
////debugging
var requestURL = 'api.php?aid=' + uid + '&p=' + arraynum;
var idn = makeid();
var idcf = idn;
//autoselect
if (document.getElementById("height").focus) {
document.getElementById("height").select();
document.getElementById("width").value = "";
}
if (document.getElementById("width").focus) {
document.getElementById("width").select();
document.getElementById("height").value = "";
}
if (document.getElementById("codebar").focus) {
document.getElementById("codebar").select();
}
//process width and height
if (height == "" && width) {
var height = width/9*6;
document.getElementById("height").value = height;
} else if (width == "" && height) {
var width = height/6*9;
document.getElementById("width").value = width;
} else if (width == "" && height == "") {
var width = 846;
var height = 568;
document.getElementById("height").value = height;
document.getElementById("width").value = width;
}
console.log("height: " + height);
console.log("width: " + width);
//make http requests.
//referring to https://www.kirupa.com/html5/making_http_requests_js.htm.
var makeHttpRequest = new XMLHttpRequest();
makeHttpRequest.open('GET', requestURL, true);
makeHttpRequest.send();
makeHttpRequest.onreadystatechange = processRequest;
function processRequest(e) {
if (makeHttpRequest.readyState == 4 && makeHttpRequest.status == 200) {
var cid = JSON.parse(makeHttpRequest.responseText);
console.log(cid.cid);
var embedCode = "<iframe id=" + idcf + " src=\"//player.bilibili.com/player.html?aid=" + uid + "&cid=" + cid.cid + "&page=" + arraynum + "\" scrolling=\"no\" border=\"0\" frameborder=\"no\" framespacing=\"0\" allowfullscreen=\"true\"" + " style=\"width: " + width + "px; height: " + height + "px; max-width: 100%" + "\"> </iframe>";
document.getElementById("codebar").value = embedCode;
document.getElementById("preview").innerHTML = embedCode;
}
}
}
else {
var embedCode = "Invalid URL. Try again. Bangumis are not supported by far.";
document.getElementById("codebar").value = embedCode;
}
}