利用者:Penn Station/util.js
表示
お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。
多くの Windows や Linux のブラウザ
- Ctrl を押しながら F5 を押す。
Mac における Safari
Mac における Chrome や Firefox
- ⌘ Cmd と ⇧ Shift を押しながら R を押す。
詳細についてはWikipedia:キャッシュを消すをご覧ください。
// Utility Scripts
//
// insertTags2() with hookEvent() Version 3.0b
// Date: 2024-08-15
// Contact: Penn Station
// Note: This script is a derivative work of insertTags() at http://ja-two.iwiki.icu/skins-1.5/common/edit.js
window.currentFocused = undefined;
function insertTags2 ( tagOpen, tagClose, sampleText ) {
var txtarea = currentFocused;
var selText, isSample = false;
var winScroll;
if ( document.selection && document.selection.createRange ) { // IE/Opera
// save window scroll position
if ( document.documentElement && document.documentElement.scrollTop ) {
winScroll = document.documentElement.scrollTop;
} else if ( document.body ) {
winScroll = document.body.scrollTop;
}
// get current selection
txtarea.focus();
var range = document.selection.createRange();
selText = range.text;
// insert tags
checkSelectedText();
range.text = tagOpen + selText + tagClose;
// mark sample text as selected
if ( isSample && range.moveStart ) {
if ( window.opera ) {
tagClose = tagClose.replace(/\n/g,'');
}
range.moveStart('character', - tagClose.length - selText.length);
range.moveEnd('character', - tagClose.length);
}
range.select();
// restore window scroll position
if ( document.documentElement && document.documentElement.scrollTop ) {
document.documentElement.scrollTop = winScroll;
} else if ( document.body ) {
document.body.scrollTop = winScroll;
}
} else if ( txtarea.selectionStart || txtarea.selectionStart == '0' ) { // Mozilla
// save textarea scroll position
var textScroll = txtarea.scrollTop;
// get current selection
txtarea.focus();
var startPos = txtarea.selectionStart;
var endPos = txtarea.selectionEnd;
selText = txtarea.value.substring( startPos, endPos );
// insert tags
checkSelectedText();
txtarea.value = txtarea.value.substring(0, startPos)
+ tagOpen + selText + tagClose
+ txtarea.value.substring(endPos, txtarea.value.length);
// set new selection
if ( isSample ) {
txtarea.selectionStart = startPos + tagOpen.length;
txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
} else {
txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
txtarea.selectionEnd = txtarea.selectionStart;
}
// restore textarea scroll position
txtarea.scrollTop = textScroll;
}
function checkSelectedText() {
if ( !selText ) {
selText = sampleText;
isSample = true;
} else if ( selText.charAt(selText.length - 1) == ' ' ) { // exclude ending space char
selText = selText.substring(0, selText.length - 1);
tagClose += ' ';
}
}
}
$ (function() {
currentFocused = document.getElementById( 'wpTextbox1' );
// http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
// focus does not bubble normally, but using a trick we can do event delegation
// on the focus event on all text inputs to make the toolbox usable on all of them
var editForm = document.getElementById( 'editform' );
var movepage = document.getElementById("movepage");
var deleteconfirm = document.getElementById("deleteconfirm");
var undelete = document.getElementById("undelete");
//var mw_revdel_form_revisions = document.getElementById("mw-revdel-form-revisions");
var mw_revdel_form_revisions = document.getElementById("wpReason");
var mw_img_deleteconfirm = document.getElementById("mw-img-deleteconfirm");
if ( !editForm && !movepage && !deleteconfirm && !undelete && !mw_revdel_form_revisions && !mw_img_deleteconfirm ) {
return;
}
function onfocus( e ) {
var elm = e.target || e.srcElement;
if ( !elm ) {
return;
}
var tagName = elm.tagName.toLowerCase();
var type = elm.type || '';
if ( tagName !== 'textarea' && tagName !== 'input' ) {
return;
}
if ( tagName === 'input' && type.toLowerCase() !== 'text' ) {
return;
}
currentFocused = elm;
}
//if ( editForm.addEventListener ) {
// // Gecko, WebKit, Opera, etc... (all standards compliant browsers)
// editForm.addEventListener( 'focus', onfocus, true ); // This MUST be true to work
//} else if ( editForm.attachEvent ) {
// // IE needs a specific trick here since it doesn't support the standard
// editForm.attachEvent( 'onfocusin', function() { onfocus( event ); } );
//}
// addEventListener to the document element - getting focus from any focused elements.
if ( document.addEventListener ) {
// Gecko, WebKit, Opera, etc... (all standards compliant browsers)
document.addEventListener( 'focus', onfocus, true ); // This MUST be true to work
} else if ( document.attachEvent ) {
// IE needs a specific trick here since it doesn't support the standard
document.attachEvent( 'onfocusin', function() { onfocus( event ); } );
}
// HACK: make currentFocused work with the usability iframe
// With proper focus detection support (HTML 5!) this'll be much cleaner
if ( typeof $ != 'undefined' ) {
var iframe = $( '.wikiEditor-ui-text iframe' );
if ( iframe.length > 0 ) {
$( iframe.get( 0 ).contentWindow.document )
.add( iframe.get( 0 ).contentWindow.document.body ) // for IE
.focus( function() { currentFocused = iframe.get( 0 ); } );
}
}
editForm;
} );