-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptura.js
60 lines (55 loc) · 1.29 KB
/
captura.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
chrome.contextMenus.create({
title: "Capturar thumbnail",
contexts: ["all"],
onclick: getVideoCod
});
function getVideoCod(info) {
let code = [];
const address_is_valid = validate_address(info.linkUrl);
if (address_is_valid) {
code = info.linkUrl.split("v=")[1].split("&")
getThumbnail(code[0])
} else {
alert("Não é um vídeo válido.")
}
}
function validate_address(url) {
let validity = false;
if (url == undefined) {
validity = false
} else {
const lista = url.split("/")
if (lista[2] == "www.youtube.com") {
validity = true
} else {
validity = false
}
}
return validity
}
async function getThumbnail(cod_video) {
url_image = "https://img.youtube.com/vi/" + cod_video
mq = url_image + "/mqdefault.jpg"
hq = url_image + "/hqdefault.jpg"
sd = url_image + "/sddefault.jpg"
max = url_image + "/maxresdefault.jpg"
const url_thumbs = [max, sd, hq, mq];
let i = 0;
let exist_thumb = false;
let status = 0;
while (!exist_thumb && i < 4) {
status = await search(url_thumbs[i]);
if (status == 200) {
exist_thumb = true;
window.open(url_thumbs[i])
}
i++;
}
}
function search(url) {
let status_cod = fetch(url)
.then(function (response) {
return response.status
});
return status_cod;
}