简体   繁体   English

使用inAppBrowser选择DOM元素-Cordova

[英]Selecting DOM elements with inAppBrowser - Cordova

It seems I can't select DOM elements from a website I've loaded using the inAppBrowser plugin. 看来我无法从使用inAppBrowser插件加载的网站中选择DOM元素。 When I log document.getElementById("input") it's null . 当我记录document.getElementById("input")它为null The resources I've read on this have treated selecting elements as trivial, but I seem unable. 我在此阅读的资源将选择元素视为琐碎的事情,但我似乎无法做到。

Here's the code I'm using: 这是我正在使用的代码:

var obj = {
      link: "https://www.foolink.com",
      input: "barInput",
      func: function(obj) {
          document.getElementById("input").value = obj.input;
      }
}

var ref = cordova.InAppBrowser.open(obj.link, "_blank");
ref.addEventListener('loadstop', function() {

  ref.executeScript({code:obj.func(obj)})

})

Has anyone else encountered this before? 有人遇到过吗? Using jquery to get selectors on gives me selectors from the index.html file, not the inAppBrowser window. 使用jquery启用选择器可以为我提供来自index.html文件的选择器,而不是inAppBrowser窗口。

*edit: currently using inAppBrowser 1.4.0 and cordova 6.0.0 *编辑:当前正在使用inAppBrowser 1.4.0和cordova 6.0.0

This work for me (cordova 6.1.1, inappbrowser 1.4.0, platform Android): 这对我有用(cordova 6.1.1,inappbrowser 1.4.0,Android平台):

...
            ref.addEventListener('loadstop', function () {

            ref.executeScript(
                { code: "document.getElementById('inputID')" },
                function (values) {
                    alert(values[0]);
                }
            );

...

Frix33 got the gears turning with his answer, but I don't think it specifically identifies the problem so posting my own. Frix33的回答随着时间的推移而变化,但我认为它并没有明确指出问题,因此请发表自己的观点。 The script injected into the page must be a STRING or else it is just executed by the app, which is a different document hence the null result before. 注入页面的脚本必须是STRING,否则只能由应用执行,这是一个不同的document因此之前为null结果。 It works if you replace {code: obj.func(obj)} with: 如果将{code: obj.func(obj)}替换为:

var codePass = "document.getElementById('input').value = '"+ obj.input +"';"
var ref = cordova.InAppBrowser.open(obj.link, "_blank", "location=yes");
ref.addEventListener('loadstop', function(event) {

   ref.executeScript({code:codePass})

});

You can pass as many script lines as needed using that format. 您可以使用该格式传递任意数量的脚本行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM