dedect and validate link type

This commit is contained in:
simon 2022-05-31 15:45:25 +07:00
parent 62e7469e3d
commit 809134d90f
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 38 additions and 2 deletions

View File

@ -182,6 +182,8 @@ document.addEventListener("DOMContentLoaded", async () => {
function createButtons(result) { function createButtons(result) {
let download = document.getElementById("download"); let download = document.getElementById("download");
let linkType = document.createElement("h3");
linkType.innerText = result.youtube.type.charAt(0).toUpperCase() + result.youtube.type.slice(1);
let title = document.createElement("p"); let title = document.createElement("p");
title.innerText = result.youtube.title; title.innerText = result.youtube.title;
@ -199,6 +201,7 @@ function createButtons(result) {
subscribeButton.setAttribute("data-id", result.youtube.url); subscribeButton.setAttribute("data-id", result.youtube.url);
subscribeButton.addEventListener("click", function(){subscribeEvent()}, false); subscribeButton.addEventListener("click", function(){subscribeEvent()}, false);
download.appendChild(linkType);
download.appendChild(title); download.appendChild(title);
download.appendChild(downloadButton); download.appendChild(downloadButton);
download.appendChild(subscribeButton); download.appendChild(subscribeButton);

View File

@ -22,12 +22,41 @@ function getBrowser() {
} }
function detectUrlType(url) {
const videoRe = new RegExp(/^https:\/\/(www\.)?(youtube.com\/watch\?v=|youtu\.be\/)\w{11}/);
if (videoRe.test(url)) {
return "video"
}
const channelRe = new RegExp(/^https:?\/\/www\.?youtube.com\/c|channel|user\/\w+(\/|featured|videos)?$/);
if (channelRe.test(url)) {
return "channel"
}
const playlistRe = new RegExp(/^https:\/\/(www\.)?youtube.com\/playlist\?list=/);
if (playlistRe.test(url)) {
return "playlist"
}
return false
}
function sendUrl() { function sendUrl() {
let url = document.URL
let urlType = detectUrlType(url);
if (urlType == false) {
console.log("not relevant")
return
}
let payload = { let payload = {
"youtube": { "youtube": {
"url": document.URL, "url": url,
"title": document.title "title": document.title,
"type": urlType,
} }
} }
console.log("youtube link: " + JSON.stringify(payload)); console.log("youtube link: " + JSON.stringify(payload));

View File

@ -8,6 +8,10 @@ body {
min-width: 300px; min-width: 300px;
max-width: 400px; max-width: 400px;
} }
.h3 {
font-family: Sen-bold, sans-serif;
text-transform: capitalize;
}
hr { hr {
height: 1px; height: 1px;
color: white; color: white;