我们的 iframe 中有页面,它们将内容附加到页面,然后允许用户在适当的位置刷新页面内的内容。在这些情况下,我们看到了同样的情况,内容不会在适当的时候缩小。
FB.Canvas.setSize() 调用_computeContentSize,如下图所示:
_computeContentSize: function() {
var body = document.body,
docElement = document.documentElement,
right = 0,
bottom = Math.max(
Math.max(body.offsetHeight, body.scrollHeight) +
body.offsetTop,
Math.max(docElement.offsetHeight, docElement.scrollHeight) +
docElement.offsetTop);
if (body.offsetWidth < body.scrollWidth) {
right = body.scrollWidth + body.offsetLeft;
} else {
FB.Array.forEach(body.childNodes, function(child) {
var childRight = child.offsetWidth + child.offsetLeft;
if (childRight > right) {
right = childRight;
}
});
}
if (docElement.clientLeft > 0) {
right += (docElement.clientLeft * 2);
}
if (docElement.clientTop > 0) {
bottom += (docElement.clientTop * 2);
}
return {height: bottom, width: right};
},
有问题的行在这里:
bottom = Math.max(
Math.max(body.offsetHeight, body.scrollHeight) +
body.offsetTop,
Math.max(docElement.offsetHeight, docElement.scrollHeight) +
docElement.offsetTop);
即使 iframe 中的内容缩小了,body.offsetHeight 的值也不会缩小。
为了解决这个问题,我们制作了一个自定义版本的 computeContentSize 函数,它只咨询 docElement 的高度,如下所示:
function rfComputeContentSize() {
var body = document.body,
docElement = document.documentElement,
right = 0,
bottom = Math.max(docElement.offsetHeight, docElement.scrollHeight) + docElement.offsetTop;
if (body.offsetWidth < body.scrollWidth) {
right = body.scrollWidth + body.offsetLeft;
} else {
FB.Array.forEach(body.childNodes, function(child) {
var childRight = child.offsetWidth + child.offsetLeft;
if (childRight > right) {
right = childRight;
}
});
}
if (docElement.clientLeft > 0) {
right += (docElement.clientLeft * 2);
}
if (docElement.clientTop > 0) {
bottom += (docElement.clientTop * 2);
}
return {height: bottom, width: right};
}
任何时候我们想要调整大小并且知道内容可能会缩小,我们将使用自定义函数将内容传递给 setSize(例如 FB.Canvas.setSize(rfComputeContentSize())),并且任何时候我们知道内容只会增长,我们'将使用标准的 FB.Canvas.setSize() 函数。
请注意,我们使用的是 setAutoGrow() 并且我没有检查,但我假设它使用相同的函数来确定大小。我们禁用了对 setAutoGrow() 的调用,并且必须对在适当的时间调用 setSize() 保持警惕。
使用 Facebook 记录此错误:https ://developers.facebook.com/bugs/228704057203827