Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Facebook link preview happens?

Tags:

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 somewhere in the page to go alongside the link and description. does anyone have an idea of how to implement that same feature for our own site without any FB API etc.. just javascript or JQuery ?

like image 437
Keshan Avatar asked Jan 22 '11 11:01

Keshan


People also ask

How does Facebook link preview work?

When you plug a URL into this tool, it pre-loads all the information Facebook needs in order to generate a link preview in the future. Facebook stores that info, and then when you get around to actually sharing the link, they're able to generate the preview – even the very first time you share it.

How long do Facebook preview links last?

Facebook needs to refresh the link again. In most cases, the data stays unrefreshed for up to 30 days.

How do I show a link preview on Facebook?

Log into Facebook if you're not already with a username and password, visit the Facebook Sharing debugger page, Paste the link, for which the Preview wasn't shown, and click 'Debug'. Ignore the warnings and click 'Scrape Again', now debugger tool will show the link preview at the bottom of the page correctly.

How does Facebook pick image from link?

The thumbnail is generated automatically as the Facebook system scans the link for any associated image files. Most links have several image files associated with them, in one way or another, and Facebook won't always select the best one by default.


1 Answers

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.

like image 165
Victor Nicollet Avatar answered Oct 25 '22 17:10

Victor Nicollet