Skip to content

Commit 5a3a4c6

Browse files
Merge pull request #31 from yuran1811/feat/youtube_downloadVideoUI
feat: youtube download video UI
2 parents d932692 + 25890b2 commit 5a3a4c6

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
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: 47 additions & 11 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

@@ -73,7 +74,7 @@ export default {
7374
{
7475
name: 'ymp4.com',
7576
color: colors.default,
76-
func: (url) => 'https://ymp4.download/en50/?url=' + url,
77+
func: (url) => 'https://ymp4.download/?url=' + url,
7778
},
7879
{
7980
name: 'getlinks.vip',
@@ -84,31 +85,66 @@ 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
96+
href="${provider.func(url)}"
97+
target="_blank"
98+
class="ufs-ytDownloadVideoUI__btn"
99+
onclick="((e)=>e.stopPropagation())(event)">
100+
${provider.name}
101+
</a>`;
102+
103+
injectCss();
94104

95105
let intervalId = setInterval(function () {
96106
const container = document.querySelector('#above-the-fold #title > h1');
97107
if (!container) return;
98108

99109
clearInterval(intervalId);
100110

111+
document.addEventListener('click', () => {
112+
const el = document.querySelector('#ufs-ytDownloadBtn__container');
113+
if (el && el.style.display === 'flex') el.style.display = 'none';
114+
});
115+
101116
const links = providers.map((provider) => genDownloadLinkFromProvider(provider, videoUrl));
102117

103118
container.insertAdjacentHTML(
104119
'afterend',
105120
/* 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>`
121+
`
122+
<button
123+
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"
124+
style="position:relative;margin:6px 0;"
125+
onclick="((e)=>{e.stopPropagation();const el = document.querySelector('#ufs-ytDownloadBtn__container');if (!el) return;el.style.display = el.style.display == 'flex' ? 'none' : 'flex'})(event)"
126+
>
127+
<div class="yt-spec-button-shape-next__icon">
128+
${downloadIcon}
129+
</div>
130+
131+
<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>
132+
133+
<div id="ufs-ytDownloadBtn__container" class="ufs-ytDownloadVideoUI__container">
134+
${links.join('')}
135+
</div>
136+
</button>`
109137
);
110138
}, 500);
111139
}, 500);
112140
},
113141
},
114142
};
143+
144+
function injectCss(path = '/scripts/youtube_downloadVideoUI.css', id = 'ufs-yt_downloadVideoUI-css') {
145+
if (!document.querySelector('#' + id)) {
146+
UfsGlobal.Extension.getURL(path).then((url) => {
147+
UfsGlobal.DOM.injectCssFile(url, id);
148+
});
149+
}
150+
}

0 commit comments

Comments
 (0)