-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
92 lines (77 loc) · 2.66 KB
/
preload.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
const electron = require('electron')
const ipcR = electron.ipcRenderer
const RiminderUrlUtils = require('./urlUtils.js')
window.onload = setTimeout(windowLoadedHandler, 50)
function getProfileOverlay () {
let elements = document.getElementsByClassName('pane___1BONj')
if (elements.length === 0) {
return null
}
return elements[0]
}
function getProfileOverlayIFrame (profileOverlay) {
let elements = profileOverlay.getElementsByTagName('iframe')
if (elements.length === 0) {
return null
}
return elements[0]
}
function getProfileOverlayAttachementContainer (profileOverlay) {
let elements = profileOverlay.getElementsByClassName('attachments')
let res = []
for (let i = 0, max = elements.length; i < max; i++) {
res.push(elements[i])
}
return res
}
let canFireEvent = true
function putListenerOnAttachements (profileOverlay) {
let attachments = getProfileOverlayAttachementContainer(profileOverlay)
attachments.forEach((element) => {
element.addEventListener('click', () => {
canFireEvent = true
})
})
}
function monitorOverlay () {
let profileOverlay = getProfileOverlay()
if (profileOverlay !== null) {
let ovIframe = getProfileOverlayIFrame(profileOverlay)
if (ovIframe !== null) {
let evt = new window.CustomEvent('onAttachementOverlay', {'detail': {iframe: ovIframe,
overlay: profileOverlay}})
if (canFireEvent) {
document.body.dispatchEvent(evt)
// to fire onAttachementOverlay event when the user want to see another
// pdf attachment (reset canFireEvent)
putListenerOnAttachements(profileOverlay)
canFireEvent = false
}
} else { canFireEvent = true }
} else { canFireEvent = true }
}
let lastPdf = 'lel'
function handleOnAttachementOverlayEvent (event) {
var ovIframe = event.detail.iframe
if (ovIframe !== null) {
// check if iframe's src is a pdf and if this is a diferent pdf from last call
// to avoid the untimely pdf reload
if (RiminderUrlUtils.isPdfLink(ovIframe.getAttribute('src')) &&
lastPdf !== ovIframe.getAttribute('src')) {
// send an event to main process
// to disrupt the pdf download when the user step on the page
ipcR.send('dlDisabler')
let newSrcUrl = RiminderUrlUtils.genPdfViewerUrlSrc(ovIframe.getAttribute('src'))
ovIframe.setAttribute('src', newSrcUrl)
lastPdf = ovIframe.getAttribute('src')
}
}
}
function windowLoadedHandler () {
// for the overlayReady event
// avoid the instant download and permit pdf support
setInterval(monitorOverlay, 10)
document.body.addEventListener('onAttachementOverlay', (event) => {
handleOnAttachementOverlayEvent(event)
})
}