0

When users add a link within their posts on my site, I want to show a summary text/image from the URL. This is commmonly done by facebook. But how is it done ?

Any JS library/ inbuilt solution that can help me ?

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294
  • This has been asked already http://stackoverflow.com/questions/4767473/how-facebook-link-preview-happens – StevenPHP Jul 20 '14 at 16:10

2 Answers2

2
  1. Send the text the user has entered to your server
  2. Have your server side code:
    1. Parse it for URLs
    2. Make HTTP requests for the URLs specified
    3. Extract a summary/images from them (using whatever algorithm you like)
    4. Include those in the response
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • A couple of performance tweaks I'm sure Facebook uses is to cache a result for a URL sharable among all users and/or have partner icons or images based on a domain, etc. – Jared Farrish Jul 20 '14 at 14:45
  • Quentin, Thanks! @Vector's solution looks more simpler to implement than anything. Are there any caveats with that or what are the advantages of your solution over that ? – Rajat Gupta Jul 20 '14 at 15:24
  • 2
    @user01 — You wanted to show a *summary*, Vector's solution shows the *entire page* (unless X-Frames-Options blocks it from loading at all) and any JS in the page will run so it might framebust you and send the user away from your page entirely. – Quentin Jul 20 '14 at 15:31
  • Can not believe this has been voted up 3 times!? – StevenPHP Jul 20 '14 at 16:06
-2

Use jQuery

$.get("www.google.com", function(data) { do what ever you want to do with the data });

Change the function to extract the page content.

Get function reference http://api.jquery.com/jquery.get/

Other posts which can help

Can Javascript read the source of any web page?

Community
  • 1
  • 1
Haja Maideen
  • 450
  • 2
  • 4
  • 1
    That won't work: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy – Quentin Jul 20 '14 at 14:54
  • Please [see the tag wiki](http://stackoverflow.com/tags/javascript/info): "Unless a tag for a framework/library is also included, a pure JavaScript answer is expected for questions with the javascript tag.". – Quentin Jul 20 '14 at 14:56