-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpreload.js
171 lines (145 loc) · 5 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict'
const _ = require('lodash')
const electron = require('electron')
const ipc = electron.ipcRenderer
const winId = electron.remote.getCurrentWindow().id
let notiObj = null
function setStyle(config) {
// Style it
let notiDoc = global.window.document
let container = notiDoc.getElementById('container')
let appIcon = notiDoc.getElementById('appIcon')
let image = notiDoc.getElementById('image')
let close = notiDoc.getElementById('close')
let message = notiDoc.getElementById('message')
// Default style
setStyleOnDomElement(config.defaultStyleContainer, container)
// Size and radius
/*
let style = {
height: config.height - 2 * config.borderRadius - 2 * config.defaultStyleContainer.padding,
width: config.width - 2 * config.borderRadius - 2 * config.defaultStyleContainer.padding,
borderRadius: config.borderRadius + 'px'
}
setStyleOnDomElement(style, container)
// Style appIcon or hide
if (config.appIcon) {
setStyleOnDomElement(config.defaultStyleAppIcon, appIcon)
appIcon.src = config.appIcon
} else {
setStyleOnDomElement({
display: 'none'
}, appIcon)
}
*/
// Style image
// setStyleOnDomElement(config.defaultStyleImage, image)
// Style close button
setStyleOnDomElement(config.defaultStyleClose, close)
// Remove margin from text p
// setStyleOnDomElement(config.defaultStyleText, message)
}
function setContents(event, notificationObj) {
notiObj = _.cloneDeep(notificationObj)
// sound
if (notificationObj.sound) {
// Check if file is accessible
try {
// If it's a local file, check it's existence
// Won't check remote files e.g. http://
if (notificationObj.sound.match(/^file\:/) !== null
|| notificationObj.sound.match(/^\//) !== null) {
let audio = new global.window.Audio(notificationObj.sound)
audio.play()
}
} catch (e) {
log('electron-notify: ERROR could not find sound file: ' + notificationObj.sound.replace('file://', ''), e, e.stack)
}
}
let notiDoc = global.window.document
let appIconDoc = notiDoc.getElementById('appIcon')
if(notificationObj.cat === 'download') {
appIconDoc.src = 'img/next.png'
setStyleOnDomElement({display: 'block'}, notiDoc.getElementById('progressbarBackground'))
} else {
appIconDoc.src = 'img/next.png'
setStyleOnDomElement({display: 'none'}, notiDoc.getElementById('progressbarBackground'))
}
// Title
let titleDoc = notiDoc.getElementById('title')
titleDoc.innerHTML = notificationObj.title || ''
let au=notiDoc.getElementById('au');
au.play();
// message
let messageDoc = notiDoc.getElementById('message')
messageDoc.innerHTML = notificationObj.text || ''
// Image
let imageDoc = notiDoc.getElementById('image')
if (notificationObj.image) {
imageDoc.src = notificationObj.image
} else {
setStyleOnDomElement({ display: 'none'}, imageDoc)
}
//setSize
let size = notiDoc.getElementById('size')
size.innerHTML = notificationObj.size || ''
// Close button
let closeButton = notiDoc.getElementById('close')
closeButton.onclick = function(event) {
event.stopPropagation()
ipc.send('electron-notify-btnClose', winId, notificationObj)
}
// URL
let container = notiDoc.getElementById('container')
container.onclick = function() {
ipc.send('electron-notify-click', winId, notificationObj)
}
}
function setStyleOnDomElement(styleObj, domElement) {
try {
for (let styleAttr in styleObj) {
domElement.style[styleAttr] = styleObj[styleAttr]
}
} catch (e) {
throw new Error('electron-notify: Could not set style on domElement', styleObj, domElement)
}
}
function loadConfig(event, conf) {
setStyle(conf || {})
}
function reset() {
setStyleOnDomElement({width: '1%'}, global.window.document.getElementById('progressNow'))
global.window.document.getElementById('progressNow').text('')
global.window.document.getElementById('size').text('')
let notiDoc = global.window.document
let container = notiDoc.getElementById('container')
let closeButton = notiDoc.getElementById('close')
// Remove event listener
closeButton.onclick = null;
let newContainer = container.cloneNode(true)
container.parentNode.replaceChild(newContainer, container)
let newCloseButton = closeButton.cloneNode(true)
closeButton.parentNode.replaceChild(newCloseButton, closeButton)
}
function updateDownloadPercent(percent) {
if (percent < 0) {
ipc.send('electron-notify-close', winId, notificationObj)
} else {
setStyleOnDomElement({width: percent + '%'}, global.window.document.getElementById('progressNow'))
if(percent == 100) {
ipc.send('electron-notify-close', winId, notiObj)
}
}
}
ipc.on('electron-notify-set-contents', setContents)
ipc.on('electron-notify-load-config', loadConfig)
ipc.on('electron-notify-reset', reset)
ipc.on('electron-notify-download-update', (event, arg) => {
updateDownloadPercent(arg)
})
function log() {
console.log.apply(console, arguments)
}
delete global.require
delete global.exports
delete global.module