やや奇妙な問題があります。ユーザーがGoogleドキュメントを編集すると、自動的に下にスクロールするChrome拡張機能を作成しました。ドキュメントがページ付けモードの場合は、正常に機能しているようです。コンパクトモードに切り替えると、jQueryが要求されたすべての要素を取得できないようです。基本的なHTMLツリーは、ページ付けモードとコンパクトモードでほとんど同じです。私は困惑していて、なぜそれが正しく機能していないのかわかりません。'$currentPage'がどのように機能するかを主に確認する必要があります。高さは、発生してはならないときに0を返すことがあります。
[編集]これが台無しになるときです:それはページ付けされたモードでうまく働きます。しかし、2ページ目にすでにテキストがあるときにコンパクトモードで拡張機能を開始すると、期待どおりに機能します。1ページ目までテキストを削除しても、期待どおりに機能します。しかし、2ページ目にテキストを入力し始めると、混乱し始めます。
コードは次のとおりです。
function autoScrollFunc(){
if(autoscrollEnabled){
var oldCount = pageCount;
// get all the pages
var $pages = $("div.kix-page");
// count how many pages there are
pageCount = $pages.length;
console.dir($pages);
console.dir($pages.last());
console.dir($pages.last().find("div.kix-paragraphrenderer"));
$currentPage = $pages.last().find("div.kix-paragraphrenderer").first();
//console.dir($currentPage);
//pageHeight = $pages.first().height();
if(pageCount != oldCount){
// update data if there are changes in page count
console.log("Number of pages changed! Count: " + pageCount);
pageHeight = $pages.first().height();
}
// scroll to new position
scrollToNewPosition();
}
}
function scrollToNewPosition(){
if(autoscrollEnabled){
oldHeight = viewLoc;
var upperSection = toolbarHeight + getRulerHeight();
var windowHeight = $(window).height();
var marginHeight = (pageCount > 1) ? getTopMarginHeight() : firstMarginHeight;
var docViewHeight = (windowHeight > upperSection) ? (windowHeight - upperSection) : 0;
// calculate the scroll location
var totalDocHeight = (pageHeight * (pageCount-1)) + $currentPage.height() + marginHeight;
viewLoc = (totalDocHeight < docViewHeight) ? 0 : totalDocHeight - docViewHeight + $currentPage.height();
console.log("upperSection: %s, windowHeight: %s, marginHeight: %s, docViewHeight: %s, totalDocHeight: %s, viewLoc: %s, currentPage.height(): %s",
upperSection, windowHeight, marginHeight, docViewHeight, totalDocHeight, viewLoc, $currentPage.height());
if(oldHeight != viewLoc){
// if there are changes in height, then scroll to the new position
$docScroller.animate({ scrollTop: viewLoc}, 250);
}
}
}
拡張機能を自分で試してみたい場合は、プロジェクトのURLを次に示します。
http://code.google.com/p/google-docs-autoscroll/
よろしくお願いします。