通过提示中的循环触发iFrame中的输入按钮(JavaScript)

Triggering Input Buttons within iFrames with a loop from a prompt (JavaScript)

本文关键字:按钮 输入 iFrame JavaScript 提示 循环      更新时间:2023-09-26

我是stackerflow的狂热用户,我总是可以指望数百万条帖子来找到解决方案,但不知何故,我被困在了这里,我可能只是累了哈哈。

忙于一些代码,以帮助将我们公司的工作发布到我们使用的一些有偿工作门户网站。(已经做了那个代码)

访问我们CRM中的页面受到限制。情况就是这样。

  • 我可以在顶级父中编辑JavaScript

  • 父级内部是iframe"app_win",内部是ifame"other_frame"

  • 我在顶级div中的代码在单击以下输入后提示用户输入total_jobs(input onclick="find_other_frame();"value="postjobs")

  • 模拟点击"other_frame"中的3个按钮

  • 模拟点击"app_winn"中的下一个按钮

我被困的地方。-如果我在每次迭代中添加一个警报,代码会很好地工作,但如果没有警报,它只执行一次。

<script language="JavaScript">

            function find_other_frame() {
                var total_jobs = Number(prompt("How Many Jobs are Currently open?"));
                var i = 0;
                while (i < total_jobs) {
                    var iframe1 = document.getElementById('app_win');
                    var innerDoc1 = iframe1.contentDocument || iframe1.contentWindow.document;
                    var iframe2 = innerDoc1.getElementById('other_frame');
                    var innerDoc2 = iframe2.contentDocument || iframe2.contentWindow.document;
                    innerDoc2.getElementById('button1').click(); //job site 1
                    innerDoc2.getElementById('button2').click(); //job site 1
                    innerDoc2.getElementById('button3').click(); //job site 1
                    // and then next job - basically a button called next job
                    innerDoc1.querySelector("#smartnav > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(4) > button:nth-child(1)").click(); 
                    i++;
                };
            };
</script>

为什么需要javascript暂停执行才能运行整个循环。是因为当在"app_win"中单击下一个按钮时,iframe被重新加载,而代码在"app_win"中找不到"other_frame"子iframe吗?

如果事情很简单,可以对我大喊大叫

我想我找到了解决方案——如果你认为它需要改进,请告诉我。

<script language="JavaScript">
            function find_other_frame_open()    {
                var total_jobs = Number(prompt("How Many Jobs would you like to Post?"));
                (function theLoop (i) {
                  setTimeout(function () {
                    var iframe1 = document.getElementById('app_win');
                    var innerDoc1 = iframe1.contentDocument || iframe1.contentWindow.document;
                    var iframe2 = innerDoc1.getElementById('other_frame');
                    var innerDoc2 = iframe2.contentDocument || iframe2.contentWindow.document;
                    innerDoc2.getElementById('wpv_1290391').click();
                    innerDoc2.getElementById('wpv_1290393').click();
                    innerDoc2.getElementById('wpv_1290340').click();
                    innerDoc1.querySelector("#smartnav > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(4) > button:nth-child(1)").click();
                    if (--i) { 
                      theLoop(i);
                    }
                  }, 4000); //4 second delay
                })(total_jobs);
            };
            function find_other_frame_close()   {
                var total_jobs = Number(prompt("How Many Jobs Need to Be Closed?"));
                (function theLoop (i) {
                  setTimeout(function () {
                    var iframe1 = document.getElementById('app_win');
                    var innerDoc1 = iframe1.contentDocument || iframe1.contentWindow.document;
                    var iframe2 = innerDoc1.getElementById('other_frame');
                    var innerDoc2 = iframe2.contentDocument || iframe2.contentWindow.document;
                    innerDoc2.getElementById('thePnetClose').click();                       
                    innerDoc1.querySelector("#smartnav > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(4) > button:nth-child(1)").click();
                    if (--i) {
                      theLoop(i);
                    }
                  }, 4000); //4 second delay
                })(total_jobs);
            };              
</script>