Skip to main content
Known Participant
August 2, 2023
Question

Read HTML file using fs.read in UXP (UTF-8 format)

  • August 2, 2023
  • 2 replies
  • 1694 views

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. 

This topic has been closed for replies.

2 replies

Community Expert
August 3, 2023

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

-Manan
Known Participant
August 4, 2023

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.,

 

 

 

Community Expert
August 5, 2023

Hi @Harihara28692478zxj6,

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.

https://community.adobe.com/t5/indesign-discussions/dom-parser-not-working-in-idjs-script/m-p/13983982#M536600

-Manan

-Manan
Robert at ID-Tasker
Legend
August 2, 2023
Known Participant
August 2, 2023

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.

Known Participant
August 2, 2023

Actually the Nodejs formats are not supporting in IDJS. Kindly suggest any one will appriciate.