forked from gerhardberger/electron-pdf-window
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (115 loc) · 3.15 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
123
124
125
126
127
128
129
130
131
132
133
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 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 {
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);
});
}
loadURL(url, options) {
isPDF(url)
.then(isit => {
if (isit) {
super.loadURL(
`file://${path.join(
__dirname,
"pdfjs",
"web",
"viewer.html"
)}?file=${decodeURIComponent(url)}`,
options
);
} else {
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);
});
browserWindow.webContents.on("new-window", (event, url) => {
event.preventDefault();
event.newGuest = new PDFWindow();
event.newGuest.loadURL(url);
});
const load = browserWindow.loadURL;
browserWindow.loadURL = function(url, options) {
isPDF(url).then(isit => {
if (isit) {
load.call(
browserWindow,
`file://${PDF_JS_PATH}?file=${decodeURIComponent(url)}`,
options
);
} else {
load.call(browserWindow, url, options);
}
});
};
};
module.exports = PDFWindow;