-
Notifications
You must be signed in to change notification settings - Fork 92
/
index.js
122 lines (103 loc) · 3.14 KB
/
index.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
const isRenderer = require('is-electron-renderer')
const electron = require('electron')
const path = require('path')
const readChunk = require('read-chunk')
const fileType = require('file-type')
const extend = require('deep-extend')
const got = require('got')
const BrowserWindow = isRenderer
? electron.remote.BrowserWindow : electron.BrowserWindow
const PDF_JS_PATH = path.join(__dirname, 'pdfjs', 'web', 'viewer.html')
function isAlreadyLoadedWithPdfJs (url) {
return url.startsWith(`file://${PDF_JS_PATH}?file=`)
}
function isFile (url) {
return url.match(/^file:\/\//i)
}
function getMimeOfFile (url) {
const fileUrl = url.replace(/^file:\/\//i, '')
const buffer = readChunk.sync(fileUrl, 0, 262)
const ft = fileType(buffer)
return ft ? ft.mime : null
}
function hasPdfExtension (url) {
return url.match(/\.pdf$/i)
}
function hasBlobData(url){
return url.match(/^blob:/i);
}
function isPDF (url) {
return new Promise((resolve, reject) => {
if (isAlreadyLoadedWithPdfJs(url)) {
resolve(false)
} else if (isFile(url)) {
resolve(getMimeOfFile(url) === 'application/pdf')
} else if (hasPdfExtension(url)) {
resolve(true)
} else if(hasBlobData(url)) {
resolve(true);
} else {
got.head(url).then(res => {
if (res.headers.location) {
isPDF(res.headers.location).then(isit => resolve(isit))
.catch(err => reject(err))
} else {
resolve(res.headers['content-type'].indexOf('application/pdf') !== -1)
}
}).catch(err => reject(err))
}
})
}
class PDFWindow extends BrowserWindow {
constructor (opts) {
super(extend({}, opts, {
webPreferences: { nodeIntegration: false }
}))
this.webContents.on('will-navigate', (event, url) => {
event.preventDefault()
this.loadURL(url)
})
this.webContents.on('new-window', (event, url) => {
event.preventDefault()
event.newGuest = new PDFWindow();
event.newGuest.loadURL(url);
event.newGuest.setMenu(null);
})
}
loadURL (url, options) {
return isPDF(url).then(isit => {
if (isit) {
return super.loadURL(`file://${
path.join(__dirname, 'pdfjs', 'web', 'viewer.html')}?file=${
decodeURIComponent(url)}`, options)
} else {
return super.loadURL(url, options)
}
}).catch(() => super.loadURL(url, options))
}
}
PDFWindow.addSupport = function (browserWindow) {
browserWindow.webContents.on('will-navigate', (event, url) => {
event.preventDefault();
browserWindow.loadURL(url);
event.newGuest.setMenu(null);
})
browserWindow.webContents.on('new-window', (event, url) => {
event.preventDefault()
event.newGuest = new PDFWindow();
event.newGuest.loadURL(url);
event.newGuest.setMenu(null);
})
const load = browserWindow.loadURL
browserWindow.loadURL = function (url, options) {
return isPDF(url).then(isit => {
if (isit) {
return load.call(browserWindow, `file://${PDF_JS_PATH}?file=${
decodeURIComponent(url)}`, options)
} else {
return load.call(browserWindow, url, options)
}
})
}
}
module.exports = PDFWindow