Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save palashkulsh/1c10dc33c7b13ea6da7b1f518e60ec4e to your computer and use it in GitHub Desktop.
Save palashkulsh/1c10dc33c7b13ea6da7b1f518e60ec4e to your computer and use it in GitHub Desktop.
run this script after installing electron by electron index.js
const { BrowserWindow,app } = require('electron')
const fs = require('fs')
app.on('ready', function (){
let win = new BrowserWindow({ width: 800, height: 600 ,show:false})
win.loadURL('https://fs.blog/2014/04/the-heart-of-humanity/')
win.webContents.on('did-finish-load', () => {
// Use default printing options
win.webContents.insertCSS('.section-suggested-reading {display: none !important;} footer.section-footer {display: none !important;} .sidebar {display: none !important;} .navigation {display: none !important;} .sumome-share-client, .sumome-share-client-mobile-bottom-bar, .sumome-share-client-counts, .sumome-share-client-light .sumome-share-client-medium {display: none !important;} .section-suggested-reading-wrapper {display: none !important;}')
win.webContents.printToPDF({}).then(data => {
fs.writeFile('/tmp/print.pdf', data, (error) => {
if (error) throw error
console.log('Write PDF successfully.')
})
}).catch(error => {
console.log(error)
})
})
})
@palashkulsh
Copy link
Author

palashkulsh commented May 30, 2020

const fs = require('fs')
const deasync = require('deasync');
const URL = require('url');
const asynclib = require('async');

// app.on('before-quit', (ev)=>{
//   const hasUnsavedChanges = true; //Get from somewhere
//   if(true){
//     //I actually show a dialog asking if they should quit
//     ev.preventDefault();
//   }
// });

async function downloadUrlToFile(win,url,file){
  return new Promise(function (resolve,reject){
    win.loadURL(url)
    win.webContents.once('did-finish-load', async () => {
      // Use default printing options
      win.webContents.insertCSS('.section-suggested-reading {display: none !important;} footer.section-footer {display: none !important;} .sidebar {display: none !important;} .navigation {display: none !important;} .sumome-share-client, .sumome-share-client-mobile-bottom-bar, .sumome-share-client-counts, .sumome-share-client-light .sumome-share-client-medium {display: none !important;} .section-suggested-reading-wrapper {display: none !important;} iframe {display: none !important;}')

      win.webContents.printToPDF({}).then(data => {
        fs.writeFileSync(file, data)
        console.log('Write PDF successfully. '+url)
        return resolve();
      }).catch(error => {
        return reject(error);
      })
    })
  })
}

async function downloadUrls(urls){
  
  app.on('ready', async function (){
    let win = new BrowserWindow({ width: 800, height: 600 ,webPreferences: {javascript: false},show:false})
    //declaring handler
    let file='';
    for(index in urls){
      let u = URL.parse(urls[index]);
      console.log(urls[index])
      file = './pdf/'+u.pathname.split('/').reverse().join('_')+'.pdf';
      if(fs.existsSync(file)){
        console.log('file already found')
        continue;
      }
      await downloadUrlToFile(win,urls[index],file);
    }
    win.close();
  })  
}

async function downloadUrlsParallel(urls){
  return new Promise(async function (resolve, reject){
    async.eachLimit(urls, 7, async function (eachUrl, lcb){
      console.log(eachUrl)      
      await downloadUrls(win,eachUrl,'./pdf/'+u.pathname.split('/').reverse().join('_')+'.pdf');
      return lcb();
    }, function finalCb(){
      return resolve();
    })
  })
}

(async function(){
  if(require.main==module){
    // await downloadUrlToFile('https://fs.blog/2014/04/the-heart-of-humanity/', '/tmp/file.pdf')
    // await downloadUrlToFile('https://fs.blog/2014/12/albert-einstein-simplicity/','/tmp/file.pdf')
    // await downloadUrlToFile('https://fs.blog/2014/12/learned-helplessness/', '/tmp/file.pdf');
    // downloadUrls(['https://fs.blog/2011/10/the-default-choice-so-hard-to-resist/','https://fs.blog/2012/02/making-good-citizenship-fun-richard-thaler/'],'/tmp/multi.pdf')
    
    downloadUrls(require('./urls2.js'))
  }
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment