-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcontent-script.ts
148 lines (125 loc) · 4.17 KB
/
content-script.ts
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
let originalVideoElementStyle: any = null;
async function asyncFind(arr: Array<any>, callback: Function) {
for (const element of arr) {
const result = await callback(element);
if (result) {
return element;
}
}
return null;
}
async function checkImageLoaded(imageElement: HTMLImageElement) {
return new Promise(resolve => {
const intervalId = setInterval(() => {
if (imageElement.complete) {
clearInterval(intervalId);
resolve(null);
}
}, 400);
});
}
async function setBackgroundImage(videoElement: HTMLVideoElement) {
if (!originalVideoElementStyle) {
originalVideoElementStyle = {
background: videoElement.style.background,
backgroundSize: videoElement.style.backgroundSize,
}
}
let vid = window.location.search.split('v=')[1];
if (!vid) return;
const pos = vid.indexOf('&');
if (pos !== -1) {
vid = vid.substring(0, pos);
}
// https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api
const videoImageUrls = [
`https://img.youtube.com/vi/${vid}/maxresdefault.jpg`,
`https://img.youtube.com/vi/${vid}/sddefault.jpg`,
`https://img.youtube.com/vi/${vid}/hqdefault.jpg`,
`https://img.youtube.com/vi/${vid}/mqdefault.jpg`,
`https://img.youtube.com/vi/${vid}/0.jpg`,
];
const bgUrl = await asyncFind(videoImageUrls, async (url: string) => {
const imageElement = new Image();
imageElement.src = url;
await checkImageLoaded(imageElement);
// Placeholder image has width of 120px.
return imageElement.width !== 120;
});
videoElement.style.background = `transparent url(${bgUrl}) no-repeat center / cover`;
}
function showAudioOnlyInformation(videoElement: HTMLVideoElement) {
if (document.getElementsByClassName('audio_only_div').length === 0) {
const extensionAlert = document.createElement('div');
extensionAlert.className = 'audio_only_div';
const alertText = document.createElement('p');
alertText.className = 'alert_text';
alertText.innerHTML =
'Audio Only. To watch video, ' +
'click on the extension icon above and refresh your page.';
extensionAlert.appendChild(alertText);
const videoParent = videoElement.parentNode;
if (!videoParent) return;
const parent = videoParent.parentNode;
if (parent) {
parent.appendChild(extensionAlert);
}
}
}
function removeBackgroundImage(videoElement: HTMLVideoElement) {
if (!originalVideoElementStyle) {
return;
}
videoElement.style.background = originalVideoElementStyle.background;
}
function removeAudioOnlyInformation() {
const elements = document.getElementsByClassName('audio_only_div');
if (!elements.length) return;
Array.from(elements).forEach(function(element) {
element.remove();
});
}
function removeVideoPlayerStyling(videoElement: HTMLVideoElement) {
removeBackgroundImage(videoElement);
removeAudioOnlyInformation();
}
function applyVideoPlayerStyling(videoElement: HTMLVideoElement) {
chrome.storage.sync.get({ showThumbnail: true }, function(item) {
if (item.showThumbnail) {
setBackgroundImage(videoElement);
}
});
showAudioOnlyInformation(videoElement);
}
function makeSetAudioURL(videoElement: HTMLVideoElement, url: string) {
function setAudioURL() {
if (url === '' || videoElement.src === url) {
return;
}
videoElement.pause();
videoElement.src = url;
videoElement.currentTime = 0;
videoElement.play();
}
return setAudioURL;
}
chrome.runtime.onMessage.addListener((request) => {
const url = request.url;
const videoElements = window.document.getElementsByTagName('video');
const videoElement = videoElements[0];
if (typeof videoElement == 'undefined') {
console.log('Audio Only Youtube - Video element undefined in this frame!');
return;
}
const videoRect = videoElement.getBoundingClientRect();
if (videoRect.width === 0 && videoRect.height === 0) {
console.log('Audio Only Youtube - Video element not visible!');
return;
}
videoElement.onloadeddata = makeSetAudioURL(videoElement, url);
if (url) {
applyVideoPlayerStyling(videoElement);
} else {
removeVideoPlayerStyling(videoElement);
}
});