2

I have a web api project in .net core 2.2,

I've integrated swagger UI to this project and as per my requirement I need to do some java script validations for Username and password, So I configured java script in startup.cs as below:

`app.UseSwaggerUI(c =>
 {
   c.SwaggerEndpoint("/swagger/v1/swagger.json", "Mobile Insurance API");
   c.InjectJavascript("basic-auth-swagger.js");
   c.InjectJavascript("sha256.js");
   c.InjectJavascript("Base64.js");
});

The problem I am facing here is, the below javascript files are loading before swagger UI is loaded due to this I am unable to find javascript and perform validations as per my requirement.

So is there any way to load javascript after swagger UI is loaded. Thanks in advance !!!

Meena
  • 685
  • 8
  • 31

1 Answers1

3

I found a way to do this from here Make function wait until element exists

not sure if this is the best way but it works

// It takes the document a sec to load the swagger stuff loop until we find it
var checkExist = setInterval(function () {
    if (document.getElementById("swagger-ui") != null) {
        clearInterval(checkExist);

        // Add this method with your code
        DoSomethingWithElements();
    }
}, 100); // check every 100ms
Scott
  • 216
  • 3
  • 8
  • on further investigation the "swagger-ui" ID is loaded before the rest of the page, I changed my code to find an element by ID I know I have "operations-tag-User", you will need to change that to an element on your page. and be careful if you rename it the original code will keep looping until the page is closed – Scott Dec 11 '20 at 06:25
  • For the given circumstances. It is not a wrong solution. Little dirty but functional. I targeted .information-container. – AntiCZ Nov 15 '22 at 13:41