Skip to content

Instantly share code, notes, and snippets.

@qufighter
Created May 30, 2012 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qufighter/2837089 to your computer and use it in GitHub Desktop.
Save qufighter/2837089 to your computer and use it in GitHub Desktop.
FB HistoryManager + jsbeautifier
__d("HistoryManager", ["event-extensions", "function-extensions", "Cookie", "Env", "URI", "UserAgent", "copyProperties", "goOrReplace"], function (a, b, c, d, e, f) {
b('event-extensions');
b('function-extensions');
var g = b('Cookie'),
h = b('Env'),
i = b('URI'),
j = b('UserAgent'),
k = b('copyProperties'),
l = b('goOrReplace'),
m = {
_IFRAME_BASE_URI: 'http://static.ak.facebook.com/common/history_manager.php',
history: null,
current: 0,
fragment: null,
_setIframeSrcFragment: function (n) {
n = n.toString();
var o = m.history.length - 1;
m.iframe.src = m._IFRAME_BASE_URI + '?|index=' + o + '#' + encodeURIComponent(n);
return m;
},
getIframeSrcFragment: function () {
return decodeURIComponent(i(m.iframe.contentWindow.document.location.href).getFragment());
},
nextframe: function (n, o) {
if (o) {
m._setIframeSrcFragment(n);
return;
}
if (n !== undefined) {
m.iframeQueue.push(n);
} else {
m.iframeQueue.splice(0, 1);
m.iframeTimeout = null;
m.checkURI();
}
if (m.iframeQueue.length && !m.iframeTimeout) {
var p = m.iframeQueue[0];
m.iframeTimeout = setTimeout(function () {
m._setIframeSrcFragment(p);
}, 100, false);
}
},
isInitialized: function () {
return !!m._initialized;
},
init: function () {
if (!h.ALLOW_TRANSITION_IN_IFRAME && window != window.top) return;
if (m._initialized) return m;
var n = i(),
o = n.getFragment() || '';
if (o.charAt(0) === '!') {
o = o.substr(1);
n.setFragment(o);
}
if (i.getRequestURI(false).getProtocol().toLowerCase() == 'https') m._IFRAME_BASE_URI = 'https://s-static.ak.facebook.com/common/history_manager.php';
k(m, {
_initialized: true,
fragment: o,
orig_fragment: o,
history: [n],
callbacks: [],
lastChanged: Date.now(),
canonical: i('#'),
fragmentTimeout: null,
user: 0,
iframeTimeout: null,
iframeQueue: [],
enabled: true,
debug: bagofholding
});
if (window.history && history.pushState) {
this.lastURI = document.URL;
window.history.replaceState(this.lastURI, null);
Event.listen(window, 'popstate', function (p) {
if (p && p.state && m.lastURI != p.state) {
m.lastURI = p.state;
m.lastChanged = Date.now();
m.notify(i(p.state).getUnqualifiedURI().toString());
}
}.bind(m));
if (j.safari() < 534 || j.chrome() <= 13) {
setInterval(m.checkURI, 42, false);
m._updateRefererURI(this.lastURI);
}
return m;
}
m._updateRefererURI(i.getRequestURI(false));
if (j.safari() < 500 || j.firefox() < 2) {
m.enabled = false;
return m;
}
if (j.ie() < 8) {
m.iframe = document.createElement('iframe');
k(m.iframe.style, {
width: '0',
height: '0',
frameborder: '0',
left: '0',
top: '0',
position: 'absolute'
});
m._setIframeSrcFragment(o);
document.body.insertBefore(m.iframe, document.body.firstChild);
} else if ('onhashchange' in window) {
Event.listen(window, 'hashchange', function () {
m.checkURI.bind(m).defer();
});
} else setInterval(m.checkURI, 42, false);
return m;
},
registerURIHandler: function (n) {
m.callbacks.push(n);
return m;
},
setCanonicalLocation: function (n) {
m.canonical = i(n);
return m;
},
notify: function (n) {
if (n == m.orig_fragment) n = m.canonical.getFragment();
for (var o = 0; o < m.callbacks.length; o++) try {
if (m.callbacks[o](n)) return true;
} catch (p) {}
return false;
},
checkURI: function () {
if (Date.now() - m.lastChanged < 400) return;
if (window.history && history.pushState) {
var n = i(document.URL).removeQueryData('ref').toString(),
o = i(m.lastURI).removeQueryData('ref').toString();
if (n != o) {
m.lastChanged = Date.now();
m.lastURI = n;
if (j.safari() < 534) m._updateRefererURI(n);
m.notify(i(n).getUnqualifiedURI().toString());
}
return;
}
if (j.ie() < 8 && m.iframeQueue.length) return;
if (j.safari() && window.history.length == 200) {
if (!m.warned) m.warned = true;
return;
}
var p = i().getFragment();
if (p.charAt(0) == '!') p = p.substr(1);
if (j.ie() < 8) p = m.getIframeSrcFragment();
p = p.replace(/%23/g, '#');
if (p != m.fragment.replace(/%23/g, '#')) {
m.debug([p, ' vs ', m.fragment, 'whl: ', window.history.length, 'QHL: ', m.history.length].join(' '));
for (var q = m.history.length - 1; q >= 0; --q) if (m.history[q].getFragment().replace(/%23/g, '#') == p) break;
++m.user;
if (q >= 0) {
m.go(q - m.current);
} else m.go('#' + p);
--m.user;
}
},
_updateRefererURI: function (n) {
n = n.toString();
if (n.charAt(0) != '/' && n.indexOf('//') == -1) return;
var o = new i(window.location);
if (o.isFacebookURI()) {
var p = o.getPath() + window.location.search;
} else var p = '';
var q = i(n).getQualifiedURI().setFragment(p).toString(),
r = 2048;
if (q.length > r) q = q.substring(0, r) + '...';
g.set('x-referer', q);
},
go: function (n, o, p) {
if (window.history && history.pushState) {
o || typeof (n) == 'number';
var q = i(n).removeQueryData('ref').toString();
m.lastChanged = Date.now();
this.lastURI = q;
if (p) {
window.history.replaceState(n, null, q);
} else window.history.pushState(n, null, q);
if (j.safari() < 534) m._updateRefererURI(n);
return false;
}
m.debug('go: ' + n);
if (o === undefined) o = true;
if (!m.enabled) if (!o) return false;
if (typeof (n) == 'number') {
if (!n) return false;
var r = n + m.current,
s = Math.max(0, Math.min(m.history.length - 1, r));
m.current = s;
r = m.history[s].getFragment() || m.orig_fragment;
r = i(r).removeQueryData('ref').getUnqualifiedURI().toString();
m.fragment = r;
m.lastChanged = Date.now();
if (j.ie() < 8) {
if (m.fragmentTimeout) clearTimeout(m.fragmentTimeout);
m._temporary_fragment = r;
m.fragmentTimeout = setTimeout(function () {
window.location.hash = '#!' + r;
delete m._temporary_fragment;
}, 750, false);
if (!m.user) m.nextframe(r, p);
} else if (!m.user) l(window.location, window.location.href.split('#')[0] + '#!' + r, p);
if (o) m.notify(r);
m._updateRefererURI(r);
return false;
}
n = i(n);
if (n.getDomain() == i().getDomain()) n = i('#' + n.getUnqualifiedURI());
var t = m.history[m.current].getFragment(),
u = n.getFragment();
if (u == t || (t == m.orig_fragment && u == m.canonical.getFragment())) {
if (o) m.notify(u);
m._updateRefererURI(u);
return false;
}
if (p) m.current--;
var v = (m.history.length - m.current) - 1;
m.history.splice(m.current + 1, v);
m.history.push(i(n));
return m.go(1, o, p);
},
getCurrentFragment: function () {
var n = m._temporary_fragment !== undefined ? m._temporary_fragment : i.getRequestURI(false).getFragment();
return n == m.orig_fragment ? m.canonical.getFragment() : n;
}
};
e.exports = m;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment