mirror of
https://github.com/VueTubeApp/VueTube
synced 2025-01-06 23:51:13 +00:00
refactor: make string conversion more error-proof
This commit is contained in:
parent
1ea6f6bdfb
commit
3030101069
1 changed files with 17 additions and 1 deletions
|
@ -15,6 +15,21 @@ function logger(func, data, isError = false) {
|
|||
});
|
||||
}
|
||||
|
||||
function getEncoding(contentType) {
|
||||
const re = /charset=([^()<>@,;:\"/[\]?.=\s]*)/i;
|
||||
const content = re.exec(contentType);
|
||||
console.log(content);
|
||||
if (!content || content[1].toLowerCase() == "utf-8") {
|
||||
return "utf8";
|
||||
}
|
||||
if (content[1].toLowerCase() == "iso-8859-1") {
|
||||
return "latin1";
|
||||
}
|
||||
if (content[1].toLowerCase() == "utf16le") {
|
||||
return "utf16le";
|
||||
}
|
||||
}
|
||||
|
||||
const searchModule = {
|
||||
logs: new Array(),
|
||||
//--- Get YouTube's Search Auto Complete ---//
|
||||
|
@ -26,10 +41,11 @@ const searchModule = {
|
|||
responseType: "arraybuffer",
|
||||
})
|
||||
.then((res) => {
|
||||
const contentType = res.headers["Content-Type"];
|
||||
// make a new buffer object from res.data
|
||||
const buffer = Buffer.from(res.data, "base64");
|
||||
// convert res.data from iso-8859-1 to utf-8
|
||||
const data = buffer.toString("latin1");
|
||||
const data = buffer.toString(getEncoding(contentType));
|
||||
logger(constants.LOGGER_NAMES.autoComplete, data);
|
||||
callback(data);
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue