0

I'm creating a small npm Pomodoro timer project. When I start the project, it starts a timer, and added a list of websites to /etc/hosts file, when I terminate the project, that list is removed.

My code works

// all the variables
let OriginalfileContents, all
//  console.log( blockedWebsites')
fs.readFile(filePath, (err, data) => {
      OriginalfileContents = data.toString()
     all = OriginalfileContents + blockedWebsites
       fs.writeFileSync(filePath, all , 'utf-8');
 }) 
function exitHandler(options, exitCode) {
    if (options.cleanup) console.log('clean');
    if (exitCode || exitCode === 0) {
       var all = OriginalfileContents + blockedWebsites
       fs.writeFileSync(filePath, OriginalfileContents , 'utf-8');
    }
    if (options.exit) process.exit();
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));

So upon sudo node blocker.js, /etc/hosts get the following

127.0.0.1  facebook.com
127.0.0.1  www.facebook.com
127.0.0.1  quora.com
127.0.0.1  www.quora.com

And when I control c, those lines are removed.

I there a way to get chrome to read the changes without closing the entire browser

relidon
  • 1,394
  • 2
  • 16
  • 27
  • This is probably up to the OS, not Chrome. Chrome requests a DNS lookup from the OS and the OS decides when it's going to look for any updates in `/etc/hosts`. There are likely DNS caches in the OS and possibly in Chrome too. This [article](https://www.howtogeek.com/197804/how-to-clear-the-google-chrome-dns-cache-on-windows/) discusses how to flush the DNS cache from within the browser, but I don't think it's something you can do programmatically (from Javascript) and I have no idea if it affects when `/etc/hosts` is re-read by the OS. – jfriend00 Jun 28 '20 at 00:09

0 Answers0