Skip to content

Instantly share code, notes, and snippets.

@ixtli
Created August 13, 2014 15:21
Show Gist options
  • Save ixtli/860382fa72bcd9073b41 to your computer and use it in GitHub Desktop.
Save ixtli/860382fa72bcd9073b41 to your computer and use it in GitHub Desktop.
Facebook double init
/** subsections of sdk.js retrieved on Aug 13th, 2014 */
function init(/*object|number|string*/ options) {__t([options, 'object|number|string', 'options']);
if (Runtime.getInitialized()) {
Log.warn(
'FB.init has already been called - this could indicate a problem');
}
if (Runtime.getIsVersioned()) {
if (Object.prototype.toString.call(options) !== '[object Object]') {
throw new ManagedError('Invalid argument');
}
if (options.authResponse) {
Log.warn('Setting authResponse is not supported');
}
if (!options.version) {
options.version = URI(location.href).getQueryData().sdk_version;
}
// Enforce that there's a version specified
PlatformVersioning.assertValidVersion(options.version);
Runtime.setVersion(options.version);
} else {
if (/number|string/.test(typeof options)) {
Log.warn('FB.init called with invalid parameters');
options = {apiKey: options};
}
options = ES('Object', 'assign', false,{
status: true
}, options || {});
}
var appId = parseAppId(options.appId || options.apiKey);
if (appId !== null) {
Runtime.setClientID(appId);
}
if ('scope' in options) {
Runtime.setScope(options.scope);
}
if (options.cookie) {
Runtime.setUseCookie(true);
if (typeof options.cookie === 'string') {
Cookie.setDomain(options.cookie);
}
}
if (options.kidDirectedSite) {
Runtime.setKidDirectedSite(true);
}
Runtime.setInitialized(true);
Event.fire('init:post', options);
}__w(init, {"signature":"function(object|number|string)"});
/** subscriber that makes the XD request after init() is called */
Event.subscribe('init:post', __w(function(/*object*/ options) {__t([options, 'object', 'options']);
init(options.xdProxyName);
var timeout = feature('xd_timeout');
if (timeout) {
setTimeout(function() {
var initialized =
httpsProxyFrame
&& (!!httpProxyFrame == httpProxyQueue.isStarted()
&& !!httpsProxyFrame == httpsProxyQueue.isStarted());
if (!initialized) {
Scribe.log('jssdk_error', {
appId: Runtime.getClientID(),
error: 'XD_INITIALIZATION',
extra: {
message: 'Failed to initialize in ' + timeout + 'ms'
}
});
}
}, timeout);
}
}, {"signature":"function(object)"}));
/** the "init" function called from above */
function init(/*?string*/ xdProxyName) {__t([xdProxyName, '?string', 'xdProxyName']);
if (inited) {
return;
}
var container = Content.appendHidden(document.createElement('div'));
var transport = XDM.create({
blacklist: null,
root: container,
channel: channel,
flashUrl: XDConfig.Flash.path,
whenReady: __w(function(/*object*/ instance) {__t([instance, 'object', 'instance']);
xdm = instance;
var proxyData = {
channel: channel,
origin: location.protocol + '//' + location.host,
transport: transport,
xd_name: xdProxyName
};
var xdArbiterFragment = '#' + QueryString.encode(proxyData);
if (Runtime.getSecure() !== true) {
httpProxyFrame = createIframe({
url: xdArbiterHttpUrl + xdArbiterFragment,
name: 'fb_xdm_frame_http',
id: 'fb_xdm_frame_http',
root: container,
'aria-hidden':true,
title: IFRAME_TITLE,
tabindex: -1
});
}
httpsProxyFrame = createIframe({
url: xdArbiterHttpsUrl + xdArbiterFragment,
name: 'fb_xdm_frame_https',
id: 'fb_xdm_frame_https',
root: container,
'aria-hidden':true,
title: IFRAME_TITLE,
tabindex: -1
});
}, {"signature":"function(object)"}),
onMessage: onMessage
});
if (!transport) {
Scribe.log('jssdk_error', {
appId: Runtime.getClientID(),
error: 'XD_TRANSPORT',
extra: {
message: 'Failed to create a valid transport'
}
});
}
inited = true;
}__w(init, {"signature":"function(?string)"});
/** Another subscriber to FB.init() which itself calls getLoginStatus */
Event.subscribe('init:post', __w(function(/*object*/ options) {__t([options, 'object', 'options']);
if (options.frictionlessRequests) {
Frictionless.init();
}
}, {"signature":"function(object)"}));
/** Yet another init responder */
Event.subscribe('init:post', __w(function(/*object*/ options) {__t([options, 'object', 'options']);
if (options.status) {
Auth.getLoginStatus();
}
if (Runtime.getClientID()) {
if (options.authResponse) {
Auth.setAuthResponse(options.authResponse, 'connected');
} else if (Runtime.getUseCookie()) {
// we don't have an access token yet, but we might have a user
var signedRequest = Cookie.loadSignedRequest(), parsedSignedRequest;
if (signedRequest) {
try {
parsedSignedRequest = SignedRequest.parse(signedRequest);
} catch (e) {
Cookie.clearSignedRequestCookie();
}
if (parsedSignedRequest && parsedSignedRequest.user_id) {
Runtime.setCookieUserID(parsedSignedRequest.user_id);
}
}
Cookie.loadMeta();
}
}
}, {"signature":"function(object)"}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment