可以使用scrollIntoView()方法让网页对位到txt2imgGallery的位置,可以在txt2imgGallery.focus();后添加以下代码实现:

txt2imgGallery.scrollIntoView();

关于txt2img_textarea.focus(),这行代码是将焦点放在文本框中,如果删除这行代码,则不会自动将焦点放在文本框中,需要手动点击文本框才能输入内容。如果不需要输入文本内容,可以删除这行代码,对程序不会有影响。

以下是优化后的代码:

function clicktxt2img() {
    var button1Skip = document.body.querySelector("#txt2img_skip");
    // var button1Skip = gradioApp().querySelector("#txt2img_skip");
    if (isHide(button1Skip)) {
        console.log(lines[i]);

        var button1 = document.body.querySelector("#txt2img_generate");

        if (lines[i] != "") {
            var txt2img_textarea = document.body.querySelector(
                "#txt2img_prompt > label > textarea"
            );
            // txt2img_textarea.focus();  // 移除文本框对焦
            txt2img_textarea.value = lines[i];

            txt2img_textarea.dispatchEvent(new Event("input", { bubbles: true }));

            button1.click();
            
            txt2imgGallery.focus();
            txt2imgGallery.scrollIntoView(); // 对位到txt2imgGallery
        }
        i += 1;

        // 不在文本框里输入的话 就相当于点击了生成按钮
        if(myInputText.value == '') {
            button1.click();
            
            txt2imgGallery.focus();
            txt2imgGallery.scrollIntoView(); // 对位到txt2imgGallery
        }

        console.log("完成了:" + i);
    }

    if (i >= lines.length) {
        toggleButton1.disabled = false;
        clearInterval(vet1);
    }
}
JavaScript代码优化:网页对位到txt2imgGallery并移除文本框对焦

原文地址: https://www.cveoy.top/t/topic/ogtg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录