/** * @article Getting the title of a link * through Javascript * * @since January 4, 2012 * @package Code Snippets * * @tags coffeescript, * jQuery, * scrapers, * YQL * */
While working on my Recommended Links plugin, I wanted a way to auto-populate the link title field based on the URL entered. I was stuck for a while dealing with cross-domain XHR issues and initially fell back to using a WP Ajax request, using wp_remote_get to get the remote link and returning that to PHP… however, it was slow and not all that elegant. Why bootstrap the entire WP environment for one simple Ajax request?
Did some researching, though, and I discovered YQL, which I had played with before, but never thought of as a tool for getting around cross-domain request issues. Apparently, however, its been widely used for that purpose for a year or so — there’s even a jQuery plugin that converts all external Ajax requests to YQL queries. Sweet!
The one problem I had, though, was that YQL’s html table only includes the <body> element, and I wanted to retrieve the <title> text.
So I looked through the Open Data Tables available, and lo and behold, there’s an htmlstring table that returns the entire html of a document as a string. So all I had to do was a little regex matching to find the <title> element, and I was all set.