Script content resetted when added to treeview

31 views
Skip to first unread message

Matteo Simionato

unread,
Oct 30, 2023, 6:41:52 AM10/30/23
to ZAP Developer Group
I have an add-on which is shipped with a script and this is the relevant code so far:

private ScriptWrapper scriptWrapper;

// I had to initailize script wrapper inside postInit(),
// cause I'd have gotten a NPE if I had initialized it right after the declaration.
// Am I overriding the proper method or there's a better one?
@Override
public void postInit() {
     super.postInit();
     File file = new File(Constant.getZapHome(), "example.kt");
     scriptWrapper = new ScriptWrapper(
             file.getName(),
             "Lorem ipsum",

             getExtScript().getEngineWrapper("kotlin"), // Is there a constant I can reference? (like TYPE_HTTP_SENDER)
             getExtScript().getScriptType(TYPE_HTTP_SENDER),
             false,
             file
     );
     getExtScript().addScript(scriptWrapper);
}

private JToggleButton getToggleButton() {
     if (toggleButton == null) {
         toggleButton = new JToggleButton();

         toggleButton.addItemListener(
                 e -> {
                     int state = e.getStateChange();

                     if (state == ItemEvent.SELECTED) {
                         getExtScript().setEnabled(scriptWrapper, true);
                     } else if (state == ItemEvent.DESELECTED) {

                         getExtScript().setEnabled(scriptWrapper, false);
                     }
                 });

        
// Init toggle button UI
     }
     return toggleButton;
}

The add-on builds without any errors and my script is shown in the treeview, but is empty.

empty.png

Its code should look like this:

import org.zaproxy.zap.extension.script.HttpSenderScriptHelper
import org.parosproxy.paros.network.HttpMessage

fun sendingRequest(msg: HttpMessage, initiator: Int, helper: HttpSenderScriptHelper) {
     print("sending request is called")
}

fun responseReceived(msg: HttpMessage, initiator: Int, helper: HttpSenderScriptHelper) {
     print(" responseReceived is called")
}

What am I doing wrong? Do you have any suggestions?
Thanks in advance and have a nice day!

thc...@gmail.com

unread,
Oct 30, 2023, 6:44:28 AM10/30/23
to zaproxy...@googlegroups.com
Where/how are you copying the script to the file system?

Best regards.

Matteo Simionato

unread,
Oct 30, 2023, 7:16:58 AM10/30/23
to ZAP Developer Group
Sorry thc202, I didn't understand your question.
The script is inside my add-on zapHomeFiles directory and is added to the treeview through ZAP's APIs.

thc...@gmail.com

unread,
Oct 30, 2023, 7:41:29 AM10/30/23
to zaproxy...@googlegroups.com
Ok, that answers the question, the file should be automatically copied
to the ZAP home then.

Call `scriptWrapper.reloadScript();` before adding the script to the
extension.


(Note that you still need to check if the script does not already exist
before adding it to the extension, otherwise that will fail if it does.)

Best regards.

Matteo Simionato

unread,
Oct 31, 2023, 11:22:24 AM10/31/23
to ZAP Developer Group
Now it works! This is the relevant code snippet:

@Override
public void postInit() {
    super.postInit();

    File scriptFile = new File(Constant.getZapHome(), "log.kt");

    scriptWrapper = new ScriptWrapper(
            scriptFile.getName(),
            "This is a logger",
            getExtScript().getEngineWrapper("kotlin"),
            getExtScript().getScriptType(TYPE_HTTP_SENDER),
            false,
            scriptFile
    );

    try {
        scriptWrapper.reloadScript();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (getExtScript().getScript(scriptFile.getName()) == null) {
        getExtScript().addScript(scriptWrapper);
    }
}

The only thing I don't understand is why Script Console tab got focus on application start? Is there a way to prevent this behaviour?

script.png

thc...@gmail.com

unread,
Oct 31, 2023, 2:30:03 PM10/31/23
to zaproxy...@googlegroups.com
Call `addScript(scriptWrapper, false)`.

Best regards.

Matteo Simionato

unread,
Nov 2, 2023, 6:40:15 AM11/2/23
to ZAP Developer Group
Awesome, now it works!
I only have one last question: is postInit() the right method to place my logic or is there a better method to override?

thc...@gmail.com

unread,
Nov 2, 2023, 6:51:17 AM11/2/23
to zaproxy...@googlegroups.com
Yes, that's a good place.

Best regards.
Reply all
Reply to author
Forward
0 new messages