Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- StackExchange.adviza = (function () {
- var questionId, leftBeforePopup, minReadingMs;
- var timeDef, scrollDef, neverAnswerDef, letsPopDef;
- var everPoppedUp, initialized;
- var now = Date.now || function () { return new Date().getTime() };
- function set(key, val) {
- localStorage.setItem("adviza:" + key, val);
- }
- function getInt(key) {
- return parseInt(localStorage.getItem("adviza:" + key), 10) || 0;
- }
- function timeSince(key) {
- return now() - getInt(key);
- }
- function init(qid, minReadingSeconds, chancePercent, ignoreDate) {
- if (initialized)
- return;
- initialized = true;
- if (getInt("leavemealone"))
- return;
- if (!ignoreDate && new Date().getDate() !== 1)
- return;
- if ("ontouchstart" in window)
- return;
- try {
- localStorage.setItem("adviza:testing", "iflocalstorageisworkingonthisbrowser");
- localStorage.removeItem("adviza:testing");
- } catch (e) {
- return;
- }
- if (!window.postMessage)
- return;
- questionId = qid;
- // Yes, dear person reading this, there's a secret trigger. But you'll have to
- // work a little bit to find out what it is.
- $("#search input").blur(function () {
- var s = $(this).val();
- var v = s.length;
- for (var i=0; i < s.length; i++)
- v = (v * (s.charCodeAt(i) % 256)) % 5113;
- if (v != 5051)
- return;
- popup();
- });
- timeDef = $.Deferred();
- scrollDef = $.Deferred();
- neverAnswerDef = $.Deferred(); // will be rejected when the user focuses the answer box
- $.when(timeDef, scrollDef, neverAnswerDef).done(function () {
- if (timeSince("lastpopup") < 10 * 60 * 1000)
- return;
- if (timeSince("lastchat") < 30 * 60 * 1000)
- return;
- if (chancePercent >= 100 || Math.random() * 100 >= chancePercent)
- popup();
- });
- $.when(timeDef, scrollDef).done(function () {
- neverAnswerDef.resolve(); // note that this has no effect if it was rejected previously
- });
- $("#wmd-input").one("focus", function () {
- neverAnswerDef.reject();
- });
- leftBeforePopup = minReadingMs = minReadingSeconds * 1000;
- $(window).on("focus", focus);
- $(window).on("blur", blur);
- if (!document.hasFocus || document.hasFocus())
- focus();
- var lastAnswer = $("#answers .answer:last");
- if (!lastAnswer.length || lastAnswer.offset().top < $(window).scrollTop() + $(window).height())
- scrollDef.resolve();
- else
- $(window).one("scroll", function () { scrollDef.resolve() });
- }
- var readingTimeout, lastTimeoutStart, lastBlur;
- function focus() {
- if (readingTimeout)
- clearTimeout(readingTimeout);
- if (lastBlur && now() - lastBlur > minReadingMs) {
- leftBeforePopup = minReadingMs;
- }
- lastBlur = null;
- lastTimeoutStart = now();
- readingTimeout = setTimeout(function() {
- timeDef.resolve();
- $(window).off("focus", focus);
- $(window).off("blur", blur);
- }, leftBeforePopup);
- }
- function blur() {
- if (readingTimeout)
- clearTimeout(readingTimeout);
- if (lastTimeoutStart) {
- leftBeforePopup -= now() - lastTimeoutStart;
- lastTimeoutStart = null;
- }
- lastBlur = now();
- }
- function delay(ms) {
- var waiter = $.Deferred();
- setTimeout(function () { waiter.resolve(); }, ms);
- return function(data) {
- var result = $.Deferred();
- waiter.done(function () { result.resolve(data); });
- return result;
- }
- }
- function popup() {
- if (everPoppedUp)
- return;
- everPoppedUp = true;
- set("lastpopup", now());
- ensureStyle();
- var box = $("<div id='adviza-box'/>").css("width", 0).appendTo("body");
- var stat = $("<div id='adviza-status' >").appendTo(box);
- $("<img width=34 height=34 src='http://i.stack.imgur.com/owly4.png'/>").appendTo(stat);
- var statspan = $("<span />").text("chat with an expert").appendTo(stat).addClass("hoverable");
- var nope = $("<div id='no-adviza'><span>not now</span><br/><span>never</span></div>")
- .appendTo(box).find("span").addClass("hoverable").click(function () {
- if (/never/.test($(this).text()))
- set("leavemealone", 1);
- box.remove();
- }).hide();
- box.animate({width: 380}, function () { nope.fadeIn() });
- statspan.one("click", function () {
- nope.remove();
- statspan.html("checking available experts").removeClass("hoverable").addSpinnerAfter({"margin-left": "10px"});
- $.post("/adviza/create-session/" + questionId, { fkey: StackExchange.options.user.fkey })
- .pipe(delay(1500), delay(1500))
- .done(function (data) {
- if (data && data.iframe)
- insertIframe(box, stat, data.iframe, failed);
- else
- failed();
- }).fail(failed);
- function failed(text)
- {
- StackExchange.helpers.removeSpinner();
- statspan.text(text || "Sorry, no expert is available.");
- $("<div id='no-adviza'><span class='hoverable'>close</span></div>")
- .appendTo(box).click(function () {
- box.remove();
- });
- }
- });
- }
- function insertIframe(box, stat, url, failed)
- {
- var origin = url.replace(/^(http:\/\/[^\/]+)\/.*$/, "$1");
- var iframeLoading = $.Deferred();
- var iframe = $("<iframe width=380 height=280 />")
- .attr("src", url)
- .attr("id", "adviza-iframe")
- .attr("frameborder", "0")
- .css({left: 400, position: "relative"})
- .appendTo(box);
- stat.find("span").html("connecting you to an expert");
- var timeout = setTimeout(function () {
- iframe.remove();
- failed("Sorry, no expert responded.")
- }, 10000);
- iframeLoading.pipe(delay(2000)).done(function () {
- iframe.animate({left: 0});
- box.animate({height: 280});
- stat.animate({top: -44}, function () {stat.remove(); });
- });
- $(window).on("message", messageHandler);
- function messageHandler(evt) {
- evt = evt.originalEvent;
- if (evt.origin !== origin)
- return;
- if (!evt.data)
- return;
- if (evt.data === "close") {
- $(window).off("message", messageHandler);
- iframe.animate({left: -400});
- box.animate({height: 34}).animate({width: 0, paddingLeft:0, paddingRight: 0}, function () { box.remove(); });
- } else if (evt.data === "loaded") {
- iframeLoading.resolve();
- clearTimeout(timeout);
- set("lastchat", now());
- }
- }
- }
- var styleInserted = false;
- function ensureStyle()
- {
- if (styleInserted)
- return;
- var CSS =
- "#adviza-box {" +
- "position: fixed; width: 380px; height: 34px; bottom: 100px; right: 100px;" +
- "right: calc((100% - 1020px)/2); right: -webkit-calc((100% - 1020px)/2)" +
- "border: 2px solid black; background: black;" +
- "background-color: rgba(0, 0, 0, .85); padding: 8px; overflow: hidden; font-family: sans-serif" +
- "}" +
- "#adviza-status {" +
- "height: 0; overflow: visible; text-align: left; line-height: 34px; font-size: 22px;" +
- "color: white; position: relative" +
- "}" +
- "#adviza-status > * {" +
- "vertical-align: middle;" +
- "}" +
- "#no-adviza {" +
- "color: white; float: right; line-height: 17px; font-size: 12px; text-align: right"+
- "}" +
- "#adviza-box .hoverable {" +
- "cursor: pointer;" +
- "}" +
- "#adviza-box .hoverable:hover {" +
- "color: #cfa;" +
- "}";
- var sheet = $("<style type='text/css' />").appendTo("head");
- if (sheet[0].styleSheet) // IE 8 pukes when you just set the text on the dom element
- sheet[0].styleSheet.cssText = CSS;
- else
- sheet.text(CSS);
- styleInserted = true;
- }
- return { init: init }
- })()
Advertisement
Add Comment
Please, Sign In to add comment