-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-selector-injected.js
79 lines (73 loc) · 2.25 KB
/
video-selector-injected.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
let playlist
// Any click should be captured and prevented; instead, a video should be added
const clickListener = e => {
let foundVideo = false
const linkFromAncestor = searchAncestorsForVideoLink(e.target)
if (linkFromAncestor) {
const videoId = getVideoIdFromHref(linkFromAncestor)
if (videoId) {
foundVideo = true
// Tell the background script to hit the Youtube API
chrome.runtime.sendMessage({func: 'addToPlaylist', video: videoId, playlist})
}
}
// TODO be smarter about clicks that arent supposed to be for videos and dont navigate; eg. the page background
if (!foundVideo) {
if (e.target.tagName.toLowerCase() !== 'a') {
return
}
alert('Was not a valid video link!')
}
e.preventDefault()
e.stopPropagation()
return false
}
const getVideoIdFromHref = href => {
if (href.includes('watch')) {
return new URLSearchParams(
new URL(href, location.origin).search
).get('v')
} else {
return href.split('/').slice(-1)[0]
}
}
// Recursively search up the DOM for a parent with an href
const searchAncestorsForVideoLink = elem => {
if (!elem) {
return null
}
const pathname = elem.href ? new URL(elem.href).pathname : ''
if (pathname.startsWith('/watch') || pathname.startsWith('/shorts')) {
return elem.href
}
return searchAncestorsForVideoLink(elem.parentElement)
}
const activate = () => {
document.addEventListener('click', clickListener, true)
}
const deactivate = () => {
document.removeEventListener('click', clickListener, true)
}
const messageListener = (msg, from) => {
if (msg.activate) {
activate()
} else if (msg.deactivate) {
deactivate()
playlist = null
}
if (msg.playlist) {
playlist = msg.playlist
}
if (msg.error) {
alert('Error: ' + msg.error)
}
}
chrome.runtime.onMessage.removeListener(messageListener)
chrome.runtime.onMessage.addListener(messageListener)
// If we've stored a playlist, then activate the script even without receiving a message
chrome.storage.local.get('playlist', data => {
if (data.playlist) {
playlist = data.playlist
activate()
}
})