Skip to content

Commit

Permalink
refactor(pdf): force dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed May 27, 2021
1 parent a8df19f commit f7d9f2c
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions scripts/pdf.js
Expand Up @@ -43,7 +43,7 @@ async function startUpgrade() {
await Promise.all(files.map(p => exists(path.join(__dirname, repoRoot, p))))

shell.echo('\nModifying files.')
await Promise.all([modifyPDFJS(), modifyPDFWorker(), modifyViewrJS(), modifyViewerHTML()])
await Promise.all([modifyViewrJS(), modifyViewerHTML()])

await fs.ensureDir(publicPDFRoot)

Expand All @@ -59,24 +59,6 @@ async function startUpgrade() {
shell.echo('\ndone.')
}

async function modifyPDFJS() {
const pdfPath = path.join(__dirname, repoRoot, 'build/pdf.js')
let file = await fs.readFile(pdfPath, 'utf8')

file = removeRegeneratorPolyfill('pdf.js', file)

await fs.writeFile(pdfPath, file)
}

async function modifyPDFWorker() {
const workerPath = path.join(__dirname, repoRoot, 'build/pdf.worker.js')
let file = await fs.readFile(workerPath, 'utf8')

file = removeRegeneratorPolyfill('pdf.worker.js', file)

await fs.writeFile(workerPath, file)
}

async function modifyViewrJS() {
const viewerPath = path.join(__dirname, repoRoot, 'web/viewer.js')
let file = await fs.readFile(viewerPath, 'utf8')
Expand Down Expand Up @@ -104,20 +86,17 @@ async function modifyViewrJS() {
'/* saladict */let validateFileURL = () => {};'
)

file = removeRegeneratorPolyfill('viewer.js', file)
// force dark mode
const viewCssTester = /"viewerCssTheme": 0,/
if (!viewCssTester.test(file)) {
shell.echo('Could not locate viewerCssTheme config in viewer.js')
shell.exit(1)
}
file = file.replace(viewCssTester, '"viewerCssTheme": 2, /* saladict */')

await fs.writeFile(viewerPath, file)
}

function removeRegeneratorPolyfill(name, file) {
// remove regenerator polyfill which triggers 'unsafe-eval' CSP
const regeneratorTester = /(^| )Function\("r"/
if (!regeneratorTester.test(file)) {
shell.echo(`Could not locate regenerator polyfill in ${name}`)
}
return file.replace(regeneratorTester, '/* saladict */ // Function("r"')
}

async function modifyViewerHTML() {
const viewerPath = path.join(__dirname, repoRoot, 'web/viewer.html')
let file = await fs.readFile(viewerPath, 'utf8')
Expand Down

0 comments on commit f7d9f2c

Please sign in to comment.