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?
});
});
});
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?
});
});
});
Share
Improve this question
edited Mar 13, 2019 at 3:25
Erik Philips
54.6k11 gold badges131 silver badges156 bronze badges
asked May 8, 2015 at 9:28
EvaEva
3231 gold badge6 silver badges17 bronze badges
13
|
Show 8 more comments
2 Answers
Reset to default 16Here's an example of how to do it. I've based it on your code but simplified it a bit so that I'd have code that executes and illustrates how to do it. The following code reads foo.html
and adds the text modified!
to all p
element and then writes it out to out.html
. The main thing you were missing is window.document.documentElement.outerHTML
.
var jsdom = require("jsdom");
var fs = require("fs");
fs.readFile('foo.html', 'utf8', function(error, data) {
jsdom.env(data, [], function (errors, window) {
var $ = require('jquery')(window);
$("p").each(function () {
var content = $(this).text();
$(this).text(content + " modified!");
});
fs.writeFile('out.html', window.document.documentElement.outerHTML,
function (error){
if (error) throw error;
});
});
});
There's no jsdom.env()
anymore, and I think this example is easier to understand:
const fs = require('fs');
const jsdom = require('jsdom');
const jquery = require('jquery');
fs.readFile('1.html', 'utf8', (err, data) => {
const dom = new jsdom.JSDOM(data);
const $ = jquery(dom.window);
$('body').html('');
fs.writeFile('2.html', dom.serialize(), err => {
console.log('done');
});
});
$(document.body.getElementsByTagName("*"))
. First, it's a combination of jQuery and javascript selectors. It should be$("*")
in jQuery, ordocument.body.getElementsByTagName("*")
in javascript. Secondly, it's server-side code, so jQuery is not available here, unless I don't know about server-side implementation of jQuery. – Jeremy Thille Commented May 8, 2015 at 9:47$(document.body.getElementsByTagName("*"))
doesn't make sense and is probably invalid, even with jQuery loaded. So you're telling me you're running jQuery server-side? I searched around and I can see no place where this is used, or even saying it's possible at all. jQuery is made for DOM manipulationsm and there's no DOM on a server. There's something I don't understand here.fs
is a SERVER module, andread
andwrite
can only be achieved from a server, not a browser. Please confirm this is a server-side script ( = NOT run in a browser) and you're trying to use jQuery in it. – Jeremy Thille Commented May 8, 2015 at 10:54document.body.getElementsByTagName
does make no sense at all, because on a server, there is no DOM, there are no elements, and no tag (with no name). This just can't work, you're mixing front-end and back-end code. It's like mixing javascript and PHP together, hoping it will work, it's just impossible. – Jeremy Thille Commented May 8, 2015 at 11:00