-
Notifications
You must be signed in to change notification settings - Fork 27
/
content.js
115 lines (92 loc) · 3.71 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
var overlay = null,
frame = null;
window.__PREVYOU_LOADED = true
// Event send by the inner `<object>` script
window.addEventListener('message', e => {
if (e.data && e.data.type === 'find_card') {
findCard()
}
})
// Event send by the extension popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type == "popup") {
//console.log(request);
showPopup();
} else if (request.type === 'close_popup') {
hidePopup();
}
return true;
});
function showPopup() {
if (document.querySelector(".py-popup-overlay")) {
hidePopup();
return false;
}
overlay = document.createElement('div');
frame = document.createElement('object');
overlay.className = "py-popup-overlay";
frame.className = "py-popup-container";
frame.setAttribute("scrolling", "no");
frame.setAttribute("frameborder", "0");
// file need to be added in manifest web_accessible_resources
frame.data = chrome.runtime.getURL("popup.html");
overlay.appendChild(frame);
document.body.appendChild(overlay);
overlay.addEventListener("click", hidePopup);
}
function hidePopup() {
// Remove EventListener
overlay.removeEventListener("click", hidePopup);
// Remove the elements:
document.querySelector(".py-popup-overlay").remove();
// Clean up references:
overlay = null;
frame = null;
}
function findCard() {
// Select a random a card in between a range
let cardPositionIndex = 0
const activeScreen = document.querySelector('[role="main"]')
// Target only ytd-rich-item-renderer element and not ytd-rich-item-renderer with id content for the main page
let cards = activeScreen.querySelectorAll('.ytd-rich-grid-media:not(#content):not(ytd-display-ad-renderer)')
if (cards.length === 0) {
cards = activeScreen.getElementsByTagName('ytd-grid-video-renderer')
}
if (cards.length === 0) {
cards = activeScreen.getElementsByTagName('ytd-compact-video-renderer')
}
chrome.storage.local.get('thumbnailProperties', (result) => {
if (result.thumbnailProperties.shuffle) {
const min = 1
const max = 12
cardPositionIndex = Math.floor(Math.random() * (max - min + 1)) + min
}
let target = cards[cardPositionIndex]
const thumbnail = target.querySelector('.ytd-thumbnail > img')
thumbnail.src = result.thumbnailProperties.thumbnail
const title = target.querySelector('#video-title')
let channelName = target.querySelector('.ytd-channel-name a')
if (!channelName) {
channelName = target.querySelector('.ytd-channel-name')
}
title.textContent = result.thumbnailProperties.title
channelName.textContent = result.thumbnailProperties.channelName
// Channel's thumbnail management
let channelThumbnailFromExtension = result.thumbnailProperties.channelThumbnail
let channelThumbnailFromYoutube = document.querySelector('#avatar-btn .yt-img-shadow')
// By default, we get the image from the extension
let channelThumbnailValue = channelThumbnailFromExtension
// But if there's no image then we try to get the real YT thumbnail
// => Thumbnail from YT is null if not logged in so we check for it
if (channelThumbnailValue == null && channelThumbnailFromYoutube != null) {
channelThumbnailValue = channelThumbnailFromYoutube.src
}
// Finally, set the channel's thumbnail in the preview
let avatar = target.querySelector('#avatar-link .yt-img-shadow')
if (avatar) {
avatar.src = channelThumbnailValue
}
hidePopup()
})
}
showPopup()