我使用這個代碼爲Facebook SDK初始化如何在Dart中使用Facebook SDK?
window.fbAsyncInit = function() {
FB.init({
appId : '{app id}',
status : true,
cookie : true,
xfbml : true
});
};
(function(d){
var js,
id = 'facebook-jssdk',
ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all/debug.js";
ref.parentNode.insertBefore(js, ref);
}(document));
爲了從Facebook使用這些個人信息,我用飛鏢代碼
void showFacebook(Event e, var detail, Node target) {
JsObject fb = context["FB"];
fb.callMethod("getLoginStatus", [checkLoginStatusCallback]);
}
void checkLoginStatusCallback(response) {
print("checkLoginStatusCallback");
if(response["status"] == "connected") {
showPhoto(response["authResponse"]["userID"]);
}
else {
authUser();
}
}
void authUser() {
print("authUser");
JsObject fb = context["FB"];
fb.callMethod("login", [loginCallback]);
}
void loginCallback (response) {
print("loginCallback");
if(response["authResponse"]) {
showPhoto(response["authResponse"]["userID"]);
}
}
這些方法,但初始化失敗,錯誤Uncaught InvalidCharacterError: The string contains invalid characters.
深入探究Facebook SDK我發現了這個錯誤的原因。初始化函數試圖發送HTTP創建一個iframe請求
httpProxyFrame = createIframe({
url: httpDomain + proxyUrl,
name: 'fb_xdm_frame_http',
id: 'fb_xdm_frame_http',
root: container,
'aria-hidden':true,
title: IFRAME_TITLE,
'tab-index': -1
});
功能createIframe
做此檢查
if (hasNamePropertyBug()) {
frame = document.createElement('<iframe name="' + name + '"/>');
} else {
frame = document.createElement("iframe");
frame.name = name;
}
功能hasNamePropertyBug
做到這一點
function hasNamePropertyBug() {
return typeof hasBug === 'undefined'
? test()
: hasBug;
};
function test() {
var form = document.createElement("form"),
input = form.appendChild(document.createElement("input"));
input.name = guid();
hasBug = input !== form.elements[input.name];
form = input = null;
return hasBug;
};
至於document.createElement
使用飛鏢的domCreateElement
功能,其中包含一個元素,hasBug
變量等於true
,因爲它比較GeneratedWrapper
包裝元素。
所以,hasNamePropertyBug
回報true
這個代碼拋出錯誤
document.createElement('<iframe name="' + name + '"/>');
所以,我的問題是如何達特環境中使用Facebook SDK?
不是最優雅的解決方案,但'hasBug'似乎是全球性的,那麼在做任何FB的東西之前將它設置爲'true'就可以了?名稱屬性錯誤只適用於IE7及以下版本,而Dart不支持。如果你不需要迎合傳統瀏覽器,這可能是可行的。 – MarioP
@MarioP看看http://connect.facebook.net/en_US/all/debug.js'hasBug'是'hasNamePropertyBug'函數的局部變量,它是動態創建的。似乎更容易覆蓋整個'hasNamePropertyBug'函數,但我不知道它是可能的 –
也許我錯過了一些東西,但它仍然看起來像它在全局對象上定義的。 'var hasBug;',正好在空行和函數test()的定義之上。 – MarioP