-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
52 lines (40 loc) · 1.45 KB
/
popup.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
var title;
var infoTitle;
var imgUrl;
const regex1 = / *\/ */g;
const setImageInfo = (ele) => {
document.getElementById('title').value = title + '_' + ele.textContent;
document.getElementById('preview').src = imgUrl + ele.id + '.jpg';
document.getElementById('download').href = imgUrl + ele.id + '.jpg';
}
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
const urlStr = tabs[0].url;
const url = new URL(urlStr);
const videoId = url.searchParams.get('v');
if (!videoId) {
document.getElementById('info').textContent = '非執行對象';
document.getElementById('main').hidden = true;
return;
}
title = tabs[0].title.slice(0, tabs[0].title.length - ' - YouTube'.length);
infoTitle = title;
title = title.replaceAll(regex1, '、');
document.getElementById('info').textContent = `標題:《${infoTitle}》`;
imgUrl = `https://i.ytimg.com/vi/${videoId}/`;
document.getElementById('maxresdefault').click();
});
document.getElementById('maxresdefault').addEventListener('click', function() {
setImageInfo(this);
});
document.getElementById('hqdefault').addEventListener('click', function() {
setImageInfo(this);
});
document.getElementById('mqdefault').addEventListener('click', function() {
setImageInfo(this);
});
document.getElementById('download').addEventListener('click', function() {
chrome.downloads.download({
filename: document.getElementById('title').value + '.jpg',
url: this.href
});
});