Client-side API to modify Script Component content?

Posted by Timothy Williamson on 05-May-2017 08:01

Is it possible, via client-side APIs, to get the content of a Script Component...and then make changes to it?

I have a script component on a page and want to load the content from it, replace some text, then save those changes.

Ideally, I could then persist those changes and see them the next time i visited that page.

Posted by Thierry Ciot on 05-May-2017 10:51

No.  And that's a good thing otherwise users would be able to inject arbitrary code that would be saved and then executed by other users thus opening a security hole.

All Replies

Posted by Timothy Williamson on 05-May-2017 10:25

...or a way to programatically get the ID of the script component that a function is executing in.

Either or :)

Posted by Thierry Ciot on 05-May-2017 10:51

No.  And that's a good thing otherwise users would be able to inject arbitrary code that would be saved and then executed by other users thus opening a security hole.

Posted by Thierry Ciot on 05-May-2017 13:00

And to answer the question in second post, you can use document.currentScript

developer.mozilla.org/.../currentScript

Hope this helps, Thierry.

Posted by Timothy Williamson on 05-May-2017 14:36

What I meant was the Rollbase Script Component.

I'm trying to find a way to persist some state data for a particular Script Component. Here's an example:

There's a button. When you click the button, a modal dialog pops up. The dialog has a text box where you can enter some text. When you click "save" on the dialog, the value of that text box gets persisted.

The next time the script runs, it can access that data by querying for it. (Using something like the "original id" of the Script Component as the key)

The other piece of the puzzle is deleting the state data when a script component is removed from the page. Easy if the components could be edited via the client-side api...tougher if persisted elsewhere because there are no events that fire when a component is deleted :(

Posted by Thierry Ciot on 06-May-2017 09:32

Maybe one way to solve this would be to have your scripts and the associated states stored in an object?

Posted by Santosh Patel on 07-May-2017 09:54

developer.mozilla.org/.../sessionStorage

Have you explored browser's local/session storage? Your script component can persist data in component specific keys and retrieve them on demand. Note: to store complex data you can use json objects and then JSON.stringify() them to be stored in the storage.

Posted by Thierry Ciot on 07-May-2017 11:20

If you were to use session storage, the state wouldn't be shared to other users (not sure if it is a requirement of yours) and it wouldn't be shared if user logins from other device or other browser.

Thierry.

Posted by Timothy Williamson on 07-May-2017 17:01

I appreciate all the input.

Storing the info in another object was a good way to go. Now I can grab the same info (at runtime, and for any user) and use it to control rendering of the component.

Will post a demo of the technique and link it in this thread soon. :)

Posted by Timothy Williamson on 07-May-2017 19:22
This thread is closed