-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject.js
83 lines (72 loc) · 2.66 KB
/
inject.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
/* global config */
const {
theme
} = config
document.addEventListener('DOMContentLoaded', function () {
var script = `
scripts = [
'//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js'
]
const scriptsReady = Promise.all(scripts.map((src, index) => {
return new Promise(resolve => {
const element = document.createElement('script')
element.src = src
element.onload = () => resolve()
document.getElementsByTagName('head')[0].appendChild(element)
})
}))
styles = [
'//cdn.jsdelivr.net/highlight.js/9.9.0/styles/${theme}.min.css'
]
const stylesReady = Promise.all(styles.map((src, index) => {
return new Promise(resolve => {
const element = document.createElement('link')
element.rel = 'stylesheet'
element.href = src
element.onload = () => resolve()
document.getElementsByTagName('head')[0].appendChild(element)
})
}))
render = () => {
if (!document.querySelector('#msgs_div').ondblclick) {
document.querySelector('#msgs_div').ondblclick = event => {
const message = event.path.find(element => element.nodeName === 'TS-MESSAGE')
if (!message) return
message.querySelector('a[data-action="actions_menu"]').click()
document.querySelector('#edit_link').click()
}
}
Array.from(document.querySelectorAll('.message'))
.filter(element => !element.id.includes('xxx'))
.forEach(element => {
Array.from(element.querySelectorAll('pre.special_formatting:not(.hljs)'))
.forEach(element => {
const text = element.innerText
const [firstLine = false] = text.split('\\n') || []
if (firstLine && !firstLine.includes(' ') && hljs.getLanguage(firstLine)) {
const newContent = text.replace(firstLine + '\\n', '')
element.innerText = newContent
element.classList.add(firstLine)
}
hljs.highlightBlock(element)
})
})
}
Promise.all([scriptsReady, stylesReady]).then(() => {
render()
setInterval(() => {
render()
}, 1000)
})
`
setInterval(() => {
Array.from(document.querySelectorAll('webview'))
.filter(webview => !webview.getAttribute('data-loaded'))
.forEach(webview => {
webview.setAttribute('data-loaded', true)
webview.addEventListener('dom-ready', () => {
webview.executeJavaScript(script)
})
})
}, 1000)
})