Example - Component Using a Trusted Mode Resource
Let’s say you want to run some JavaScript code in trusted mode because LWS blocks an API that the JS code uses. You also want to access global properties created by that code from your component.
This example assumes that you’ve already performed the admin tasks in Set Up an Org to Elevate Scripts in Trusted Mode.
Here’s some JS code that you can upload to a static resource in your test org and save as myResourceName. This code creates three properties on the global window object, making them accessible from anywhere in the browser’s JavaScript environment.
Normally, LWS applies distortions to these global properties. LWS distortions restrict newData, newFunction and isFullScreen properties to the LWS sandbox’s global context, and block use of the document.fullscreen API.
You can’t use the myResourceName.js code in your component unless you elevate the code to trusted mode.
Here’s an example Lightning web component code that uses the myResourceName static resource that the admin elevated to trusted mode.
When calling the loadScript function, the component provides an additional config object as an argument. The config object consists of a trustedMode property set to true and a trustedGlobals property. The trustedGlobals property specifies the global properties newData, newFunction and isFullScreen that the component can access within the LWS sandbox. Because the component runs in the sandbox, and the trusted mode script runs outside it, these global properties must be available in the sandbox.
See Also