Guest User

StackExchange April Fools unobfuscated

a guest
Apr 1st, 2013
534
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. StackExchange.adviza = (function () {
  3. var questionId, leftBeforePopup, minReadingMs;
  4. var timeDef, scrollDef, neverAnswerDef, letsPopDef;
  5. var everPoppedUp, initialized;
  6.  
  7. var now = Date.now || function () { return new Date().getTime() };
  8.  
  9. function set(key, val) {
  10. localStorage.setItem("adviza:" + key, val);
  11. }
  12. function getInt(key) {
  13. return parseInt(localStorage.getItem("adviza:" + key), 10) || 0;
  14. }
  15. function timeSince(key) {
  16. return now() - getInt(key);
  17. }
  18.  
  19. function init(qid, minReadingSeconds, chancePercent, ignoreDate) {
  20. if (initialized)
  21. return;
  22. initialized = true;
  23. if (getInt("leavemealone"))
  24. return;
  25. if (!ignoreDate && new Date().getDate() !== 1)
  26. return;
  27. if ("ontouchstart" in window)
  28. return;
  29.  
  30. try {
  31. localStorage.setItem("adviza:testing", "iflocalstorageisworkingonthisbrowser");
  32. localStorage.removeItem("adviza:testing");
  33. } catch (e) {
  34. return;
  35. }
  36.  
  37. if (!window.postMessage)
  38. return;
  39.  
  40. questionId = qid;
  41.  
  42. // Yes, dear person reading this, there's a secret trigger. But you'll have to
  43. // work a little bit to find out what it is.
  44. $("#search input").blur(function () {
  45. var s = $(this).val();
  46. var v = s.length;
  47. for (var i=0; i < s.length; i++)
  48. v = (v * (s.charCodeAt(i) % 256)) % 5113;
  49. if (v != 5051)
  50. return;
  51.  
  52. popup();
  53. });
  54.  
  55. timeDef = $.Deferred();
  56. scrollDef = $.Deferred();
  57. neverAnswerDef = $.Deferred(); // will be rejected when the user focuses the answer box
  58.  
  59. $.when(timeDef, scrollDef, neverAnswerDef).done(function () {
  60. if (timeSince("lastpopup") < 10 * 60 * 1000)
  61. return;
  62. if (timeSince("lastchat") < 30 * 60 * 1000)
  63. return;
  64. if (chancePercent >= 100 || Math.random() * 100 >= chancePercent)
  65. popup();
  66. });
  67.  
  68. $.when(timeDef, scrollDef).done(function () {
  69. neverAnswerDef.resolve(); // note that this has no effect if it was rejected previously
  70. });
  71.  
  72. $("#wmd-input").one("focus", function () {
  73. neverAnswerDef.reject();
  74. });
  75.  
  76. leftBeforePopup = minReadingMs = minReadingSeconds * 1000;
  77. $(window).on("focus", focus);
  78. $(window).on("blur", blur);
  79. if (!document.hasFocus || document.hasFocus())
  80. focus();
  81.  
  82. var lastAnswer = $("#answers .answer:last");
  83. if (!lastAnswer.length || lastAnswer.offset().top < $(window).scrollTop() + $(window).height())
  84. scrollDef.resolve();
  85. else
  86. $(window).one("scroll", function () { scrollDef.resolve() });
  87. }
  88.  
  89. var readingTimeout, lastTimeoutStart, lastBlur;
  90. function focus() {
  91. if (readingTimeout)
  92. clearTimeout(readingTimeout);
  93. if (lastBlur && now() - lastBlur > minReadingMs) {
  94. leftBeforePopup = minReadingMs;
  95. }
  96. lastBlur = null;
  97. lastTimeoutStart = now();
  98. readingTimeout = setTimeout(function() {
  99. timeDef.resolve();
  100. $(window).off("focus", focus);
  101. $(window).off("blur", blur);
  102. }, leftBeforePopup);
  103. }
  104. function blur() {
  105. if (readingTimeout)
  106. clearTimeout(readingTimeout);
  107. if (lastTimeoutStart) {
  108. leftBeforePopup -= now() - lastTimeoutStart;
  109. lastTimeoutStart = null;
  110. }
  111. lastBlur = now();
  112. }
  113.  
  114. function delay(ms) {
  115. var waiter = $.Deferred();
  116. setTimeout(function () { waiter.resolve(); }, ms);
  117. return function(data) {
  118. var result = $.Deferred();
  119. waiter.done(function () { result.resolve(data); });
  120. return result;
  121. }
  122. }
  123.  
  124. function popup() {
  125. if (everPoppedUp)
  126. return;
  127. everPoppedUp = true;
  128. set("lastpopup", now());
  129. ensureStyle();
  130. var box = $("<div id='adviza-box'/>").css("width", 0).appendTo("body");
  131.  
  132. var stat = $("<div id='adviza-status' >").appendTo(box);
  133. $("<img width=34 height=34 src='http://i.stack.imgur.com/owly4.png'/>").appendTo(stat);
  134. var statspan = $("<span />").text("chat with an expert").appendTo(stat).addClass("hoverable");
  135. var nope = $("<div id='no-adviza'><span>not now</span><br/><span>never</span></div>")
  136. .appendTo(box).find("span").addClass("hoverable").click(function () {
  137. if (/never/.test($(this).text()))
  138. set("leavemealone", 1);
  139. box.remove();
  140. }).hide();
  141.  
  142. box.animate({width: 380}, function () { nope.fadeIn() });
  143.  
  144. statspan.one("click", function () {
  145. nope.remove();
  146. statspan.html("checking available experts").removeClass("hoverable").addSpinnerAfter({"margin-left": "10px"});
  147. $.post("/adviza/create-session/" + questionId, { fkey: StackExchange.options.user.fkey })
  148. .pipe(delay(1500), delay(1500))
  149. .done(function (data) {
  150. if (data && data.iframe)
  151. insertIframe(box, stat, data.iframe, failed);
  152. else
  153. failed();
  154. }).fail(failed);
  155.  
  156. function failed(text)
  157. {
  158. StackExchange.helpers.removeSpinner();
  159. statspan.text(text || "Sorry, no expert is available.");
  160. $("<div id='no-adviza'><span class='hoverable'>close</span></div>")
  161. .appendTo(box).click(function () {
  162. box.remove();
  163. });
  164. }
  165.  
  166. });
  167.  
  168. }
  169.  
  170. function insertIframe(box, stat, url, failed)
  171. {
  172. var origin = url.replace(/^(http:\/\/[^\/]+)\/.*$/, "$1");
  173. var iframeLoading = $.Deferred();
  174.  
  175. var iframe = $("<iframe width=380 height=280 />")
  176. .attr("src", url)
  177. .attr("id", "adviza-iframe")
  178. .attr("frameborder", "0")
  179. .css({left: 400, position: "relative"})
  180. .appendTo(box);
  181.  
  182. stat.find("span").html("connecting you to an expert");
  183.  
  184. var timeout = setTimeout(function () {
  185. iframe.remove();
  186. failed("Sorry, no expert responded.")
  187. }, 10000);
  188.  
  189. iframeLoading.pipe(delay(2000)).done(function () {
  190. iframe.animate({left: 0});
  191. box.animate({height: 280});
  192. stat.animate({top: -44}, function () {stat.remove(); });
  193. });
  194.  
  195. $(window).on("message", messageHandler);
  196.  
  197. function messageHandler(evt) {
  198. evt = evt.originalEvent;
  199. if (evt.origin !== origin)
  200. return;
  201. if (!evt.data)
  202. return;
  203.  
  204. if (evt.data === "close") {
  205. $(window).off("message", messageHandler);
  206. iframe.animate({left: -400});
  207. box.animate({height: 34}).animate({width: 0, paddingLeft:0, paddingRight: 0}, function () { box.remove(); });
  208. } else if (evt.data === "loaded") {
  209. iframeLoading.resolve();
  210. clearTimeout(timeout);
  211. set("lastchat", now());
  212. }
  213. }
  214.  
  215. }
  216.  
  217. var styleInserted = false;
  218. function ensureStyle()
  219. {
  220. if (styleInserted)
  221. return;
  222. var CSS =
  223. "#adviza-box {" +
  224. "position: fixed; width: 380px; height: 34px; bottom: 100px; right: 100px;" +
  225. "right: calc((100% - 1020px)/2); right: -webkit-calc((100% - 1020px)/2)" +
  226. "border: 2px solid black; background: black;" +
  227. "background-color: rgba(0, 0, 0, .85); padding: 8px; overflow: hidden; font-family: sans-serif" +
  228. "}" +
  229. "#adviza-status {" +
  230. "height: 0; overflow: visible; text-align: left; line-height: 34px; font-size: 22px;" +
  231. "color: white; position: relative" +
  232. "}" +
  233. "#adviza-status > * {" +
  234. "vertical-align: middle;" +
  235. "}" +
  236. "#no-adviza {" +
  237. "color: white; float: right; line-height: 17px; font-size: 12px; text-align: right"+
  238. "}" +
  239. "#adviza-box .hoverable {" +
  240. "cursor: pointer;" +
  241. "}" +
  242. "#adviza-box .hoverable:hover {" +
  243. "color: #cfa;" +
  244. "}";
  245.  
  246. var sheet = $("<style type='text/css' />").appendTo("head");
  247. if (sheet[0].styleSheet) // IE 8 pukes when you just set the text on the dom element
  248. sheet[0].styleSheet.cssText = CSS;
  249. else
  250. sheet.text(CSS);
  251. styleInserted = true;
  252.  
  253. }
  254.  
  255. return { init: init }
  256. })()
RAW Paste Data