Advertisement
stanevplamen

Untitled

May 23rd, 2014
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.52 KB | None | 0 0
  1. // START read the url content
  2.  
  3. readUrlContents('http://www.abv.bg/');
  4.  
  5. function readUrlContents(givenURL) {
  6.     jQuery.ajax = (function (_ajax) {
  7.  
  8.         var protocol = location.protocol,
  9.             hostname = location.hostname,
  10.             exRegex = RegExp(protocol + '//' + hostname),
  11.             YQL = 'http' + (/^https/.test(protocol) ? 's' : '') + '://query.yahooapis.com/v1/public/yql?callback=?',
  12.             query = 'select * from html where url="{URL}" and xpath="*"';
  13.  
  14.         function isExternal(url) {
  15.             return !exRegex.test(url) && /:\/\//.test(url);
  16.         }
  17.  
  18.         return function (o) {
  19.  
  20.             var url = o.url;
  21.  
  22.             if (/get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url)) {
  23.  
  24.                 // Manipulate options so that JSONP-x request is made to YQL
  25.  
  26.                 o.url = YQL;
  27.                 o.dataType = 'json';
  28.  
  29.                 o.data = {
  30.                     q: query.replace(
  31.                         '{URL}',
  32.                         url + (o.data ?
  33.                             (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
  34.                         : '')
  35.                     ),
  36.                     format: 'xml'
  37.                 };
  38.  
  39.                 // Since it's a JSONP request
  40.                 // complete === success
  41.                 if (!o.success && o.complete) {
  42.                     o.success = o.complete;
  43.                     delete o.complete;
  44.                 }
  45.  
  46.                 o.success = (function (_success) {
  47.                     return function (data) {
  48.  
  49.                         if (_success) {
  50.                             // Fake XHR callback.
  51.                             _success.call(this, {
  52.                                 responseText: data.results[0]
  53.                                     // YQL screws with <script>s
  54.                                     // Get rid of them
  55.                                     .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
  56.                             }, 'success');
  57.                         }
  58.  
  59.                     };
  60.                 })(o.success);
  61.  
  62.             }
  63.  
  64.             return _ajax.apply(this, arguments);
  65.  
  66.         };
  67.  
  68.     })(jQuery.ajax);
  69.  
  70.  
  71.  
  72.     $.ajax({
  73.         url: hrefText,
  74.         type: 'GET',
  75.         success: function (res) {
  76.             var text = res.responseText;
  77.             lastUrlContent = text;
  78.             // then you can manipulate your text as you wish
  79.             //alert(lastUrlContent);
  80.         }
  81.     });
  82.  
  83.     // END read the url content
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement