Using javascript to run a function in a different directory.

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

Berjan
User
Posts: 3
Joined: Tue Feb 20, 2024 12:53 pm

Using javascript to run a function in a different directory.

Post by Berjan »

Hi,

I'm trying to get a script to work that is not in the JavaScripts folder.
When I search for a way to do that it says to use the

Code: Select all

import {} from "path"
function, but that gives me the following error:

App:Init:1: SyntaxError: import declarations may only appear at top level of a module
import CalculateStempel from "./Pathfile/PDFCalculatieMain.js"

I tried the

Code: Select all

import("path").then("do this")
function next, It doesn't give me an error but it doesn't work.

This is the code I have in the JavaScripts folder, I copied part of the hatch tool and changed some things.

Code: Select all

var iconSet_hatchTool = { hatch:
	iconStream:function(val){let data=this[val];
		return {count:0, width:20, height:20, read:function(nBytes){return data.slice(this.count,this.count+=2*nBytes)}}}
};

// This adds a button to the Add-on Tools toolbar
app.addToolButton( {
	cName: "CalculationTool",
	cLabel: "Calculatie",
	oIcon: iconSet_hatchTool.iconStream("hatch"),
	cTooltext: "Calculeer stempels",
	cExec: "getCalCode(this)" }
);

// This adds a menu item
app.addMenuItem( {
	cName: "Calculation",
	cUser: "Calculatie…",
	oIcon: iconSet_hatchTool.iconStream("hatch"),
	cParent: "Comments",
	cExec: "getCalCode(this)" }
);

function getCalCode(t){

	import("path").then(CalculateStempel(t));

	console.println(CalculateStempel + " defined")

	console.println("works")
}
the other file has

Code: Select all

export function CalculateStempel(t){}
as it's name.

So my question is if it's possible to run a function that isn't in the JavaScripts folder?

Kind regards,
Berjan
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: Using javascript to run a function in a different directory.

Post by Tracker Supp-Stefan »

Hello Berjan,

I do not think it is possible, but will ask our devs for confirmation.
May I ask why do you need to run such external code and what is the set up that would require it?

Kind regards,
Stefan
Berjan
User
Posts: 3
Joined: Tue Feb 20, 2024 12:53 pm

Re: Using javascript to run a function in a different directory.

Post by Berjan »

Hi Stefan,

The code that I have made is still being developed. The people that use the code are the ones that are testing it for bugs and they give me things they want to see or have the code do.

As such I update the code regularly and having to go to different people every time I update it is time consuming, if I only have to update 1 file and all users automactically get the correct code it saves me alot of time.

As for setup, I don't fully understand what you mean with that but this is what I think you want to know:
We have 1 server which everyone has access to. I want to put the code that needs to be run on there as a .js file. It's location will remain the same.

I hope that this answers the questions.

Kind regards,
Berjan
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8624
Joined: Wed Jan 03, 2018 6:52 pm

Re: Using javascript to run a function in a different directory.

Post by TrackerSupp-Daniel »

Hello, Berjan

As Stefan says, I do not believe that it is possible for JS to be executed remotely in any capacity by a PDF for this purpose, for security reasons. In PDF, due in part to the popularity of the spec, such actions are restricted to prevent dangerous code execution by most applications, us included, and JS remains almost entirely self contained. There may be some exceptions which I am unaware of, and hopefully the Dev team will come along and correct me once they get a chance.

You will most likely need to deploy the updated JS files to users, directly into their JS folders, while they are not actively using the software (note that if the app is running while the JS is replaced, it will not properly go into effect. They will need to restart to see any updated JS files)


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
Berjan
User
Posts: 3
Joined: Tue Feb 20, 2024 12:53 pm

Re: Using javascript to run a function in a different directory.

Post by Berjan »

Hello Daniel,

I did kind of expect it. If they do know a way to do it, Please let me know.

Kind regards,
Berjan
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Using javascript to run a function in a different directory.

Post by Tracker Supp-Stefan »

:)