-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathloader.js
More file actions
69 lines (59 loc) · 2.05 KB
/
Copy pathloader.js
File metadata and controls
69 lines (59 loc) · 2.05 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
import { InPageUIBackground } from 'src/in-page-ui/background/index'
// For things that must only happen once
export function loader(promiseCreator) {
let promise
return (...args) => {
if (!promise) {
promise = promiseCreator(...args).then((res) => {
promise.loaded = true
return res
})
}
return promise
}
}
export function injectYoutubeContextMenu() {
const config = { attributes: true, childList: true, subtree: true }
const observer = new MutationObserver((mutation) => {
if (mutation[0].target.className === 'ytp-popup ytp-contextmenu') {
const panel = document.getElementsByClassName('ytp-panel-menu')[1]
const newEntry = document.createElement('div')
const highlightRenderer = new InPageUIBackground()
newEntry.setAttribute('class', 'ytp-menuitem')
newEntry.onclick = function () {
highlightRenderer.showSidebar()
}
newEntry.innerHTML =
'<div class="ytp-menuitem-icon"></div><div class="ytp-menuitem-label">Add Note to timestamp with Memex</div>'
panel.prepend(newEntry)
panel.style.height = '320px'
observer.disconnect()
}
})
observer.observe(document, config)
}
export const bodyLoader = loader(() => {
return new Promise((resolve) => {
if (document.readyState === 'complete') {
if (window.location.href.includes('youtube.com/watch?')) {
injectYoutubeContextMenu()
}
return resolve()
}
window.addEventListener('load', () => {
resolve()
})
})
})
export const interactiveLoader = loader(() => {
return new Promise((resolve) => {
if (document.readyState !== 'loading') {
return resolve()
}
document.addEventListener('readystatechange', () => {
if (document.readyState !== 'loading') {
return resolve()
}
})
})
})