Skip to content

Instantly share code, notes, and snippets.

@shanenoi
Last active November 14, 2020 06:28
Show Gist options
  • Save shanenoi/751b8d101f439e19f7812bdcb03964f1 to your computer and use it in GitHub Desktop.
Save shanenoi/751b8d101f439e19f7812bdcb03964f1 to your computer and use it in GitHub Desktop.
Sort các video có lượt view cao trên Youtube
/* === Map on Vietnamese === */
// Units
MAP_VALUE = {
N: Math.pow(10,3),
Tr: Math.pow(10,6),
T: Math.pow(10,9)
}
// Details Regex
DETAILS = /(([\d,]+).+lượt xem)/
/* ================================= */
function Mapping(matched) {
try {
number = parseFloat(
matched[2].replace(',', '.')
);
} catch (TypeError) {
return 0;
}
keys = Object.keys(MAP_VALUE);
for(let i=0; i<keys.length; i++) {
if (matched[1].includes(keys[i])) {
number = number * MAP_VALUE[keys[i]];
break;
}
}
return number;
}
result_array = [];
result_found = document.getElementsByTagName('ytd-grid-video-renderer');
for(i=0; i<result_found.length; i++) {
result_array.push(
[result_found[i].getElementsByTagName('h3')[0].textContent,
Mapping(result_found[i].innerText.match(DETAILS))]
);
}
console.log(
result_array.sort(
function(first_element, second_element)
{return second_element[1]- first_element[1];}
).join("\n")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment