http://snippets.dzone.com/posts/show/4973の JavaScriptとそのscrollTop
下の提案を使用して、事前設定されたテキスト文字列を Blogger の新しい投稿に挿入するためのブックマークレットを作成していますtextarea
。コードは次のようになります。
//IE support
if (document.selection) {
myField.focus();
//in effect we are creating a text range with zero
//length at the cursor location and replacing it
//with myValue
sel = document.selection.createRange();
sel.text = myValue;
//Mozilla/Firefox/Netscape 7+ support
} else if (myField.selectionStart || myField.selectionStart == '0') {
myField.focus();
//Here we get the start and end points of the
//selection. Then we create substrings up to the
//start of the selection and from the end point
//of the selection to the end of the field value.
//Then we concatenate the first substring, myValue,
//and the second substring to get the new value.
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length);
} else {
myField.value += myValue;
}
}
そしてその下の提案:
//add this to the start of function
textAreaScrollPosition = myField.scrollTop;
//add this to end of the function
myField.scrollTop = textAreaScrollPosition;
提案はscrollTop
Firefox では失敗し、代わりにブラウザーの現在のページが の値に置き換えられますtextAreaScrollPosition
。
これを、ブックマークレットのサンドイッチ ダウン バージョンの先頭に追加しました。
javascript:var myField=document.getElementById('postingHtmlBox');var myValue='lol';
全体として、次のように書かれています。
javascript:var myField=document.getElementById('postingHtmlBox');
var myValue='lol';
var textAreaScrollPosition=myField.scrollTop;
if(document.selection){myField.focus();
sel=document.selection.createRange();
sel.text=myValue;
}else if(myField.selectionStart||myField.selectionStart=='0'){myField.focus();
var startPos=myField.selectionStart;
var endPos=myField.selectionEnd;
myField.value=myField.value.substring(0,startPos)+myValue+myField.value.substring(endPos,myField.value.length);
myField.setSelectionRange(endPos+myValue.length,endPos+myValue.length);
}else{myField.value+=myValue;
}myField.scrollTop=textAreaScrollPosition;
ただし、改行なし。
私は JS の魔法使いではありません。テクノロジーに詳しくない友人が Blogger で少し複雑なことをするのを手伝おうとしているだけです。何か案は?
EDIT:プリミティブページ検出を追加し、プリセットテキストをプロンプトボックスに置き換えることに加えmyField.focus();
て、最後に追加することで元の問題を解決できました:
javascript:if(document.getElementById('postingHtmlBox')){var myField=document.getElementById('postingHtmlBox');
var myValue=prompt('Insert text here.');
var textAreaScrollPosition=myField.scrollTop;
if(document.selection){myField.focus();
sel=document.selection.createRange();
sel.text=myValue;
}else if(myField.selectionStart||myField.selectionStart=='0'){myField.focus();
var startPos=myField.selectionStart;
var endPos=myField.selectionEnd;
myField.value=myField.value.substring(0,startPos)+myValue+myField.value.substring(endPos,myField.value.length);
myField.setSelectionRange(endPos+myValue.length,endPos+myValue.length);
}else{myField.value+=myValue;
}myField.scrollTop=textAreaScrollPosition;
myField.focus();
};
最後のセミコロンが厳密に必要かどうかはわかりませんが、まあ、解決策です!