0

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;

提案はscrollTopFirefox では失敗し、代わりにブラウザーの現在のページが の値に置き換えられます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();
};

最後のセミコロンが厳密に必要かどうかはわかりませんが、まあ、解決策です!

4

2 に答える 2

0

編集された質問に従って、myField.focus();最後に追加すると問題が解決しました。

于 2010-11-09T00:16:04.823 に答える
0

ブックマークレットが長すぎてブックマークに収まらない場合があります。オプションは、動的スクリプト タグを使用することです。

javascript:document.body.appendChild(document.createElement('script')).setAttribute('src','http://mysite/myscript.js')

myscript.js は、作業を行う実際のスクリプトです。

自己完結型のブックマークレットとして保持する場合は、必ず全体 (「javascript:」の後) を中括弧で囲んでください。

于 2010-11-08T22:57:17.483 に答える