Skip to content

Commit bd17a9f

Browse files
committed
feat: ✨ enhance ui with dropdown and youtube-like ui
new file: scripts/youtube_downloadVideoUI.css modified: scripts/youtube_downloadVideoUI.js
1 parent 6d45c02 commit bd17a9f

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

scripts/youtube_downloadVideoUI.css

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.ufs-ytDownloadVideoUI__btn {
2+
color: #f1f1f1;
3+
display: inline-block;
4+
padding: 2px 12px;
5+
border-radius: 6px;
6+
text-decoration: none;
7+
font-weight: bold;
8+
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
9+
-webkit-tap-highlight-color: transparent;
10+
}
11+
12+
.ufs-ytDownloadVideoUI__btn:hover {
13+
background-color: #333;
14+
}
15+
16+
.ufs-ytDownloadVideoUI__container {
17+
background-color: #272727;
18+
z-index: 999;
19+
position: absolute;
20+
top: 45px;
21+
left: 0;
22+
23+
display: none;
24+
flex-flow: column;
25+
justify-content: start;
26+
align-items: start;
27+
28+
overflow: hidden auto;
29+
width: max-content;
30+
max-height: 250px;
31+
padding: 8px;
32+
border-radius: 12px;
33+
}

scripts/youtube_downloadVideoUI.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UfsGlobal } from './content-scripts/ufs_global.js';
12
import { BADGES } from './helpers/badge.js';
23

34
export default {
@@ -17,8 +18,8 @@ export default {
1718
setTimeout(function () {
1819
// https://stackoverflow.com/a/8260383/11898496
1920
function getIdFromYoutubeURL(url) {
20-
let regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
21-
let match = url.match(regExp);
21+
const regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
22+
const match = url.match(regExp);
2223
return match && match[1].length == 11 ? match[1] : false;
2324
}
2425

@@ -84,31 +85,62 @@ export default {
8485

8586
const videoUrl = window.location.href;
8687

88+
const downloadIcon = `
89+
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" focusable="false" style="pointer-events:none;display:inherit;width:100%;height:100%;">
90+
<path d="M17 18v1H6v-1h11zm-.5-6.6-.7-.7-3.8 3.7V4h-1v10.4l-3.8-3.8-.7.7 5 5 5-4.9z"></path>
91+
</svg>`;
92+
8793
const genDownloadLinkFromProvider = (provider, url) =>
8894
/* html */
89-
`<a href="${provider.func(
90-
url
91-
)}" target="_blank" style="display:inline-block;font-size:16px;padding:8px 12px;background-color:${
92-
provider.color
93-
};color:white;border-radius:12px;text-decoration:none;font-weight:bold">${provider.name}</a>`;
95+
`<a href="${provider.func(url)}" target="_blank" class="ufs-ytDownloadVideoUI__btn">
96+
${provider.name}
97+
</a>`;
98+
99+
injectCss();
94100

95101
let intervalId = setInterval(function () {
96102
const container = document.querySelector('#above-the-fold #title > h1');
97103
if (!container) return;
98104

99105
clearInterval(intervalId);
100106

107+
document.addEventListener('click', () => {
108+
const el = document.querySelector('#ufs-ytDownloadBtn__container');
109+
if (el && el.style.display === 'flex') el.style.display = 'none';
110+
});
111+
101112
const links = providers.map((provider) => genDownloadLinkFromProvider(provider, videoUrl));
102113

103114
container.insertAdjacentHTML(
104115
'afterend',
105116
/* html */
106-
`<div style="display:flex;justify-content:start;align-items:center;flex-wrap:wrap;gap:6px;width:100%;h:max-content">
107-
${links.join('')}
108-
</div>`
117+
`
118+
<button
119+
class="yt-spec-button-shape-next yt-spec-button-shape-next--tonal yt-spec-button-shape-next--mono yt-spec-button-shape-next--size-m yt-spec-button-shape-next--icon-leading"
120+
style="position:relative;margin:6px 0;"
121+
onclick="((function(event){event.preventDefault();event.stopPropagation();const el = document.querySelector('#ufs-ytDownloadBtn__container');if (!el) return;el.style.display = el.style.display == 'flex' ? 'none' : 'flex'}))(event)"
122+
>
123+
<div class="yt-spec-button-shape-next__icon">
124+
${downloadIcon}
125+
</div>
126+
127+
<div class="yt-spec-button-shape-next__button-text-content"><span class="yt-core-attributed-string yt-core-attributed-string--white-space-no-wrap" role="text">Tải video</span></div>
128+
129+
<div id="ufs-ytDownloadBtn__container" class="ufs-ytDownloadVideoUI__container">
130+
${links.join('')}
131+
</div>
132+
</button>`
109133
);
110134
}, 500);
111135
}, 500);
112136
},
113137
},
114138
};
139+
140+
function injectCss(path = '/scripts/youtube_downloadVideoUI.css', id = 'ufs-yt_downloadVideoUI-css') {
141+
if (!document.querySelector('#' + id)) {
142+
UfsGlobal.Extension.getURL(path).then((url) => {
143+
UfsGlobal.DOM.injectCssFile(url, id);
144+
});
145+
}
146+
}

0 commit comments

Comments
 (0)