function getBilibiliHeaders(cookie) {
return new Headers({
"connection": "keep-alive",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
"referer": "https://www.bilibili.com/",
"Cookie": "SESSDATA=" + cookie.sessdata + "; bili_jct=" + cookie.bilijct + "; DedeUserID=" + cookie.dedeuserid,
});
}
function postBilibiliHeaders(cookie) {
return new Headers({
"connection": "keep-alive",
"referer": "https://www.bilibili.com/",
"accept": "application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
"charset": "UTF-8",
"Cookie": "SESSDATA=" + cookie.sessdata + "; bili_jct=" + cookie.bilijct + "; DedeUserID=" + cookie.dedeuserid,
});
}
export function get_bilibili(url, cookie) {
return fetch(url, {
method: "GET",
credentials: "include",
headers: getBilibiliHeaders(cookie),
})
.then(response => response.json())
.then(data => {
if (data.code != 0) {
throw new Error(
"Error when processing data, received data.code!=0. Check the result of the get request manually?",
);
}
return data.data;
});
}
function post_encode(params) {
return Object.keys(params).map(key => encodeURIComponent(key) + "=" + encodeURIComponent(params[key])).join("&");
}
export function post_bilibili(url, post_data, cookie) {
return fetch(url, {
method: "POST",
credentials: "include",
headers: postBilibiliHeaders(cookie),
body: post_encode(post_data),
})
.then(response => response.json())
.then(data => {
if (data.code != 0) {
throw new Error(
"Error when processing data, received data.code!=0. Check the result of the post request manually?",
);
}
return true;
});
}