const { matchesGlob } = require('path');
const { exit } = require('process');
const { Builder, Browser, By, Key, until } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

async function example() {
    let options = new firefox.Options();
    //let profile = 'C:\\Users\\skh\\AppData\\Local\\Temp\\rust_mozprofilexIEnL6';
    //let profile = 'C:\\Users\\skh\\AppData\\Local\\Temp\rust_mozprofile6gaYwK';
    let profile = 'C:\\Users\\skh\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\7plwclek.default-release';
    options.setProfile(profile);
    let driver = await new Builder().forBrowser('firefox').
    withCapabilities({
        'browserName': 'firefox',
        'version': '',
        'platform': 'ANY',
        'javascriptEnabled': true
    }).setFirefoxOptions(options).build();
    //let driver = await new Builder().forBrowser(Browser.FIREFOX).build()
  try {
    const fs = require("fs");
    
    let ch_url = "https://www.youtube.com/";
    
    await driver.get(ch_url);
    await driver.sleep(3000);
    let session = await driver.getSession();
    console.log(session);
    
    var thisProf = session.getCapabilities().get('moz:profile');
    console.log(thisProf);
    await driver.sleep(10000);
    var content;
    var os = require('os');
    await driver.manage().getCookies().then(function (cookie) {
      content = "# Netscape HTTP Cookie File\r\n# http://curl.haxx.se/rfc/cookie_spec.html\r\n# This file was generated by Cookie-Editor\r\n";
      console.log(cookie);
      cookie.forEach(element => {
        var secure = element['secure'];
        var thisHttp = element['httpOnly'];

        if(thisHttp == false){
          content += element['domain']+"\t"+"TRUE"+"\t/\t"+secure.toString().toUpperCase()+"\t"+element['expiry']+"\t"+element['name']+"\t"+element['value']+"\n";
        }else{
          content += "#HttpOnly_"+element['domain']+"\t"+"TRUE"+"\t/\t"+secure.toString().toUpperCase()+"\t"+element['expiry']+"\t"+element['name']+"\t"+element['value']+"\n";
        }
        
      });
      console.log(content);
    });
    //const expandTabs = (str, count) => str.replace(/\t/g, " ".repeat(count));
    //content = expandTabs(content, 1);
    //fs.writeFileSync('C:\\Users\\skh\\Desktop\\test.txt', content.toString());
    fs.writeFileSync('', content.toString());

    
  } finally {
    //await driver.quit()
  }
}

example();
