【问题标题】:Targeting input box in a external web page在外部网页中定位输入框
【发布时间】:2017-05-25 21:24:04
【问题描述】:

我是一个相当新的程序员,我需要一个建议,我如何才能做到这一点,

我的目标是能够在新窗口中打开一个新网站,例如 google.com,并在搜索输入框中输入一个字符串,而无需用户执行任何操作。

我已经看过很多想法如何实现,但似乎没有找到我可以使用的解决方案。

我做了一个按钮来触发这个javascript函数:

function OpenPopupCenter() {
     pageURL = 'http://www.google.com';
     title = 'temp title';
     w = (window.outerWidth * 0.67);
     h = (window.outerHeight * 0.80);

    var dualScreenLeft = typeof window.screenLeft !== "undefined" ? window.screenLeft : screen.left;
    var dualScreenTop = typeof window.screenTop !== "undefined" ? window.screenTop : screen.top;

    var width = window.innerWidth ? window.innerWidth :
    document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
    var height = window.innerHeight ? window.innerHeight :
    document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

    var left = ((width / 2) - (w / 2)) + dualScreenLeft -125;
    var top = ((height / 2) - (h / 2)) + dualScreenTop;
    var newWindow = window.open(pageURL, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

    $(newWindow).ready(function () {
        $("#lst-ib").val("Hello World");
    });

打开的窗口显示 google,但我无法通过 id lst-ib 定位输入框并将字符串“Hello World”插入其中,我做错了吗,谁能指导我找到正确的解决方案或想出一个可行的方法。

【问题讨论】:

  • 这听起来像是一项违反 JavaScript 跨域策略的任务,所以它可能不可行。请记住,可以控制另一个网站的网站可以访问敏感信息,例如银行信息或电子邮件。也许在“让我为你谷歌”网站上看到的“虚假”收购可以激发你的需求?

标签: javascript jquery target popupwindow


【解决方案1】:

在您的情况下,由于Same-origin policy,这是不可能的。引用相关资料:

根据该政策,网络浏览器允许包含在第一个网页中的脚本访问第二个网页中的数据,但前提是两个网页具有相同的来源

这是出于安全原因。如果没有该政策,任何人都可以(例如)编写一个脚本来打开您的银行账户网站并将您的所有资金转移给他们。

【讨论】:

    【解决方案2】:

    这里。当 Google 想要搜索某些内容时,它会使用 base urlhttp://google.com/search?q=。它在 '=' 之后搜索的内容,将 [spaces] 替换为 '+'。如果我想查找 'Genetic Algorithm',我会去掉空格并用 '+' 替换它们,所以它变成 'Genetic+Algorithm' 。然后,我将 join 与基本 url,因此 'http://google.com/search?q=Genetic+Algorithm' 将是最终结果。除了 'search?q=' 之外,您还可以执行其他操作,例如在末尾添加 '&source=lnms&tbm=isch&sa' 使其进行图像搜索。您可以将许多不同的标签用于不同的目的。我希望这有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-25
      • 2016-10-08
      • 2020-12-24
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多