-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
188 lines (169 loc) · 6.73 KB
/
content.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var videoIds = [];
var videoUrls = [];
var videoId;
var videoTitle=[];
var state = [];
var I=0;
var likeList=[];
var dislikeList=[];
async function likedislikeFetch(videoID, VideoTitle){
const apiUrl = `https://returnyoutubedislikeapi.com/votes?videoId=${videoID}`
try {
const response = await fetch(apiUrl)
if (!response.ok) {
throw new Error(`Fetch failed with status ${response.status}`)
}
const data = await response.json();
console.log(data,videoID)
const like = Number(data.likes)
const dislike=Number(data.dislikes)
console.log(dislike)
if(dislike===undefined){
likeIcon=await insertIcon('assets/like.jpg')
dislikeIcon=await insertIcon('assets/dislike.jfif')
const likeContainer = document.createElement('div')
const dislikeContainer = document.createElement('div')
likeContainer.appendChild(likeIcon)
likeContainer.insertAdjacentText('beforeend', `${like}`)
dislikeContainer.appendChild(dislikeIcon)
dislikeContainer.insertAdjacentText('beforeend', `${dislike}`)
VideoTitle.insertAdjacentElement('beforeend', likeContainer)
VideoTitle.insertAdjacentElement('beforeend', dislikeContainer)
VideoTitle.style.display = 'flex'
VideoTitle.style.justifyContent = 'space-between'
const totalVotes = like + dislike;
const likePercentage = totalVotes === 0 ? 0 : (like / totalVotes) * 100
if(totalVotes===0){
VideoTitle.style.background = '#B5B5B5'
}else if (likePercentage > 98) {
VideoTitle.style.background = '#B2FFBC'
const recommendedText = document.createElement('span')
recommendedText.style.fontSize = '120%'
recommendedText.textContent = ' Recommended'
recommendedText.style.border="1px solid green"
recommendedText.style.borderRadius="4px"
dislikeContainer.insertAdjacentElement('afterend', recommendedText)
} else if (likePercentage < 40) {
VideoTitle.style.background = '#FFB9B9'
}
}
else{
if(VideoTitle){
likeIcon=await insertIcon('assets/like.jpg')
dislikeIcon=await insertIcon('assets/dislike.jfif')
const likeContainer = document.createElement('div')
const dislikeContainer = document.createElement('div')
likeContainer.appendChild(likeIcon)
likeIcon.style.height="18%"
likeIcon.style.width="18%"
likeContainer.insertAdjacentText('beforeend', `${like}`)
dislikeContainer.appendChild(dislikeIcon);
dislikeContainer.insertAdjacentText('beforeend', `${dislike}`)
VideoTitle.insertAdjacentElement('beforeend', likeContainer)
VideoTitle.insertAdjacentElement('beforeend', dislikeContainer)
VideoTitle.style.display = 'flex'
VideoTitle.style.justifyContent = 'space-between'
const totalVotes = like + dislike
const likePercentage = totalVotes === 0 ? 0 : (like / totalVotes) * 100
if(totalVotes===0){
VideoTitle.style.background = '#B5B5B5'
}else if (likePercentage > 98) {
VideoTitle.style.background = '#B2FFBC'
const recommendedText = document.createElement('span')
recommendedText.style.fontSize = '120%'
recommendedText.textContent = ' Recommended'
recommendedText.style.border="1px solid green"
recommendedText.style.borderRadius="4px"
dislikeContainer.insertAdjacentElement('afterend', recommendedText)
} else if (likePercentage < 40) {
VideoTitle.style.background = '#FFB9B9'
}
}
}
} catch (error) {
console.error(`Failed to fetch like/dislike counts for video ${videoID}: ${error.message}`)
}
}
async function observeVideos() {
var videoUrls = removeDuplicates(getVideoUrls());
var i=0
while(i<videoUrls.length) {
console.log(i, state[i])
videoId = getVideoId(videoUrls[i])
if (videoId && !state[i]) {
videoIds.push(videoId)
state[i]=true
likedislikeFetch(videoId,videoTitle[i])
}
else{dislikeList=removeDuplicates(dislikeList);i++;I=i;continue}
//console.log(videoId)
videoTitle=document.querySelectorAll('#channel-info')
if(videoTitle[i] && !state[i]){
state[i]=true}
}
var observer = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
var newVideoUrl = entry.target.href
var newVideoId = getVideoId(newVideoUrl)
if (newVideoId) {
videoIds.push(newVideoId)
}
//console.log(videoIds)
}
});
}, {threshold: 1.0});
var newVideoThumbnails = document.querySelectorAll("a#video-title-link:not(.observed)");
for (var i = 0; i < newVideoThumbnails.length; i++) {
observer.observe(newVideoThumbnails[i])
newVideoThumbnails[i].classList.add("observed")
}
videoIds=removeDuplicates(videoIds)
//console.log(videoIds);
}
var videoParent;
var likeIcon;
var dislikeIcon;
function insertIcon(path){
const graphics=document.createElement("img")
graphics.src=chrome.runtime.getURL(path)
graphics.style.width = "11%"
graphics.style.height = "11%"
return graphics
}
function removeDuplicates(arr) {
let unique = arr.reduce(function (acc, curr) {
if (!acc.includes(curr))
acc.push(curr);
return acc;
}, []);
return unique;
}
function getVideoUrls() {
var videoThumbnails = document.querySelectorAll("a#video-title");
//var videoTitle = document.querySelectorAll('a#video-title-link')
for (var i = 0; i < videoThumbnails.length; i++) {
var videoUrl = videoThumbnails[i].href
if(videoUrl===''){
continue
}
videoUrls.push(videoUrl)
}
//console.log(videoUrls)
videoUrls=removeDuplicates(videoUrls)
return (videoUrls)
}
function getVideoId(url) {
var VID_REGEX = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i;
var match = url.match(VID_REGEX);
console.log(match)
if (match && match[1]) {
return match[1];
} else {
return false;
}
}
observeVideos()
window.addEventListener('scroll', function() {
observeVideos();}
);