【问题标题】:Method 'executeScript' not working - InAppBrowser方法“executeScript”不起作用 - InAppBrowser
【发布时间】:2019-05-22 23:03:28
【问题描述】:

在我的应用程序中,我正在尝试使用 InAppBrowser 的 executeScript 方法执行一些脚本。记住我使用的是 Ionic Framework v2,这是我的代码:

browser.executeScript({
            code: `
              $(".rodape.geral table tbody tr:nth-child(2) td:nth-child(1) div:nth-child(1)").css("min-width","auto");
              $(".rodape.geral table tbody tr:nth-child(2) td:nth-child(1) div:nth-child(1) div:nth-child(1)").css("width","auto");
              $(".rodape.geral table tbody tr:nth-child(1) td:nth-child(2) table:nth-child(1)").attr("align","center");
            `
          }).then((e)=>{ console.log('JS adicionado.'); }).catch((e)=>{ console.log('Erro ao adicionar JS. '+e); });

为什么代码不工作并且不返回回调?谢谢。

【问题讨论】:

    标签: cordova angular ionic-framework inappbrowser


    【解决方案1】:

    确保此代码位于 inAppBrowser 的 loadstop 事件侦听器中。

    在运行 JS 之前需要加载浏览器视图。

    【讨论】:

      【解决方案2】:

      我发现你需要在loadstop事件中添加这个,以便在网页加载后添加脚本或样式。

      browser.on('loadstop').subscribe(event => {
        browser.executeScript({
          code : 'your javascript code'
        }).then(() => {
          console.log("in executeScript then()");
        });
      });    
      

      【讨论】:

        【解决方案3】:

        问题在于executeScript 本身就是一个promise,所以要让你的代码正常工作,你必须写这样的东西:

        $rootScope.$on(’$cordovaInAppBrowser:loadstop’, function (e, event) {
                browser.executeScript({
                    code: `
                      $(".rodape.geral table tbody tr:nth-child(2) td:nth-child(1) div:nth-child(1)").css("min-width","auto");
                      $(".rodape.geral table tbody tr:nth-child(2) td:nth-child(1) div:nth-child(1) div:nth-child(1)").css("width","auto");
                      $(".rodape.geral table tbody tr:nth-child(1) td:nth-child(2) table:nth-child(1)").attr("align","center");
                    `
                  }).then((e)=>{ console.log('JS adicionado.'); }).catch((e)=>{ console.log('Erro ao adicionar JS. '+e); });
        });
        

        【讨论】:

        • 对我来说工作正常,但如果我想通过 getelementbyid 获取元素,如果元素不存在,回调不会触发,也不会捕获回调块,有什么想法吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-14
        • 2017-07-10
        • 1970-01-01
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多