1
+ import { UfsGlobal } from './content-scripts/ufs_global.js' ;
1
2
import { BADGES } from './helpers/badge.js' ;
2
3
3
4
export default {
@@ -17,8 +18,8 @@ export default {
17
18
setTimeout ( function ( ) {
18
19
// https://stackoverflow.com/a/8260383/11898496
19
20
function getIdFromYoutubeURL ( url ) {
20
- let regExp = / .* (?: y o u t u .b e \/ | v \/ | u \/ \w \/ | e m b e d \/ | w a t c h \? v = ) ( [ ^ # \& \? ] * ) .* / ;
21
- let match = url . match ( regExp ) ;
21
+ const regExp = / .* (?: y o u t u .b e \/ | v \/ | u \/ \w \/ | e m b e d \/ | w a t c h \? v = ) ( [ ^ # \& \? ] * ) .* / ;
22
+ const match = url . match ( regExp ) ;
22
23
return match && match [ 1 ] . length == 11 ? match [ 1 ] : false ;
23
24
}
24
25
@@ -84,31 +85,62 @@ export default {
84
85
85
86
const videoUrl = window . location . href ;
86
87
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
+
87
93
const genDownloadLinkFromProvider = ( provider , url ) =>
88
94
/* 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 ( ) ;
94
100
95
101
let intervalId = setInterval ( function ( ) {
96
102
const container = document . querySelector ( '#above-the-fold #title > h1' ) ;
97
103
if ( ! container ) return ;
98
104
99
105
clearInterval ( intervalId ) ;
100
106
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
+
101
112
const links = providers . map ( ( provider ) => genDownloadLinkFromProvider ( provider , videoUrl ) ) ;
102
113
103
114
container . insertAdjacentHTML (
104
115
'afterend' ,
105
116
/* 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>`
109
133
) ;
110
134
} , 500 ) ;
111
135
} , 500 ) ;
112
136
} ,
113
137
} ,
114
138
} ;
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