Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (!jQuery.support.cors && window.XDomainRequest) {
- var httpRegEx = /^https?:\/\//i;
- var getOrPostRegEx = /^get|post$/i;
- var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
- var xmlRegEx = /\/xml/i;
- // ajaxTransport exists in jQuery 1.5+
- jQuery.ajaxTransport('text html xml json', function(options, userOptions, jqXHR){
- // XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
- if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(userOptions.url) && sameSchemeRegEx.test(userOptions.url)) {
- var xdr = null;
- var userType = (userOptions.dataType||'').toLowerCase();
- return {
- send: function(headers, complete){
- xdr = new XDomainRequest();
- if (/^\d+$/.test(userOptions.timeout)) {
- xdr.timeout = userOptions.timeout;
- }
- xdr.ontimeout = function(){
- complete(500, 'timeout');
- };
- xdr.onload = function(){
- var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
- var status = {
- code: 200,
- message: 'success'
- };
- var responses = {
- text: xdr.responseText
- };
- /*
- if (userType === 'html') {
- responses.html = xdr.responseText;
- } else
- */
- try {
- if (userType === 'json') {
- try {
- responses.json = JSON.parse(xdr.responseText);
- } catch(e) {
- status.code = 500;
- status.message = 'parseerror';
- //throw 'Invalid JSON: ' + xdr.responseText;
- }
- } else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {
- var doc = new ActiveXObject('Microsoft.XMLDOM');
- doc.async = false;
- try {
- doc.loadXML(xdr.responseText);
- } catch(e) {
- doc = undefined;
- }
- if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
- status.code = 500;
- status.message = 'parseerror';
- throw 'Invalid XML: ' + xdr.responseText;
- }
- responses.xml = doc;
- }
- } catch(parseMessage) {
- throw parseMessage;
- } finally {
- complete(status.code, status.message, responses, allResponseHeaders);
- }
- };
- xdr.onerror = function(){
- complete(500, 'error', {
- text: xdr.responseText
- });
- };
- xdr.open(options.type, options.url);
- //xdr.send(userOptions.data);
- xdr.send();
- },
- abort: function(){
- if (xdr) {
- xdr.abort();
- }
- }
- };
- }
- });
- }
- var PeerFly = {};
- PeerFly.Contextual = function(userid) { $pfContextual = this; $(document).ready(function() { $pfContextual.__construct(userid); }); };
- PeerFly.Contextual.prototype =
- {
- __construct: function(userid)
- {
- console.log(userid);
- $.support.cors = true;
- $.ajax({
- dataType: "json",
- crossDomain: true,
- url: '//direct.pixxur.com/contextual.php',
- data: {'userid': userid},
- success: function (data) {
- $pfContextual.$words = data;
- $pfContextual.replace();
- },
- beforeSend: function (xhr) { },
- error: function(e, t, r) { console.log(e, t, r); }
- });
- },
- replace: function()
- {
- $pfContextual.replaceChildren($("body"));
- },
- replaceChildren: function(element)
- {
- if ($(element).children().length > 0 && $(element).hasClass('ignoreChildren') === false)
- {
- $.each(
- $(element).children(),
- function (index, child)
- {
- $pfContextual.replaceChildren($(child));
- }
- );
- }
- $.each(
- $pfContextual.$words,
- function (index, value)
- {
- $index = index;
- $value = value;
- $(element).contents().each(
- function()
- {
- $e = this;
- if ($e.nodeType === 3)
- {
- if ($(element).prop('tagName') != 'A' && $(element).hasClass('ignore') === false)
- {
- $text = $e.nodeValue;
- $reg = new RegExp($value['word'], "igm");
- if (
- ($value['limit'] > 0 && $value['used'] < $value['limit'])
- || $value['limit'] == 0
- )
- {
- $newText = $text.replace($reg, '<a href="' + $value['replacement']['link'] + '">' + $value['replacement']['text'] + '</a>');
- $($e).replaceWith($newText);
- if ($newText != $text)
- {
- $pfContextual.$words[index]['used'] = $pfContextual.$words[$index]['used'] + 1;
- }
- }
- }
- else
- {
- }
- }
- }
- );
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement