Embedded js in PDF button using window var gets "Cannot read properties of undefined" error

WebViewer Version: 8.6.0

Do you have an issue with a specific file(s)? No (happens with numerous files)
Can you reproduce using one of our samples or online demos? No
Are you using the WebViewer server? No
Does the issue only happen on certain browsers? Fails in chrome and firefox (the only ones tested)
Is your issue related to a front-end framework? No
Is your issue related to annotations? Yes? (buttons defined in XFDF)

Please give a brief summary of your issue:
In a PDF with embedded buttons, the js in the button fails referencing ‘window’ properties; ‘window’ is undefined.

Please describe your issue and provide steps to reproduce it:
The XFDF for many of our PDFs contain buttons of this style:
<widget …>


window.top.document.getElementById(‘obsf:actionEmpSign1ofN’).click();

When this PDF+XFDF is loaded in WebViewer, and we click on the button, I see these errors in the Chrome browser console:
webviewer-core.min.js:215 Embedded JS error encountered, Mouse Up event on Field: TypeError: Cannot read properties of undefined (reading ‘top’)
webviewer-core.min.js:215 window.top.document.getElementById(‘obsf:actionAppSign1of1’).click();

In Firefox the messages are similar:
Embedded JS error encountered, Mouse Up event on Field: TypeError: window is undefined webviewer-core.min.js:215:453
window.top.document.getElementById(‘obsf:actionAppSign1of1’).click(); [webviewer-core.min.js:215:476]

This worked in v6.3.6; we’re in the process of trying to upgrade to v8.6.

The XFDF for the document is supplied via the WebViewer callback to the serverUrl supplied in the WebViewer constructor.

Please provide a link to a minimal sample where the issue is reproducible: N/A

Hello @Dwight

I will need a file with this embedded JS to reproduce this on my end. Can you send me one?

Yes, here’s a sample PDF file with one button that fails as described above.
SimpleButtonTest.zip (34.4 KB)

I also tried ‘turning off’ our XFDF loading, so all that happens is the WebViewer loads the PDF, and then when I click the button, I still see the same error.

Hello @Dwight

It looks like there is an issue with the embedded JS. window is not defined. Here is the issue when you open it with adobe acrobat:

I’m not sure what you are trying to do, but the embedded JS needs to be fixed.

Yes, window is undefined, but why? This WORKED in WebViewer 5 & 6, now it doesn’t. It appears that the environment supplied by WebViewer when executing the JS has broken this.

Hello @Dwight

I’ve just tried with the latest 6.3 and it shows the same error:

Which version are you using exactly in which this error is not shown?

Hello @Dwight

A colleague of mine reminded me that, basically, it’s not secure for embedded JS to allow access to the window, so at some point (by the 7.3 version and we ported back to 6.3), we disallowed that so that the environment would be consistent with the embedded JS environment in other PDF viewers.

So, in summary, it worked before as you’ve noted, but we blocked it since it’s a security concern.

It would be good to add that information to the “migrating to V7” document. It’s not a security issue for us because we do not allow end users to modify or upload documents, so the JS is strictly controlled by us. [Probably doesn’t matter, but I tested with v6.3.6 Build ‘Ny8yOS8yMDIwfDljMzNhYmIx’ and it worked. The Build id is different in your screen shot.]

We have dozens of documents that rely on this feature. How can we accomplish the same goal, which is to “click” a (hidden) button on the page that contains the webViewer?

Hello @Dwight

You could probably use this API: https://www.pdftron.com/documentation/web/guides/forms/embedded-js/#customization

The following code snippet should work:

Annotations.Forms.EmbeddedJS.update(scope => {
  scope.window = instance.UI.iframeWindow;
});

Hi @dfelix . Bear with me, I’m no JS expert. I inserted the code following the webviewer constructor like this:

	myWebViewer = new WebViewer({
		path: "../../lib",
		enableAnnotations: true,
		...
	}, viewerElement
	).then(instance => {
		console.log("webview instance");
		Annotations.Forms.EmbeddedJS.update(scope => {
			scope.window = instance.UI.iframeWindow;
			console.log('js updated');
		});
	});

The PDF loads, but I get this error in the browser console:
Uncaught (in promise) ReferenceError: Annotations is not defined
at forms.jsf:57:3

What am I missing?

Hello @Dwight

Sorry about that. “Annotations” is defined under “instance.Core”, so the full code snippet would be:

const { Annotations } = instance.Core;
Annotations.Forms.EmbeddedJS.update(scope => {
  scope.window = instance.UI.iframeWindow;
});

// ...OR...

instance.Core.Annotations.Forms.EmbeddedJS.update(scope => {
  scope.window = instance.UI.iframeWindow;
});

Hi @dfelix ,
Thanks, that fixed the ‘Annotation’ error, but now the PDF doesn’t load.

	myWebViewer = new WebViewer({ ...
			initialDoc: '../../_db_document/3103$RO/my test document.pdf.xod',
			documentType: 'xod',  ...
	).then(instance => {
		const { Annotations } = instance.Core;
		Annotations.Forms.EmbeddedJS.update(scope => {
			scope.window = instance.UI.iframeWindow; /* document load hangs if executed*/
			console.log('scope updated'); /* this always shows up */
		});
             ...
	});

If I comment out the “scope.window = …”, it works fine. But when that line is executed, the screen is left in a gray-disabled state with a circle. I do get my console.log output in either case, and my server gets the PDF URL request and responds. In the case where the document does not load, I do not get the callback for the XFDF via the DocumentXFDFRetriever I’ve specified. (Don’t think it’d make a difference, but note that we’re loading xod documents.) Here’s a screen shot.

Hello @Dwight

I can reproduce the issue. It looks like there are some undesirable side effects when you try to override a property called window since it’s a special case when we are talking about DOM.

We fixed the issue on our end and tomorrow’s nightly build for 8.6 will have this bugfix. You can download it here: https://www.pdftron.com/nightly/#stable/2022-07-08/webviewer/

Hi @dfelix ,
Thanks for the quick test & fix! But so far the nightly build page is empty. :frowning:

Hello @Dwight

It looks like we had some kind of issue with the publication of our nightly builds today. It’s there now: https://www.pdftron.com/nightly/#stable/2022-07-08/webviewer/

Hi @dfelix
The new build seems to be working great! Thanks so much for all of your efforts, and the quick fix!