javascript: trustPropagatorFunction() crashes app  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
Mathew
User
Posts: 204
Joined: Thu Jun 19, 2014 7:30 pm

javascript: trustPropagatorFunction() crashes app

Post by Mathew »

I'm having trouble reading trying to read a text file saved in the /Javascripts/ folder into javascript. Maybe it's my failure: I can make it work fine in the console, but if it's part of a toolbar button or menu item, it fails because util.readFileIntoStream() does not have permission.

I've tried making a trusted function for this, but if I copy util.readFileIntoStream() into a trustedPropagatorFunction the application crashes. Here's some partial code to reproduce.

Code: Select all

// try to make a trust propagator function out of the readFileIntoStream function
var trustedReadFileFunction = app.trustPropagatorFunction( util.readFileIntoStream );

// make a trusted function to read the file
var trustedReadFile = app.trustedFunction( function(fileName) {
	var filePath = app.getPath("user","javascript")+"/"+fileName;
	app.beginPriv();
	var theFile = trustedReadFileFunction( filePath ); //  ***** CRASH ******
	app.endPriv();
	// do something with the file data
	var hLines = util.stringFromStream(theFile).replaceAll(/[\n\r]+/ig,"\n").split("\n");
	theFile = null;
	return hLines;
});

trustedReadFile("MyFileName.txt");
User avatar
Roman - Tracker Supp
Site Admin
Posts: 303
Joined: Sun Nov 21, 2004 3:19 pm

Re: javascript: trustPropagatorFunction() crashes app  SOLVED

Post by Roman - Tracker Supp »

Hello, Mathew,
The issue is caused by using the method util.readFileIntoStream as an unbound function.
This is a correct version of your code:

Code: Select all

// try to make a trust propagator function out of the readFileIntoStream function
var trustedReadFileFunction = app.trustPropagatorFunction( file => util.readFileIntoStream(file) );

// make a trusted function to read the file
var trustedReadFile = app.trustedFunction( function(fileName) {
	var filePath = app.getPath("user","javascript")+"/"+fileName;
	app.beginPriv();
	var theFile = trustedReadFileFunction( filePath ); //  ***** CRASH ******
	app.endPriv();
	// do something with the file data
	var hLines = util.stringFromStream(theFile).replaceAll(/[\n\r]+/ig,"\n").split("\n");
	theFile = null;
	return hLines;
});

trustedReadFile("MyFileName.txt");
Mathew
User
Posts: 204
Joined: Thu Jun 19, 2014 7:30 pm

Re: javascript: trustPropagatorFunction() crashes app

Post by Mathew »

ahhh. Thank you!
Mathew
User
Posts: 204
Joined: Thu Jun 19, 2014 7:30 pm

Re: javascript: trustPropagatorFunction() crashes app

Post by Mathew »

...although I still can't get PDFXchange to trust it. I get this when loading it as part of a toolbar:

App:Init:47: NotAllowedError: NotAllowedError: Security settings prevent access to this property or method.
Util.readFileIntoStream:47:App:Init
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8440
Joined: Wed Jan 03, 2018 6:52 pm

Re: javascript: trustPropagatorFunction() crashes app

Post by TrackerSupp-Daniel »

Hello, Mathew

To be sure, are you running the current latest release (9.5.368.0)? Older versions of the software did have more restrictions on security methods that could be used, and that may be part of the issue here. If you are updated and are still seeing this issue, please let me know and I will wave Roman over for his help again :)

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Mathew
User
Posts: 204
Joined: Thu Jun 19, 2014 7:30 pm

Re: javascript: trustPropagatorFunction() crashes app

Post by Mathew »

I just updated, and even in the latest version I can't make it work. It could well be something else like the folder I'm accessing.
User avatar
Roman - Tracker Supp
Site Admin
Posts: 303
Joined: Sun Nov 21, 2004 3:19 pm

Re: javascript: trustPropagatorFunction() crashes app

Post by Roman - Tracker Supp »

Hi Mathew,
Yes, our support for trusted functions is incomplete at the moment. We are going improve this in the next major release of the Editor. It is expected within the next month.
Post Reply