-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
144 lines (129 loc) · 4.63 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
const {contextBridge, ipcRenderer} = require('electron')
const sharp = require('sharp')
const path = require('path')
const fs = require('fs')
window.addEventListener('DOMContentLoaded', ()=>{
window.addEventListener('message', evt => {
if (evt.data.type === 'select-dir') {
let ipc = ipcRenderer.sendSync('select-dir')
if(ipc.length > 0 ){
document.querySelector('#path_value').innerText = ipc[0]
}
}
})
})
contextBridge.exposeInMainWorld("imageMetaDataAPI" , async(path) =>{
const metadata = await sharp(path).metadata()
const {width , height} = metadata
const resolution = { width , height }
return resolution
})
contextBridge.exposeInMainWorld("favvyExportAPI", async (src , website_name , theme_color, output_path)=>{
const sharpImgInst = sharp(src)
const dir_name = `favvy-desktop-${new Date().toISOString().replace('T' , '-').replaceAll(':' ,'').slice(0,-5)}/`
const dir_path = path.join(output_path , dir_name)
fs.mkdir(dir_path , (err)=>{
if(err){
return
}
})
// creating the webmanifest file
let webmanifestContent = `
{
"name": "${website_name}",
"short_name": "${website_name}",
"icons": [
{
"src": "./android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "./android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "${theme_color}",
"background_color": "${theme_color}",
"display": "standalone"
}
`
fs.writeFile(dir_path+'site.webmanifest' , webmanifestContent , (err)=>{
if(err){
return
}
})
// creating the browserconfig file
let browserconfigContent = `
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="/mstile-150x150.png"/>
<TileColor>${theme_color}</TileColor>
</tile>
</msapplication>
</browserconfig>
`
fs.writeFile(dir_path+'browserconfig.xml' , browserconfigContent , (err)=>{
if(err){
return
}
})
/*
Creating the head.html file
- contains details of how link favicons in index.html
*/
let headHtmlContent = `
<head>
<!-- Created with favvy-desktop -->
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x-32x.png.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x-16x.png">
<link rel="manifest" href="/site.webmanifest">
<meta name="msapplication-config" content="/browserconfig.xml">
<meta name="msapplication-TileColor" content="${theme_color}">
<meta name="theme-color" content="${theme_color}">
</head>
`
fs.writeFile(dir_path+"head.html" , headHtmlContent , (err)=>{
if(err){
return
}
})
// all favicons to be created
const favicons = [
{
width:16 ,
height:16,
name:'favicon-16x-16x.png'
},
{
width:32 ,
height:32,
name:'favicon-32x-32x.png'
},
{
width:48 ,
height:48,
name:'favicon.png'
},
{
width:150 ,
height:150,
name:'mstile-150x-150x.png'
},
{
width:192 ,
height:192,
name:'andriod-chrome-192x-192x.png'
},
{
width:512 ,
height:512,
name:'andriod-chrome-512x-512x.png'
},
]
favicons.forEach(favicon => sharpImgInst.resize({width:favicon.width , height:favicon.height}).png().toFile(dir_path+favicon.name))
})