From 1b151e4c3ad34d17e530fd50053df3d860400a3e Mon Sep 17 00:00:00 2001 From: liwenrui Date: Wed, 28 Feb 2024 17:19:58 +0800 Subject: [PATCH] * tweak input injector for forms that renders late. --- module/ai/ui/inputinject.html.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/module/ai/ui/inputinject.html.php b/module/ai/ui/inputinject.html.php index 72e9cf1273..3899d7aad6 100644 --- a/module/ai/ui/inputinject.html.php +++ b/module/ai/ui/inputinject.html.php @@ -31,12 +31,15 @@ $inputInject = function() $this->app->loadLang('ai'); js(<<< JAVASCRIPT - window.injectToInputElement = (inputName, data, index) => + window.injectToInputElement = (inputName, data, index, tries = 0) => { - if(typeof index !== 'undefined') inputName = inputName + '[' + index + ']'; + let name = inputName; + if(typeof index !== 'undefined') name = name + '[' + index + ']'; - const inputEl = $('[name="' + inputName + '"]'); - if(!inputEl.length) return; + const inputEl = $('[name="' + name + '"]'); + + /* Retry if input is not found, sometimes form renders late. */ + if(!inputEl.length && tries < 5) return setTimeout(() => window.injectToInputElement(inputName, data, index, ++tries), 1000); const inputType = inputEl.prop('nodeName'); switch(inputType) // Contains case fallthroughs, on purpose. @@ -79,7 +82,7 @@ $inputInject = function() /* Textareas might be controlled by KindEditors. */ if(typeof KindEditor !== 'undefined' && KindEditor.instances.length) { - const editorInstance = KindEditor.instances.find(e => e.srcElement.attr('name') == inputName); + const editorInstance = KindEditor.instances.find(e => e.srcElement.attr('name') == name); if(editorInstance) { if(data === null) data = '';