The readPasswordsHtm
function reads an HTML file (passwords.htm
), extracts host and credential information from each table row, and saves the encrypted credentials to a JSON file.
The readPasswordsHtm
function reads an HTML file (passwords.htm
), extracts host and credential information from each table row using regular expressions. The extracted credentials are then saved to a JSON file in an encrypted format using the saveCredentials
function.
// add all passwords from passwords.html?
var importer = require('../Core');
var fs = require('fs');
var saveCredentials = importer.import("add encrypted passwords.json");
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var project = PROFILE_PATH + '/Documents/passwords.htm';
function readPasswordsHtm() {
var credentials;
var passwords = fs.readFileSync(project).toString('utf16le');
importer.regexToArray(/TBODY[^>]*>[\s\S]*?\/TBODY/ig, passwords).forEach(b => {
var creds = {};
creds['host'] = (((/subcaption[^>]*>(.*?)<\/td>/ig)
.exec(b) || [])[1] || '').replace(/<wbr>/ig, '').toLowerCase();
if (creds.host.trim() === '') {
return;
}
importer.regexToArray(/<tr>[\s\S]*?<\/tr>/ig, b).forEach(f => {
var key = (((/field[^>]*>(.*?)<\/td>/ig)
.exec(f) || [])[1] || '').replace(/<wbr>/ig, '');
var value = (((/wordbreakfield[^>]*>(.*?)<\/td>/ig)
.exec(f) || [])[1] || '').replace(/<wbr>/ig, '');
if (key.trim() !== '') {
creds[key] = value;
}
});
credentials = saveCredentials(creds);
});
return credentials;
};
module.exports = readPasswordsHtm;
// display login form and add to passwords.json?
// Import required modules
const path = require('path');
const fs = require('fs');
const importer = require('../Core');
// Define constants
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const PROJECT_PATH = path.join(PROFILE_PATH, 'Documents');
const PROJECT_FILE = path.join(PROJECT_PATH, 'passwords.htm');
const CREDENTIALS_FILE = path.join(PROJECT_PATH, 'encrypted passwords.json');
// Function to read passwords from HTML file
async function readPasswords() {
try {
// Read project file
const projectData = fs.readFileSync(PROJECT_FILE, 'utf16le');
// Extract table body from project data
const credentials = importer.regexToArray(/TBODY[^>]*>[\s\S]*?\/TBODY/ig, projectData);
// Process each table row
const result = await Promise.all(credentials.map(async (row) => {
try {
// Initialize credentials object
const creds = {};
// Extract host from row data
const host = (((/subcaption[^>]*>(.*?)<\/td>/ig)
.exec(row) || [])[1] || '').replace(/<wbr>/ig, '').toLowerCase();
if (host.trim() === '') {
return;
}
// Extract key-value pairs from row data
const keyValuePairs = importer.regexToArray(/<tr>[\s\S]*?<\/tr>/ig, row);
keyValuePairs.forEach((pair) => {
const key = (((/field[^>]*>(.*?)<\/td>/ig)
.exec(pair) || [])[1] || '').replace(/<wbr>/ig, '');
const value = (((/wordbreakfield[^>]*>(.*?)<\/td>/ig)
.exec(pair) || [])[1] || '').replace(/<wbr>/ig, '');
if (key.trim()!== '') {
creds[key] = value;
}
});
// Save credentials to JSON file
await saveCredentials(creds);
// Return updated credentials
return creds;
} catch (error) {
// Log any errors that occur during processing
globalThis.console.error(`Error processing row: ${error.message}`);
return null;
}
}));
// Return processed credentials
return result;
} catch (error) {
// Log any errors that occur during file reading
globalThis.console.error(`Error reading project file: ${error.message}`);
return null;
}
}
// Function to save credentials to JSON file
async function saveCredentials(credentials) {
try {
// Read existing credentials
const existingCredentials = fs.existsSync(CREDENTIALS_FILE)? JSON.parse(fs.readFileSync(CREDENTIALS_FILE, 'utf8')) : {};
// Update existing credentials with new credentials
existingCredentials[credentials.host] = credentials;
// Save updated credentials to JSON file
fs.writeFileSync(CREDENTIALS_FILE, JSON.stringify(existingCredentials, null, 2));
// Return saved credentials
return existingCredentials;
} catch (error) {
// Log any errors that occur during saving credentials
globalThis.console.error(`Error saving credentials: ${error.message}`);
return null;
}
}
// Export readPasswords function
module.exports = readPasswords;
Function Description
The readPasswordsHtm
function reads passwords from an HTML file (passwords.htm
) and extracts host and credential information from each table row.
Variables and Functions
importer
: An object that provides functions for importing and processing data.fs
: The built-in Node.js file system module.saveCredentials
: A function that saves encrypted passwords to a JSON file.PROJECT_PATH
: The path to the user's documents directory.project
: The path to the passwords.htm
file.Functionality
passwords.htm
file using fs.readFileSync
.TBODY
) from the HTML content.saveCredentials
.Export
The function is exported as a module using module.exports = readPasswordsHtm;
.