-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-most-viewed.js
120 lines (95 loc) · 2.84 KB
/
youtube-most-viewed.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* POP UP with vids sorted by views
*/
(function () {
let videos = new Array();
let url;
let views;
let elements = document.getElementsByTagName('ytd-rich-item-renderer');
[...elements].forEach(element => {
let url = element.querySelector('a').getAttribute('href');
let name = element.querySelector('#video-title').innerHTML;
let views = element.getElementsByClassName('inline-metadata-item style-scope ytd-video-meta-block')[0].innerHTML;
views = views.replace(' views', '');
if (views.includes('K')) {views = parseFloat(views) * 1000} else if
(views.includes('M')) {views = parseFloat(views) * 1000000} else if
(views.includes('B')) {views = parseFloat(views) * 1000000000};
videos.push({u: url, v: views, n: name});
});
videos.sort((a, b) => (a.v > b.v) ? -1 : 1);
// Create styles dynamically
const style = document.createElement('style');
style.textContent = `
.popup {
display: none; /* Hidden by default */
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
justify-content: center;
align-items: flex-start;
z-index: 3000;
padding-top: 20px;
}
.popup-content {
box-shadow: 0px 0px 400px 30px #000;
background-color: white;
padding: 20px;
border-radius: 5px;
text-align: left;
min-width: 300px;
}
#arrayList {
list-style-type: none;
padding: 0;
}
#arrayList a {
font-family: monospace;
font-size: 16px !important;
}
#arrayList a:visited {
color: red !important;
}
#closePopupBtn {
margin-bottom: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
`;
document.head.appendChild(style);
// Create popup elements dynamically
const popup = document.createElement('div');
popup.id = 'popup';
popup.className = 'popup';
const popupContent = document.createElement('div');
popupContent.className = 'popup-content';
const arrayList = document.createElement('ul');
arrayList.id = 'arrayList';
const closePopupBtn = document.createElement('button');
closePopupBtn.id = 'closePopupBtn';
closePopupBtn.textContent = 'OK';
popupContent.appendChild(closePopupBtn);
popupContent.appendChild(arrayList);
popup.appendChild(popupContent);
document.body.appendChild(popup);
arrayList.innerHTML = '';
// Create list items from array
videos.forEach(item => {
const li = document.createElement('li');
const link = document.createElement('a');
link.href = item['u'];
link.textContent = item['v'] + ' ' + item['n'];
link.target = '_blank';
li.appendChild(link);
arrayList.appendChild(li);
});
// Show the popup
popup.style.display = 'flex';
// Add event listener to close popup button
closePopupBtn.addEventListener('click', function() {
// Hide the popup
popup.style.display = 'none';
});
})();