aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/js-components/previewBar.ts
blob: 6793e42d7d72794d5b8bd4dd4e52b7a9534754c7 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
	This is based on code from VideoSegments.
	https://github.com/videosegments/videosegments/commits/f1e111bdfe231947800c6efdd51f62a4e7fef4d4/segmentsbar/segmentsbar.js
*/

'use strict';

let barTypes = {
	"undefined": {
		color: "#00d400",
		opacity: "0.7"
	},
	"sponsor": {
		color: "#00d400",
		opacity: "0.7"
	},
	"preview-sponsor": {
		color: "#007800",
		opacity: "0.7"
	},
	"intro": {
		color: "#00ffff",
		opacity: "0.7"
	},
	"preview-intro": {
		color: "#008080",
		opacity: "0.7"
	},
	"outro": {
		color: "#0202ed",
		opacity: "0.7"
	},
	"preview-outro": {
		color: "#000070",
		opacity: "0.7"
	},
	"interaction": {
		color: "#cc00ff",
		opacity: "0.7"
	},
	"preview-interaction": {
		color: "#6c0087",
		opacity: "0.7"
	},
	"selfpromo": {
		color: "#ffff00",
		opacity: "0.7"
	},
	"preview-selfpromo": {
		color: "#bfbf35",
		opacity: "0.7"
	},
	"music_offtopic": {
		color: "#ff9900",
		opacity: "0.7"
	},
	"preview-music_offtopic": {
		color: "#a6634a",
		opacity: "0.7"
	}
};

class PreviewBar {
	container: HTMLUListElement;
	parent: any;
	onMobileYouTube: boolean;

	timestamps: number[][];
	types: string;

	constructor(parent, onMobileYouTube) {
		this.container = document.createElement('ul');
		this.container.id = 'previewbar';
		this.parent = parent;

		this.onMobileYouTube = onMobileYouTube;

		this.updatePosition(parent);

		this.setupHoverText();
	}

	setupHoverText() {
		let seekBar = document.querySelector(".ytp-progress-bar-container");

		// Create label placeholder
		let tooltipTextWrapper = document.querySelector(".ytp-tooltip-text-wrapper");
		let titleTooltip = document.querySelector(".ytp-tooltip-title");
		let categoryTooltip = document.createElement("div");
		categoryTooltip.className = "sbHidden ytp-tooltip-title";
		categoryTooltip.id = "sponsor-block-category-tooltip"

		tooltipTextWrapper.insertBefore(categoryTooltip, titleTooltip.nextSibling);

		let mouseOnSeekBar = false;

		seekBar.addEventListener("mouseenter", (event) => {
			mouseOnSeekBar = true;
		});

		seekBar.addEventListener("mouseleave", (event) => {
			mouseOnSeekBar = false;
			categoryTooltip.classList.add("sbHidden");
		});

		const observer = new MutationObserver(() => {
			if (!mouseOnSeekBar) return;

			let tooltips = document.querySelectorAll(".ytp-tooltip-text");
			for (const tooltip of tooltips) {
				let splitData = tooltip.textContent.split(":");
				if (splitData.length === 2 && !isNaN(parseInt(splitData[0])) && !isNaN(parseInt(splitData[1]))) {
					// Add label
					let timeInSeconds = parseInt(splitData[0]) * 60 + parseInt(splitData[1]);

					// Find category at that location
					let category = null;
					for (let i = 0; i < this.timestamps.length; i++) {
						if (this.timestamps[i][0] < timeInSeconds && this.timestamps[i][1] > timeInSeconds){
							category = this.types[i];
						} 
					}

					if (category === null && !categoryTooltip.classList.contains("sbHidden")) {
						categoryTooltip.classList.add("sbHidden");
						tooltipTextWrapper.classList.remove("sbTooltipTwoTitleThumbnailOffset");
						tooltipTextWrapper.classList.remove("sbTooltipOneTitleThumbnailOffset");
					} else if (category !== null) {
						categoryTooltip.classList.remove("sbHidden");
						categoryTooltip.textContent = chrome.i18n.getMessage("category_" + category) 
							|| (chrome.i18n.getMessage("preview") + " " + chrome.i18n.getMessage("category_" + category.split("preview-")[1]));

						// There is a title now
						tooltip.classList.remove("ytp-tooltip-text-no-title");

						// Add the correct offset for the number of titles there are
						if (titleTooltip.textContent !== "") {
							if (!tooltipTextWrapper.classList.contains("sbTooltipTwoTitleThumbnailOffset")) {
								tooltipTextWrapper.classList.add("sbTooltipTwoTitleThumbnailOffset");
							}
						} else if (!tooltipTextWrapper.classList.contains("sbTooltipOneTitleThumbnailOffset")) {
							tooltipTextWrapper.classList.add("sbTooltipOneTitleThumbnailOffset");
						}
					}

					break;
				}
			}
		});

		observer.observe(tooltipTextWrapper, {
			childList: true,
			subtree: true
		});
	}

	updatePosition(parent) {
		//below the seek bar
		// this.parent.insertAdjacentElement("afterEnd", this.container);

		this.parent = parent;

		if (this.onMobileYouTube) {
			parent.style.backgroundColor = "rgba(255, 255, 255, 0.3)";
			parent.style.opacity = "1";
			
			this.container.style.transform = "none";
		}
		
		//on the seek bar
		this.parent.insertAdjacentElement("afterBegin", this.container);
	}

	updateColor(segment, color, opacity) {
		let bars = <NodeListOf<HTMLElement>> document.querySelectorAll('[data-vs-segment-type=' + segment + ']');
		for (let bar of bars) {
			bar.style.backgroundColor = color;
			bar.style.opacity = opacity;
		}
	}

	set(timestamps, types, duration) {
		while (this.container.firstChild) {
			this.container.removeChild(this.container.firstChild);
		}

		if (!timestamps || !types) {
			return;
		}

		this.timestamps = timestamps;
		this.types = types;

		// to avoid rounding error resulting in width more than 100% 
		duration = Math.floor(duration * 100) / 100;
		let width;
		for (let i = 0; i < timestamps.length; i++) {
			if (types[i] == null) continue;

			width = (timestamps[i][1] - timestamps[i][0]) / duration * 100;
			width = Math.floor(width * 100) / 100;

			let bar = this.createBar();
			bar.setAttribute('data-vs-segment-type', types[i]);

			bar.style.backgroundColor = barTypes[types[i]].color;
			if (!this.onMobileYouTube) bar.style.opacity = barTypes[types[i]].opacity;
			bar.style.width = width + '%';
			bar.style.left = (timestamps[i][0] / duration * 100) + "%";
			bar.style.position = "absolute"

			this.container.insertAdjacentElement("beforeend", bar);
		}
	}

	createBar() {
		let bar = document.createElement('li');
		bar.classList.add('previewbar');
		bar.innerHTML = '&nbsp;';
		return bar;
	}

	remove() {
		this.container.remove();
		this.container = undefined;
	}
}

export default PreviewBar;