Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从web url加载元数据,就像我们在状态更新时发布url时facebook所做的一样。。。?_Javascript_Ajax_Angularjs - Fatal编程技术网

Javascript 如何从web url加载元数据,就像我们在状态更新时发布url时facebook所做的一样。。。?

Javascript 如何从web url加载元数据,就像我们在状态更新时发布url时facebook所做的一样。。。?,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,我想使用AngularJS/Javascript从网站url加载元数据,如标题、徽标、描述,类似于facebook状态更新功能,在粘贴网站url时加载元数据。有什么API吗?我相信它不是API;但是,一种智能算法可以在后台读取元数据并为您创建视图 你应该也能做到 对URL进行HTTP访问并读取元数据 <meta property="og:image" content="[url to image]" /> <meta property="og:title" content="[

我想使用AngularJS/Javascript从网站url加载元数据,如标题、徽标、描述,类似于facebook状态更新功能,在粘贴网站url时加载元数据。有什么API吗?

我相信它不是API;但是,一种智能算法可以在后台读取元数据并为您创建视图

你应该也能做到

对URL进行HTTP访问并读取元数据

<meta property="og:image" content="[url to image]" />
<meta property="og:title" content="[page name]" />
<meta property="og:description" content="[content description]" />

实际上,使用HTML5代理可以:

用于仅读取此示例的所有属性的实用程序函数:

(function($) {
    $.fn.getAttributes = function() {
        var attributes = {}; 

        if( this.length ) {
            $.each( this[0].attributes, function( index, attr ) {
                attributes[ attr.name ] = attr.value;
            } ); 
        }

        return attributes;
    };
})(jQuery);
代码:

$.get(
    'http://www.corsproxy.com/' +
    'en.wikipedia.org/wiki/Cross-origin_resource_sharing',
    function(response) {
        var a = $(response);
        for(var i = 0; i < a.length; i++) {
            if (typeof $(a[i]).prop("tagName") != 'undefined' && $(a[i]).prop("tagName").toLowerCase() == 'meta')
            console.log($(a[i]).getAttributes());
        }
});
工作原理:corsproxy.com上的代理使用CORS HTML5功能解决同源策略的问题。仔细阅读这个答案

然后从页面创建jQuery对象,搜索所有元标记并获取它们的属性,然后选择您需要的属性

如果您害怕使用第三方网站,那么唯一的方法就是在服务器上创建一个脚本,通过AJAX向其发送url,将页面加载到服务器并返回到客户端,然后查找所有元标记

如果您不想加载整个页面,则只能加载包含标记的第一部分-根据HTML规范,所有标记都应位于其中:

var xhr = new XMLHttpRequest();

xhr.addEventListener("progress", updateProgress, false);
xhr.addEventListener("load", transferComplete, false);
xhr.addEventListener("error", transferFailed, false);
xhr.addEventListener("abort", transferCanceled, false);

xhr.open("GET", 'http://www.corsproxy.com/' +
    'en.wikipedia.org/wiki/List_of_law_clerks_of_the_Supreme_Court_of_the_United_States?1234', true);
xhr.send(null);
var nextLine = 0;
var resp = '';

// progress on transfers from the server to the client (downloads)
function updateProgress(evt) {
    resp = xhr.response.slice(nextLine);
    console.log(resp.length);
    if (resp.indexOf('</head>') != -1) {
        var a = $(resp);
        for(var i = 0; i < a.length; i++) {
            if (typeof $(a[i]).prop("tagName") != 'undefined' && $(a[i]).prop("tagName").toLowerCase() == 'meta')
                console.log($(a[i]).getAttributes());
        }
        xhr.abort();
    }
    nextLine = xhr.response.length;
}

function transferComplete(evt) {
    console.log("The transfer is complete.");
}

function transferFailed(evt) {
    console.log("An error occurred while transferring the file.");
}

function transferCanceled(evt) {
    console.log("The transfer has been canceled by the user.");
}

我怎样才能只加载元标记而不是加载整个html…加载整个html并不好,因为我们不需要它…你甚至可以查看内部,有人提到,该页面被加载、解析,只有这样你才能得到你想要的页面的一部分。所以你需要一个流式html解析器。。。我需要搜索,如果有。谢谢老兄…但我需要那个智能算法,如果你有,你能发布它吗?