Advertisement
Corey

Untitled

Nov 23rd, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (!jQuery.support.cors && window.XDomainRequest) {
  2.     var httpRegEx = /^https?:\/\//i;
  3.     var getOrPostRegEx = /^get|post$/i;
  4.     var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
  5.     var xmlRegEx = /\/xml/i;
  6.  
  7.     // ajaxTransport exists in jQuery 1.5+
  8.     jQuery.ajaxTransport('text html xml json', function(options, userOptions, jqXHR){
  9.         // XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
  10.         if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(userOptions.url) && sameSchemeRegEx.test(userOptions.url)) {
  11.             var xdr = null;
  12.             var userType = (userOptions.dataType||'').toLowerCase();
  13.             return {
  14.                 send: function(headers, complete){
  15.                     xdr = new XDomainRequest();
  16.                     if (/^\d+$/.test(userOptions.timeout)) {
  17.                         xdr.timeout = userOptions.timeout;
  18.                     }
  19.                     xdr.ontimeout = function(){
  20.                         complete(500, 'timeout');
  21.                     };
  22.                     xdr.onload = function(){
  23.                         var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
  24.                         var status = {
  25.                             code: 200,
  26.                             message: 'success'
  27.                         };
  28.                         var responses = {
  29.                             text: xdr.responseText
  30.                         };
  31.                         /*
  32.                         if (userType === 'html') {
  33.                             responses.html = xdr.responseText;
  34.                         } else
  35.                         */
  36.                         try {
  37.                             if (userType === 'json') {
  38.                                 try {
  39.                                     responses.json = JSON.parse(xdr.responseText);
  40.                                 } catch(e) {
  41.                                     status.code = 500;
  42.                                     status.message = 'parseerror';
  43.                                     //throw 'Invalid JSON: ' + xdr.responseText;
  44.                                 }
  45.                             } else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {
  46.                                 var doc = new ActiveXObject('Microsoft.XMLDOM');
  47.                                 doc.async = false;
  48.                                 try {
  49.                                     doc.loadXML(xdr.responseText);
  50.                                 } catch(e) {
  51.                                     doc = undefined;
  52.                                 }
  53.                                 if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
  54.                                     status.code = 500;
  55.                                     status.message = 'parseerror';
  56.                                     throw 'Invalid XML: ' + xdr.responseText;
  57.                                 }
  58.                                 responses.xml = doc;
  59.                             }
  60.                         } catch(parseMessage) {
  61.                             throw parseMessage;
  62.                         } finally {
  63.                             complete(status.code, status.message, responses, allResponseHeaders);
  64.                         }
  65.                     };
  66.                     xdr.onerror = function(){
  67.                         complete(500, 'error', {
  68.                             text: xdr.responseText
  69.                         });
  70.                     };
  71.                     xdr.open(options.type, options.url);
  72.                     //xdr.send(userOptions.data);
  73.                     xdr.send();
  74.                 },
  75.                 abort: function(){
  76.                     if (xdr) {
  77.                         xdr.abort();
  78.                     }
  79.                 }
  80.             };
  81.         }
  82.     });
  83. }
  84.  
  85.  
  86.         var PeerFly = {};
  87.        
  88.         PeerFly.Contextual = function(userid) { $pfContextual = this; $(document).ready(function() { $pfContextual.__construct(userid); }); };
  89.         PeerFly.Contextual.prototype =
  90.         {
  91.             __construct: function(userid)
  92.             {
  93.                 console.log(userid);
  94.                 $.support.cors = true;
  95.                 $.ajax({
  96.                     dataType: "json",
  97.                     crossDomain: true,
  98.                     url: '//direct.pixxur.com/contextual.php',
  99.                     data: {'userid': userid},
  100.                     success: function (data) {
  101.                         $pfContextual.$words = data;
  102.                         $pfContextual.replace();
  103.                     },
  104.                     beforeSend: function (xhr) {  },
  105.                     error: function(e, t, r) { console.log(e, t, r); }
  106.                 });
  107.             },
  108.             replace: function()
  109.             {
  110.                 $pfContextual.replaceChildren($("body"));
  111.             },
  112.             replaceChildren: function(element)
  113.             {
  114.                 if ($(element).children().length > 0 && $(element).hasClass('ignoreChildren') === false)
  115.                 {
  116.                     $.each(
  117.                         $(element).children(),
  118.                         function (index, child)
  119.                         {
  120.                             $pfContextual.replaceChildren($(child));
  121.                         }
  122.                     );
  123.                 }
  124.                
  125.                 $.each(
  126.                     $pfContextual.$words,
  127.                     function (index, value)
  128.                     {
  129.                         $index = index;
  130.                         $value = value;
  131.                         $(element).contents().each(
  132.                             function()
  133.                             {
  134.                                 $e = this;
  135.                                 if ($e.nodeType === 3)
  136.                                 {
  137.                                     if ($(element).prop('tagName') != 'A' && $(element).hasClass('ignore') === false)
  138.                                     {
  139.  
  140.                                         $text = $e.nodeValue;
  141.  
  142.                                         $reg = new RegExp($value['word'], "igm");
  143.                                        
  144.                                             if (
  145.                                                     ($value['limit'] > 0 && $value['used'] < $value['limit'])
  146.                                                     || $value['limit'] == 0
  147.                                                 )
  148.                                             {
  149.                                                 $newText = $text.replace($reg, '<a href="' + $value['replacement']['link'] + '">' + $value['replacement']['text'] + '</a>');
  150.                                                 $($e).replaceWith($newText);
  151.                                                 if ($newText != $text)
  152.                                                 {
  153.                                                     $pfContextual.$words[index]['used'] = $pfContextual.$words[$index]['used'] + 1;
  154.                                                 }
  155.                                             }
  156.                                     }
  157.                                     else
  158.                                     {
  159.                                     }
  160.                                 }
  161.                             }
  162.                         );
  163.                     }
  164.                 );
  165.             }
  166.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement