【问题标题】:To get text present in a browser window获取浏览器窗口中的文本
【发布时间】:2014-07-06 15:42:01
【问题描述】:

我想在使用以下代码打开的窗口中显示文本

  var yy = window.open("http://www.vignanuniversity.org/");

现在我想在那个窗口中显示我使用的文本

            var responseText = yy.html();

我在 chrome 控制台中收到错误

      Protocols, domains, and ports must match.

所以我正在使用跨域,那么如何解决我的问题。

【问题讨论】:

  • 您可以使用诸如服务器端脚本之类的东西,诸如 php 之类的东西,然后调用该脚本并获得它的响应。否则,唯一的另一种方法是,如果外部域使用消息传递 api 并设置了一个命令来返回某些消息上的页面 html,我怀疑任何网站都会这样做。

标签: javascript jquery html cross-domain


【解决方案1】:

无法访问其他网页的窗口内容。但是,您可以让 javascript 在后端向您的服务器发出请求,以便为您发出请求。

Javascript

//Gets the contents of the web page using the backend server
getContents("http://www.vignanuniversity.org/", function(contents, responseCode){
    alert("Page received with status: " responseCode);
    alert(contents);
});

//Takes the URL as page and a function to handle the result.
//retFunc has one parameter, which is the response
function getContents(page, retFunc){
    var myServer = "/php/getRemote.php";
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4){
            retFunc(xmlhttp.responseText, xmlhttp.status);
        }
    }
    xmlhttp.open("GET", myServer + "?url=" + page);
    xmlhttp.send();
}

PHP

//Location: /php/getRemote.php
$url = $_GET["url"];
echo file_get_contents($url);

这将使您能够有效地抓取获取网站的内容。您将无法访问修改后的内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2012-05-21
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多