This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathpdfJS.js
56 lines (51 loc) · 2.02 KB
/
pdfJS.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const UrlUtil = require('../js/lib/urlutil')
const Filtering = require('./filtering')
const config = require('../js/constants/config')
const appActions = require('../js/actions/appActions')
const {getPathFromFileURI} = require('./common/lib/platformUtil')
const extensionState = require('./common/state/extensionState')
const appStore = require('../js/stores/appStore')
const fs = require('fs')
const path = require('path')
const getSetting = require('../js/settings').getSetting
const settings = require('../js/constants/settings')
const pdfjsBaseUrl = `chrome-extension://${config.PDFJSExtensionId}/`
const viewerBaseUrl = `${pdfjsBaseUrl}content/web/viewer.html`
const onBeforeRequest = (details) => {
const result = { resourceName: 'pdfjs' }
if (!(details.resourceType === 'mainFrame' &&
UrlUtil.isFileScheme(details.url) &&
UrlUtil.isFileType(details.url, 'pdf'))) {
return result
}
// Cancel and redirect to the PDF viewer URL
const extensions = extensionState.getExtensions(appStore.getState())
const extensionPath = extensions.getIn([config.PDFJSExtensionId, 'filePath'])
const pdfPath = getPathFromFileURI(details.url)
const pdfName = pdfPath.split('/').pop()
try {
const writeStream = fs.createWriteStream(path.join(extensionPath, 'tmp', pdfName))
writeStream.on('close', () => {
const pdfChromeUrl = `${pdfjsBaseUrl}tmp/${pdfName}`
const viewerUrl = `${viewerBaseUrl}?file=${encodeURIComponent(pdfChromeUrl)}#${pdfPath}`
appActions.loadURLRequested(details.tabId, viewerUrl)
})
fs.createReadStream(pdfPath).pipe(writeStream)
} catch (e) {
return result
}
result.cancel = true
return result
}
/**
* Load PDF.JS
*/
module.exports.init = () => {
if (getSetting(settings.PDFJS_ENABLED)) {
Filtering.registerBeforeRequestFilteringCB(onBeforeRequest)
}
}