-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
150 lines (121 loc) · 4.27 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
var player, lyricContainer, moveRequired, insidePrimary, observer
var primaryInner, secondaryInner
(async () => {
console.log('Script Injected')
var preconnect1 = document.createElement("link");
preconnect1.rel = "preconnect";
preconnect1.href = "https://fonts.googleapis.com";
document.head.appendChild(preconnect1);
var preconnect2 = document.createElement("link");
preconnect2.rel = "preconnect";
preconnect2.href = "https://fonts.gstatic.com";
preconnect2.crossOrigin = "";
document.head.appendChild(preconnect2);
moveRequired = document.querySelector(window.innerWidth < 1000
? '#primary > #primary-inner > #below > #yf-container'
: '#secondary > #secondary-inner > #yf-container') == null;
// player = document.querySelector('#primary video')
// lyricContainer = document.querySelector('#columns #yf-container');
// observer = new MutationObserver(observePlayer);
// if(player != null){
// observer.observe(player, { attributes: true, attributeFilter: ['style'] });
// }
chrome.runtime.onMessage.addListener(async (obj, sender, res) => {
//NEW_SEARCH
const { type, val } = obj;
// console.log(obj)
var ytc = document.querySelector('#secondary > #secondary-inner')
switch (type) {
case 'NEW_SEARCH': {
if (!ytc) {
res({ 'name': '', 'channel': '' })
break;
}
var container = ytc.querySelector('.yf-container')
var progressbar = ytc.querySelector('#ytf-progressbar')
var nowPlaying = ytc.querySelector('.now-playing')
var music = document.querySelector('button-view-model a')
var isMusic = music === null ? false : (music.textContent == 'Music')
// player = document.querySelector('#primary video')
// if(player != null){
// observer.observe(player, { attributes: true, attributeFilter: ['style'] });
// }
if (container && container.getAttribute('data-uid') === val) {
res({ 'name': '', 'channel': '' })
break;
}
if(isMusic){
if (progressbar) progressbar.style.visibility = 'visible'
if (nowPlaying) nowPlaying.textContent = 'Searching -'
}
let title = document.querySelector('#above-the-fold > #title')?.textContent.trim();
let channel = document.querySelector('#upload-info > #channel-name > div > div')?.textContent.trim();
console.log(title + " - " + channel)
if (title && channel) {
res({ 'name': title, 'channel': channel })
}
else {
res({ 'name': '', 'channel': '' })
}
break;
}
case 'DISPLAY_LYRICS': {
// if(currentTab.isActive){
// }
break;
}
default: {
console.log('cjs : Unknown command')
}
}
})
})();
// function observePlayer() {
// if(lyricContainer == null){
// lyricContainer = document.querySelector(window.innerWidth < 1000 ? '#primary > #primary-inner > #below > #yf-container' : '#secondary > #secondary-inner > #yf-container');
// }
// if (insidePrimary && lyricContainer) {
// player.style.width = `${lyricContainer.style.width}px`;
// }
// }
function windowResize() {
insidePrimary = document.querySelector('#primary > #primary-inner > #below > #yf-container')
moveRequired = document.querySelector(window.innerWidth < 1000
? '#primary > #primary-inner > #below > #yf-container'
: '#secondary > #secondary-inner > #yf-container') == null;
if(moveRequired){
if (insidePrimary) {
moveDivToSecondary();
}
else {
moveDivToPrimary();
}
}
}
function moveDivToPrimary() {
lyricContainer = document.querySelector('#secondary > #secondary-inner > #yf-container');
if (lyricContainer) {
lyricContainer.classList.add('ytf-container-marginTop');
if (primaryInner == null) {
primaryInner = document.querySelector('#primary > #primary-inner > #below');
}
const firstChild = primaryInner.children[2];
if (firstChild) {
primaryInner.insertBefore(lyricContainer, firstChild);
}
}
}
function moveDivToSecondary() {
lyricContainer = document.querySelector('#primary > #primary-inner > #below > #yf-container');
if (lyricContainer) {
lyricContainer.classList.remove('ytf-container-marginTop');
if (secondaryInner == null) {
secondaryInner = document.querySelector('#secondary > #secondary-inner');
}
const firstChild = secondaryInner.firstChild;
if (firstChild) {
secondaryInner.insertBefore(lyricContainer, firstChild);
}
}
}
window.onresize = windowResize;