forked from hankbao/electron-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
123 lines (110 loc) · 3.75 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
'use strict'
const electron = require('electron')
const ipc = electron.ipcRenderer
const winId = electron.remote.getCurrentWindow().id
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) {
// 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
// Title
let titleDoc = notiDoc.getElementById('title')
titleDoc.innerHTML = notificationObj.title || ''
// 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)
}
// Close button
let closeButton = notiDoc.getElementById('close')
closeButton.addEventListener('click', function(event) {
event.stopPropagation()
ipc.send('electron-notify-close', winId, notificationObj)
})
// URL
let container = notiDoc.getElementById('container')
container.addEventListener('click', 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() {
let notiDoc = global.window.document
let container = notiDoc.getElementById('container')
let closeButton = notiDoc.getElementById('close')
// Remove event listener
let newContainer = container.cloneNode(true)
container.parentNode.replaceChild(newContainer, container)
let newCloseButton = closeButton.cloneNode(true)
closeButton.parentNode.replaceChild(newCloseButton, closeButton)
}
ipc.on('electron-notify-set-contents', setContents)
ipc.on('electron-notify-load-config', loadConfig)
ipc.on('electron-notify-reset', reset)
function log() {
console.log.apply(console, arguments)
}
delete global.require
delete global.exports
delete global.module