I want to load an HTML file (using fs.read), load the DOM using jsdom, and then change the text of the nodes of the body (via jquery). Then I want to save the edited DOM window as an HTML file. Is there a way to do this? The code I am using is the following:
fs.readFile(file, 'utf8', function(error, data) {
jsdom.env(data, [], function (errors, window) {
var $ = require('jquery')(window);
$(document.body.getElementsByTagName("*")).each(function () {
var content = $(this).text();
var word = "\\b"+wordSuggestions.word+"\\b";
var re = new RegExp(word, "g");
content = content.replace(re, wordSuggestions.suggestion);
$(this).text(content);
});
fs.writeFile(file, data, function (error){ // saving the new HTML file? What should I put instead of data? Window?
});
});
});