How Facebook link preview happens?

后端 未结 4 1633
小鲜肉
小鲜肉 2021-02-01 17:47

When you type a URL into facebook, it automatically comes up with the \"post a link\" feature. With most websites, it automatically loads a selection of images it pulls from som

4条回答
  •  借酒劲吻你
    2021-02-01 18:30

    Due to security restrictions, JavaScript cannot load any page from any server. Facebook actually asks the Facebook servers for information about that page, and the servers in turn query the page to extract the information. You, too, will need a server-side implementation of this.

    Aside from that, it's fairly simple: use an HTTP client library for your language of choice to connect to the provided URL, then use an HTML parsing library to extract the title tag, identify a significant piece of text in the body tag, and extract the img tag sources that seem the most appropriate.

    Once your server is able to extract information about a page, it's a fairly simple exercise to call it through AJAX.

提交回复
热议问题