Read HTML file using fs.read in UXP (UTF-8 format)
Copy link to clipboard
Copied
Hi All,
I'm using the UXP script to read the HTML file from the local folder. But I'm unable to read the file with UTF-8 format.
//=========================================
let myInDesign = require("indesign");
let app = myInDesign.app;
const uxpfs = require("uxp").storage;
const ufs = uxpfs.localFileSystem;
const ufs_frm = require('uxp').storage.formats;
var fs = require('fs');
const file = await ufs.getFileForOpening({mode: uxpfs.modes.readWrite, type: "file"});
if (!file) {
// no file selected
return;
}
var manifest_str = fs.readFileSync(file);
const myNovel = await file.read({format: formats.utf8});
if (window.DOMParser) {
const parser = new DOMParser();
var manifest = parser.parseFromString(manifest_str, "text/xml");
}
//=========================================
Kindly suggest any one, will appriciate.
Copy link to clipboard
Copied
I'm not JS guy - but maybe this will help?
http://www.java2s.com/ref/javascript/nodejs-fs-readfilesync-with-utf8-encoding.html
Copy link to clipboard
Copied
Hi Rober
Thank you, Actually you shared web site is belongs to NodeJS related. We already working with Nodejs. it is working to reading the UTF-8 format with HTML file.
We requesting to do in IDJS script. Kindly suggest.
Copy link to clipboard
Copied
Actually the Nodejs formats are not supporting in IDJS. Kindly suggest any one will appriciate.
Copy link to clipboard
Copied
I see an issue with your code, the read method invocation should be
const myNovel = await file.read({format: uxpfs.formats.utf8});
Now after this if I alert myNovel I get the text displayed. After that the control does not go into the if block, which points that window does not have the DOMParser property.
-Manan
Copy link to clipboard
Copied
Dear Manan Joshi,
Thank you,
Actually I tried that reading the html file using UTF-8. which you mentioned above. it is working fine now.
But need to read the HTML file with DOM parser based on only I can read the <div> elements inside the HTML.
//==========================================
let myInDesign = require("indesign");
let app = myInDesign.app;
const uxpfs = require("uxp").storage;
const ufs = uxpfs.localFileSystem;
const ufs_frm = require('uxp').storage.formats;
var fs = require('fs');
var DOMParser = require("uxp").DOMParser;
const file = await ufs.getFileForOpening({mode: uxpfs.modes.readWrite, type: "file"});
if (!file) {
// no file selected
return;
}
const text = await file.read({format: uxpfs.formats.utf8});
var parser = window.DomParser;
var dom = parser.parseFromString(text);
//==========================================
It shows the error for : Cannot read the properties of undefined (reading 'parseFromString')
So that, need your help. How to read the HTML file using UTF-8 with DOM parser or without DOM parser to read the HTML file with inside <div> elements.
Kindly help me to achive the issues.
Thanks & Regards,
Harihara Sudhan T R.,
Copy link to clipboard
Copied
Now that we have figured out that the Window object does not have the DOMParser property, it explains the error you get. The only way now is to search for a library that will do this, and I did suggest one in the other thread of yours and the possible way we could be able to use that. Please try that out and share the update.
-Manan
Copy link to clipboard
Copied
Dear Manan,
Thank you So much, 🙂 but that will work NodeJS. anyway we already done through NodeJS.
🙂 Actually, our firm want to conduct InDesign on its own. That is why we asked.
Is there any indication that Adobe will support complete NodeJS?
Thanks & Regards
Harihara Sudhan T R.,
Copy link to clipboard
Copied
@Harihara28692478zxj6 the Nodejs package is not something that would need the Nodejs env to work. If it contains just js then it might work for you. Now regarding the support for complete Nodejs API, I doubt that as per the link I shared.
-Manan
Copy link to clipboard
Copied
Dear Manan,
Thank you for your comments and clarification. But, Adobe will give the full support with IDJS - NodeJS then really happy to work with new tecc knowledge.
Thanks & Regards
Harihara Sudhan T R.,

