diff --git a/lib/swagger-initializer.js b/lib/swagger-initializer.js index 368158f..e8e9372 100644 --- a/lib/swagger-initializer.js +++ b/lib/swagger-initializer.js @@ -7,6 +7,26 @@ function swaggerInitializer (opts) { const logoData = `data:${opts.logo.type};base64,${logoBase64}` return `window.onload = function () { + function waitForElement(selector) { + return new Promise(resolve => { + if (document.querySelector(selector)) { + return resolve(document.querySelector(selector)); + } + + const observer = new MutationObserver(mutations => { + if (document.querySelector(selector)) { + observer.disconnect(); + resolve(document.querySelector(selector)); + } + }); + + // If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336 + observer.observe(document.body, { + childList: true, + subtree: true + }); + }); + } function resolveUrl(url) { const anchor = document.createElement('a') anchor.href = url @@ -35,12 +55,13 @@ function swaggerInitializer (opts) { const logoData = '${logoData}' if (logoData && resConfig.layout === 'StandaloneLayout') { - const link = document.querySelector('#swagger-ui > section > div.topbar > div > div > a') - const img = document.createElement('img') - img.height = 40 - img.src = logoData - link.innerHTML = '' - link.appendChild(img) + waitForElement('#swagger-ui > section > div.topbar > div > div > a').then((link) => { + const img = document.createElement('img') + img.height = 40 + img.src = logoData + link.innerHTML = '' + link.appendChild(img) + }) } ui.initOAuth(${serialize(opts.initOAuth)}) diff --git a/package.json b/package.json index 36dde1b..1669fc2 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "fs-extra": "^11.0.0", "qs": "^6.11.0", "standard": "^17.0.0", - "swagger-ui-dist": "5.10.5", + "swagger-ui-dist": "5.11.2", "tap": "^18.6.1", "tsd": "^0.30.0" },