forked from AleshaOleg/youquachoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (104 loc) · 2.85 KB
/
index.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
// To prevent multiple calls of adjustQuality from different watchers/listeners
let isAdjusting = false
let env
if (typeof browser !== 'undefined') env = browser
else env = chrome
const detectQuality = resolve => {
env.storage.sync.get('quality', ({ quality }) => {
if (!quality) {
env.storage.sync.set({ quality: 3 }, () => {
resolve(3)
})
return
}
env.storage.sync.set({ quality }, () => {
resolve(quality)
})
})
}
const clickToSettings = quality => new Promise((resolve, reject) => {
if (quality === -1) {
reject('disabled')
}
const video = document.querySelector('video.html5-main-video')
const videoReadinessInterval = setInterval(() => {
if (video.readyState > 0) {
document.querySelector('.ytp-settings-button').click()
clearInterval(videoReadinessInterval)
resolve()
}
}, 100)
})
const clickToQuality = () => {
const settingsElements = document
.querySelector('.ytp-settings-menu')
.querySelectorAll('.ytp-menuitem')
settingsElements[settingsElements.length - 1].click()
}
const pickQuality = () => {
env.storage.sync.get('quality', data => {
const qualityElements = document
.querySelector('.ytp-quality-menu')
.querySelectorAll('.ytp-menuitem')
const missedResolutions = 10 - qualityElements.length
let qualityToPick = data.quality - missedResolutions
if (qualityToPick < 0) qualityToPick = 0
qualityElements[qualityToPick].click()
isAdjusting = false
})
}
const onAdjustError = type => {
if (type === 'disabled') {
env.runtime.sendMessage({
action: 'updateIcon',
icon: '-1',
})
console.log('YouQuaChoo plugin disabled!')
return
}
env.runtime.sendMessage({
action: 'updateIcon',
icon: '-2',
})
throw new Error('YouQuaChoo: Something went wrong while changing quality.')
}
const adjustQuality = () => {
isAdjusting = true
new Promise(detectQuality)
.then(clickToSettings)
.then(clickToQuality)
.then(pickQuality)
.catch(onAdjustError)
}
(() => {
const onInitialLoad = setInterval(() => {
if (document.querySelector('.ytp-settings-button') && window.location.pathname === '/watch') {
adjustQuality()
clearInterval(onInitialLoad)
}
}, 100)
let pathname = '/'
// URL change watcher
setInterval(() => {
if (pathname !== window.location.pathname) {
({ pathname } = window.location)
if (window.location.pathname === '/watch' && !isAdjusting) {
adjustQuality()
}
}
}, 500)
env.runtime.onMessage.addListener(message => {
if (message.action === 'adjustQuality') {
if (window.location.pathname === '/watch') {
adjustQuality()
}
}
})
env.runtime.onMessage.addListener((msg, _, sendResponse) => {
if (msg.action === 'getDOM') {
sendResponse({
document,
})
}
})
})()