-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
440 lines (386 loc) · 13.3 KB
/
content.js
File metadata and controls
440 lines (386 loc) · 13.3 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/************************************************************
* OTT 자동 재생 & 오프닝 스킵 통합 스크립트
* 지원 플랫폼: Wavve / Tving / Netflix
************************************************************/
/** @type {string} 현재 감지된 URL */
let previousUrl = location.pathname;
/** 트래커 상태 플래그 */
let tvingTrackerStarted = false;
let wavveTrackerStarted = false;
let netflixTrackerStarted = false;
/************************************************************
* 1. URL 변경 감지
************************************************************/
function handleUrlChange() {
const currentPath = location.pathname;
if (currentPath === previousUrl) return;
console.log(`URL 변경 감지: ${previousUrl} → ${currentPath}`);
previousUrl = currentPath;
initializePlatformFeatures();
}
/************************************************************
* 2. 플랫폼 기능 초기화
************************************************************/
function initializePlatformFeatures() {
chrome.storage.local.get(['ojPop'], (result) => {
const settings = result.ojPop || {};
const hostname = location.hostname;
const platformMap = {
wavve: {
hostnameMatch: 'wavve',
selectors: { opening: 'opening-skip-btn', next: 'next-episode-box' },
playNext: playWavvesNextEpisode,
},
tving: {
hostnameMatch: 'tving',
selectors: {
opening: 'PcSkipOpeningButton_overlayButton',
next: 'PcContinueNextEpisodeButton_episode',
},
playNext: playTvingNextEpisode,
},
netflix: {
hostnameMatch: 'netflix',
selectors: {
opening: 'button.watch-video--skip-content-button',
next: 'button[data-uia="next-episode-seamless-button"]',
},
playNext: playNetflixNextEpisode,
},
};
for (const [platform, config] of Object.entries(platformMap)) {
if (hostname.includes(config.hostnameMatch) && settings[platform]) {
console.log(`${platform} 설정 감지됨:`, settings[platform]);
setupObserversByPlatform(config, settings[platform], platform);
}
}
// 트래커 초기화
if (hostname.includes('tving')) initTvingTracker();
if (hostname.includes('wavve'))
initWavveTracker(globalThis.Utility.getContentIdFromUrl());
if (hostname.includes('netflix')) initNetflixTracker();
console.info(`OTT JUMP 초기화 완료 for ${hostname}`);
});
}
/************************************************************
* 3. 트래커 초기화 함수
************************************************************/
/**
* tving 트래커 초기화
*/
const initTvingTracker = () => {
const remainEl = document.querySelector('.uihide .remain-time');
if (!remainEl || tvingTrackerStarted) return;
const thisContent = new globalThis.EpisodeManager().getTvingContentId();
const tracker = new TvingWatchTracker(thisContent, '.uihide .remain-time');
tracker.startTracking();
tvingTrackerStarted = true;
console.log('✅ Tracker initialized for content:', thisContent);
};
/**
* Wavve 트래커 초기화
*/
const initWavveTracker = (contentId) => {
const tempContent = JSON.parse(
localStorage.getItem('ojWatchedContent') || '{}'
);
if (!tempContent.wavve || tempContent.wavve !== contentId) {
const tracker = new WavveWatchTracker(
contentId,
'.text-time.text',
'.text-duration.text'
);
tracker.startTracking();
wavveTrackerStarted = true;
console.log('⏱️ Wavve Tracker initialized for content:', contentId);
}
};
/**
* Netflix 트래커 초기화
*/
function initNetflixTracker() {
if (netflixTrackerStarted) return;
const remainSelector =
'div.watch-video--bottom-controls-container span[data-uia="controls-time-remaining"]';
const trackId = globalThis.Utility.getTrackIdFromUrl();
const tempContent = JSON.parse(
localStorage.getItem('ojWatchedContent') || '{}'
);
if (!tempContent.netflix || tempContent.netflix !== trackId) {
const tracker = new NetflixWatchTracker(trackId, remainSelector);
tracker.startTracking();
netflixTrackerStarted = true;
console.log('⏱️ Netflix Tracker initialized for content:', trackId);
}
}
/************************************************************
* 4. 옵저버 및 자동 실행
************************************************************/
/** Netflix 전용 오프닝 스킵 감지 */
let netflixObserver = null;
let netflixIntervalId = null;
/**
* Netflix 오프닝 스킵 버튼 감지 후 클릭
*/
function handleNetflixSkip() {
const skipButton = document.querySelector(
'button.watch-video--skip-content-button'
);
if (skipButton) {
console.log('🎬 Netflix 오프닝 스킵 버튼 감지');
skipButton.click();
}
}
/**
* Netflix용 옵저버 + Interval 시작
*/
function startNetflixSkipWatcher() {
if (netflixObserver || netflixIntervalId) return;
netflixObserver = new MutationObserver(handleNetflixSkip);
netflixObserver.observe(document.body, { childList: true, subtree: true });
netflixIntervalId = window.setInterval(handleNetflixSkip, 500);
}
/**
* Netflix 옵저버 + Interval 종료
*/
function stopNetflixSkipWatcher() {
if (netflixObserver) {
netflixObserver.disconnect();
netflixObserver = null;
}
if (netflixIntervalId) {
clearInterval(netflixIntervalId);
netflixIntervalId = null;
}
}
/** 플랫폼별 옵저버 설정 */
function setupObserversByPlatform(config, toggles, platform) {
const { opening, next } = config.selectors;
const { openingSkip, nextPlay, all } = toggles;
if (platform === 'netflix') {
// Netflix 오프닝 스킵
if (openingSkip || all) startNetflixSkipWatcher();
// Netflix 다음 회차
if (nextPlay || all) observeFeature(next, config.playNext, platform, true);
} else {
// 다른 플랫폼 기존 로직
if (all || (openingSkip && nextPlay)) {
observeFeature(opening, handleSkip, platform);
observeFeature(next, config.playNext, platform, true);
} else if (openingSkip) {
observeFeature(opening, handleSkip, platform);
} else if (nextPlay) {
observeFeature(next, config.playNext, platform, true);
}
}
}
/**
* ElementObserver 래핑 함수
* @param {string} selector 감지할 CSS 선택자
* @param {(target: HTMLElement) => void} callback 감지 시 실행할 콜백 함수
* @param {boolean} [once=false] 한 번만 감지할지 여부
*/
function observeFeature(selector, callback, platform = '', once = false) {
if (!selector || typeof callback !== 'function') return;
const observer = new globalThis.ElementObserver(
selector,
callback,
platform,
once
);
observer.observe();
}
/** 오프닝 스킵 버튼 클릭 */
function handleSkip(targetNode) {
console.log('🎬 오프닝 스킵 버튼 클릭:', targetNode);
if (targetNode) targetNode.click();
}
/************************************************************
* 5. 플랫폼별 다음 회차 자동 재생
************************************************************/
/**
* Wavve 플랫폼에서 다음 에피소드 자동 재생
* @param {HTMLElement} targetNode 다음 에피소드 버튼 컨테이너
*/
function playWavvesNextEpisode(targetNode) {
const link = targetNode?.querySelector('a');
const episodeManager = new globalThis.EpisodeManager();
const thisContent = globalThis.Utility.getContentIdFromUrl();
const tempContent = JSON.parse(
localStorage.getItem('ojWatchedContent') || '{}'
);
const lastContent = JSON.parse(localStorage.getItem('ojLastContent') || '{}');
const previous = lastContent.wavve;
// 진행시간/전체시간 확인
const duration = document
.querySelector('.text-duration')
?.textContent?.split('/ ')
.pop();
const now = document.querySelector('.text-time')?.textContent;
// 1분 이하 남으면 다음 회차 재생 스킵
if (episodeManager.isLessThanOneMinute(duration, now)) {
if (tempContent.wavve) {
localStorage.setItem(
'ojWatchedContent',
JSON.stringify({ ...tempContent, wavve: thisContent })
);
console.log('✅ Wavve 콘텐츠 임시 -> 확정 저장:', tempContent.wavve);
localStorage.removeItem('ojWatchedContent');
}
return;
}
// 이전 회차보다 새 콘텐츠인지 확인
if (link && globalThis.Utility.isContentIdNewer(previous, thisContent)) {
if (tempContent.wavve) {
localStorage.removeItem('ojWatchedContent');
}
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, wavve: thisContent })
);
link.click();
} else {
if (tempContent.wavve) {
localStorage.removeItem('ojWatchedContent');
}
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, wavve: thisContent })
);
}
}
/**
* Tving 플랫폼에서 다음 에피소드 자동 재생
* @param {HTMLElement} targetNode 다음 에피소드 버튼 컨테이너
*/
function playTvingNextEpisode(targetNode) {
const nextBtn = targetNode?.querySelector('button');
const episodeManager = new globalThis.EpisodeManager();
const thisContent = episodeManager.getTvingContentId();
console.log('playTvingNextEpisode thisContent:', thisContent);
const tempContent = JSON.parse(
localStorage.getItem('ojWatchedContent') || '{}'
);
const lastContent = JSON.parse(localStorage.getItem('ojLastContent') || '{}');
const previous = lastContent.tving;
// 기존 비교 로직: 현재 콘텐츠가 이전보다 최신이 아닌 경우
if (!globalThis.Utility.isContentIdNewer(previous, thisContent, 'tving')) {
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, tving: thisContent })
);
return;
}
// 남은 시간 체크
const remainTimeText = document
.querySelector('.remain-time')
?.textContent?.trim();
console.log('remainTimeText:', remainTimeText);
if (remainTimeText) {
const [m, s] = remainTimeText.split(':').map(Number);
if (m < 1 && s < 40) {
console.log(
`✅ Tving 콘텐츠 40초 미만. 다음 에피소드 재생 스킵`,
this.contentId
);
if (tempContent.tving) {
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, tving: tempContent.tving })
);
console.log('✅ Tving 콘텐츠 임시 -> 확정 저장:', tempContent.tving);
localStorage.removeItem('ojWatchedContent');
}
return;
}
}
// 다음 회차 재생
if (nextBtn) {
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, tving: thisContent })
);
nextBtn.click();
}
}
/**
* Netflix 플랫폼에서 다음 에피소드 자동 재생
* @param {HTMLElement} targetNode 다음 회차 버튼
*/
function playNetflixNextEpisode(targetNode) {
const btn = targetNode;
const trackId = globalThis.Utility.getTrackIdFromUrl(); // 변경됨
const tempContent = JSON.parse(
localStorage.getItem('ojWatchedContent') || '{}'
);
const lastContent = JSON.parse(localStorage.getItem('ojLastContent') || '{}');
const previous = lastContent.netflix;
// 기존 비교 로직: 현재 콘텐츠가 이전보다 최신이 아닌 경우
if (!globalThis.Utility.isContentIdNewer(previous, trackId, 'netflix')) {
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, netflix: trackId })
);
return;
}
// 남은 시간 체크
const remainTimeText = document
.querySelector(
'div.watch-video--bottom-controls-container span[data-uia="controls-time-remaining"]'
)
?.textContent?.trim();
if (remainTimeText) {
const [m, s] = remainTimeText.split(':').map(Number);
if (m < 1 && s < 40) {
console.log(
`✅ Netflix 콘텐츠 40초 미만. 다음 에피소드 재생 스킵`,
trackId
);
if (tempContent.netflix) {
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, netflix: tempContent.netflix })
);
console.log(
'✅ Netflix 콘텐츠 임시 -> 확정 저장:',
tempContent.netflix
);
localStorage.removeItem('ojWatchedContent');
}
return;
}
}
// 다음 회차 재생
if (btn) {
if (tempContent.netflix) localStorage.removeItem('ojWatchedContent');
localStorage.setItem(
'ojLastContent',
JSON.stringify({ ...lastContent, netflix: trackId })
);
btn.click();
}
}
// 초기 로딩 시 platform 설정
if (
location.hostname.includes('wavve') ||
location.hostname.includes('tving') ||
location.hostname.includes('netflix')
) {
initializePlatformFeatures();
}
// URL 변경 감지
const urlChangeObserver = new MutationObserver(handleUrlChange);
urlChangeObserver.observe(document.body, { childList: true, subtree: true });
console.info(`OTT JUMP 초기화 완료. ${Math.random()}`);
/**
* @typedef {Object} PlatformConfig
* @property {string} hostnameMatch - 호스트 이름 포함 문자열
* @property {{ opening: string, next: string }} selectors - 요소 선택자
* @property {(targetNode: HTMLElement) => void} playNext - 다음 회차 실행 함수
*/
/**
* @typedef {Object} FeatureToggle
* @property {boolean} all - 전체 자동화 실행 여부
* @property {boolean} openingSkip - 오프닝 스킵 여부
* @property {boolean} nextPlay - 다음 에피소드 자동 재생 여부
*/