Repository: fraserxu/electron-pdf (js)

Issue:
extraHeaders no longer applies Accept-Language 
#210 Added support to pass along a value for `Accept-Language`.  Sometime after upgrading from Electron version `1.4.16` the current implementation broke.  

This needs to be reimplemented to use https://electronjs.org/docs/api/session#sessetuseragentuseragent-acceptlanguages

Pull request description:
Fixes #249 - Use session.setUserAgent instead of extraHeaders for setting acceptLang

Patch:
diff --git a/lib/exportJob.js b/lib/exportJob.js
index ec16123..b677847 100644
--- a/lib/exportJob.js
+++ b/lib/exportJob.js
@@ -356,13 +356,19 @@ class ExportJob extends EventEmitter {
     const loadOpts = {
       extraHeaders: _.join(this._getHeaders(), '\n')
     }
+
+    // #249 - acceptLang doesn't work when set on extraHeaders anymore
+    if (this.args.acceptLanguage) {
+      const s = window.webContents.session
+      s.setUserAgent(s.getUserAgent(), this.args.acceptLanguage)
+    }
+
     this.emit(`${RENDER_EVENT_PREFIX}loadurl`, { url: url })
     window.loadURL(wargs.urlWithArgs(url, null), loadOpts)
   }
 
   _getHeaders () {
     const headers = []
-    this.args.acceptLanguage && headers.push(`Accept-Language: ${this.args.acceptLanguage}`)
     this.args.disableCache && headers.push('pragma: no-cache')
     // Request headers to pass through
     if (this.args.requestHeaders) {
@@ -386,22 +392,21 @@ class ExportJob extends EventEmitter {
   }
 
   _saveDownload (win) {
-
-    const downloadPromise = new Promise( (resolve, reject) => {
-        win.webContents.session.on('will-download', (event, item, webContents) => {
-            // Set the save path, making Electron not to prompt a save dialog.
-            item.setSavePath(this.output)
-            item.once('done', (event, state) => resolve({event, state}))
-        })
+    const downloadPromise = new Promise((resolve, reject) => {
+      win.webContents.session.on('will-download', (event, item, webContents) => {
+        // Set the save path, making Electron not to prompt a save dialog.
+        item.setSavePath(this.output)
+        item.once('done', (event, state) => resolve({ event, state }))
+      })
     })
 
     this._renderAndCollectOutput(win, (win, inputIndex, outputDoneFn) => {
-      downloadPromise.then(({state}) => {
-          if (state === 'completed') {
-              this._emitResourceEvents(null, this.output, outputDoneFn)
-          } else {
-              this._emitResourceEvents(state, null, outputDoneFn)
-          }
+      downloadPromise.then(({ state }) => {
+        if (state === 'completed') {
+          this._emitResourceEvents(null, this.output, outputDoneFn)
+        } else {
+          this._emitResourceEvents(state, null, outputDoneFn)
+        }
       })
     })
   }
diff --git a/lib/sentry.js b/lib/sentry.js
index 9e924df..47aa0de 100644
--- a/lib/sentry.js
+++ b/lib/sentry.js
@@ -1,5 +1,4 @@
 if (process.env.SENTRY_DSN) {
-
   require('./logger').info('SENTRY_DSN environment variable detected, enabling Sentry...')
 
   const { init } = require('@sentry/electron')
diff --git a/package-lock.json b/package-lock.json
index 74329aa..c6afd5c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "electron-pdf",
-  "version": "4.0.5",
+  "version": "4.0.6",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index d34125a..10fc637 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "electron-pdf",
-  "version": "4.0.5",
+  "version": "4.0.6",
   "description": "A command line tool to generate PDF from URL, HTML or Markdown files",
   "main": "lib/index.js",
   "scripts": {


Test patch:
diff --git a/test/exportJob-test.js b/test/exportJob-test.js
index ec4daec..4bdc10f 100644
--- a/test/exportJob-test.js
+++ b/test/exportJob-test.js
@@ -105,7 +105,6 @@ test.only('_getHeaders all possible headers', t => {
   args.requestHeaders = `{"H1": "V1", "H2": "V2"}`
   const headers = job._getHeaders()
   const expected = [
-    'Accept-Language: en',
     'pragma: no-cache',
     'H1: V1',
     'H2: V2'
@@ -113,13 +112,6 @@ test.only('_getHeaders all possible headers', t => {
   t.deepEqual(headers, expected)
 })
 
-test('_getHeaders --requestHeaders single value only', t => {
-  args.requestHeaders = `{"H1": "V1"}`
-  const headers = job._getHeaders()
-  const expected = ['H1: V1']
-  t.deepEqual(headers, expected)
-})
-
 test('_getHeaders empty', t => {
   const headers = job._getHeaders()
   const expected = []