Jump to content
Wikipedia The Free Encyclopedia

User:V111P/SKLChanger.js

From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:V111P/SKLChanger.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
 /* Simple Keyboard Layout Changer v. 2013年11月17日
  * http://en.wikipedia.org/wiki/User:V111P/js/Simple_Keyboard_Layout_Changer
  *
  * With this script you can transform the characters you type in input boxes and textareas
  * into different characters - for example, into characters from a different language.
  * The script supports multiple groups of characters to convert to and you can switch
  * between the different groups with a click on a link.
  * Warning: Test the undo function (Ctrl-Z) in your browser to see if you like it.
  * Undo doesn't work in Google Chrome 30 at all.
  *
  * CC0 Public Domain Dedication:
  * http://creativecommons.org/publicdomain/zero/1.0/
  */

 window.sKLChanger=(function($){
 'use strict';

 // US keyboard layout is used by default
 vardefaultCharsToConvert='`-='
 +'qwertyuiop[]\\'
 +'asdfghjkl;\''
 +'zxcvbnm,./'
 // with shift
 +'~!@#$%^&*()_+'
 +'QWERTYUIOP{}|'
 +'ASDFGHJKL:"'
 +'ZXCVBNM<>?';

 varlang=mw.config.get('wgUserLanguage');
 varmsgs={
 indicatorLabelWhenTurnedOff_short:(lang=='bg'?'Изкл.':'OFF'),
 indicatorLabelWhenTurnedOff_long:(lang=='bg'
 ?'Превключвателят е изключен'
 :'The Keyboard Layout Changer is turned off'),
 nonsupportedBrowserMsg_short:'<s>OFF</s>',
 nonsupportedBrowserMsg_long:(lang=='bg'
 ?'Превключвателят на клавиатурни подредби няма да работи с вашия браузър'
 :'Simple Keyboard Layout Changer: Browser not supported')
 };

 vardefaultIndicatorPos={
 nearElementSelector:'#editform',//'prepend' on #editform doesn't (always) work in IE
 position:'before'// 'before', 'after', 'prepend', 'append'
 };

 varinputBoxesSelector='textarea, input[type="text"], input[type="password"], '
 +'input:not([type]), input[name="search"], input#searchInput';
 // Some MediaWiki skins don't have type="text" attribute on their search input boxes.
 // :not() not supported in older browsers

 varinputBoxesDeselector='input#wpAntispam';// #wpAntispam is a hidden input box in MediaWiki

 varshowLayoutHint;// if true, the string with the characters will be shown
 // in the title attribute of the long-label indicator link

 varlongLabelClassName='sKL_longIndicator';

 varkeyboardLayouts=[];
 varcurrLayoutN=-1;// none initially
 varcharsToConvertMap={};
 varkeyboardLayoutIndicator=$([]);// create an empty jQuery object
 varbrowser;// 'ok', 'ie', 'other'


 // init() - auto called if a window.sKLChangerConfig object exists at the time of the loading of this file
 functioninit(configObj){
 // jQuery is passing a function as an argument on $(init)
 configObj=(typeofconfigObj=='object'?configObj:(window.sKLChangerConfig||{}));

 varinputArea=$(inputBoxesSelector).not(inputBoxesDeselector)
 .filter(':visible')[0];// hidden elements cause an error in Firefox 6

 if(!inputArea)
 return;

 setCharsToConvert(configObj.charsToConvert||defaultCharsToConvert);
 showLayoutHint=(configObj.showLayoutHint===false?false:true);

 // add-and-replace the passed messages to the msgs object
 $.extend(msgs,configObj.messages);
 if(typeofconfigObj.offName=='string')
 msgs.indicatorLabelWhenTurnedOff_short=configObj.offName;
 if(typeofconfigObj.offLabel=='string')
 msgs.indicatorLabelWhenTurnedOff_long=configObj.offLabel;

 if((window.wikEd&&wikEd.useWikEd)
 ||(inputArea&&typeofinputArea.selectionStart!='undefined')
 )
 browser='ok';
 elseif(document.selection&&document.selection.createRange)
 browser='ie';// old IE & IE in Compatibility View mode
 else{
 browser='other';// won't work
 msgs.indicatorLabelWhenTurnedOff_long=msgs.nonsupportedBrowserMsg_long;
 }

 removeAllLayouts();

 if(configObj.layouts)
 $.each(configObj.layouts,function(i,val){
 addLayout(val.name,val.label,val.chars);
 });

 addIndicator(configObj.indicatorPosition);
 vardefaultLayout=configObj.defaultLayout;
 if(typeofdefaultLayout!='undefined')
 turnOn(defaultLayout);
 }


 functionaddIndicator(positionObj){
 varlongIndicatorID='sKL_indicator2';
 varlabel_short=msgs.indicatorLabelWhenTurnedOff_short;
 varlabel_long=msgs.indicatorLabelWhenTurnedOff_long;
 if(browser=='other'){
 label_short=msgs.nonsupportedBrowserMsg_short;
 label_long=msgs.nonsupportedBrowserMsg_long;
 }

 keyboardLayoutIndicator.remove();// remove any indicators already on the page
 keyboardLayoutIndicator=$([]);
 $('#sKLChangerPortlet').remove();
 $('#'+longIndicatorID).remove();

 // first add the indicator in the "personal bar"
 mw.util.addPortletLink('p-personal','#','['+label_short+']',
 'sKLChangerPortlet',label_long);
 varindicatorLink=$('#sKLChangerPortlet').find('a');
 keyboardLayoutIndicator=keyboardLayoutIndicator.add(indicatorLink);

 // then add the indicator/switch near the specified element, if it exists

 varnearElementSelector=defaultIndicatorPos.nearElementSelector;
 varposition=defaultIndicatorPos.position;

 if(positionObj
 &&typeofpositionObj.nearElementSelector=='string'
 &&typeofpositionObj.position=='string'
 &&$(positionObj.nearElementSelector).length!=0
 ){
 nearElementSelector=positionObj.nearElementSelector;
 position=positionObj.position;
 }

 if($(nearElementSelector).length>0){
 varindicator2=$('<a/>',{
 href:'#',
 id:longIndicatorID,
 'class':longLabelClassName,
 text:'['+label_long+']'
 });

 switch(position){
 case'before':indicator2.insertBefore(nearElementSelector);break;
 case'after':indicator2.insertAfter(nearElementSelector);break;
 case'prepend':indicator2.prependTo(nearElementSelector);break;
 case'append':default:indicator2.appendTo(nearElementSelector);
 }

 keyboardLayoutIndicator=keyboardLayoutIndicator.add(indicator2);
 }

 // finally, attach the event handler to all indicators
 keyboardLayoutIndicator.bind('click',function(event){
 event.preventDefault();
 nextLayout();
 });
 }


 functionremoveLayout(layoutToRemove){
 if(typeoflayoutToRemove=='string'){
 $.each(keyboardLayouts,function(indexInArray,layout){
 if(layout.name==layoutToRemove){
 turnOff();
 keyboardLayouts.splice(indexInArray,1);
 returnfalse;
 }
 });
 }
 elseif(typeoflayoutToRemove=='number'){
 turnOff();
 keyboardLayouts.splice(layoutToRemove,1);
 }
 }


 functionremoveAllLayouts(){
 turnOff();
 keyboardLayouts=[];
 }


 functiongetAllLayouts(){
 returnkeyboardLayouts;
 }


 functionaddLayout(name,label,layoutStr,position){
 if(!name||!label||!layoutStr)
 return;

 if(typeofposition=='undefined'
 ||position>=keyboardLayouts.length
 ||position<0
 )
 position=keyboardLayouts.length;

 varlayout={name:name,label:label,layoutStr:layoutStr};
 keyboardLayouts.splice(position,0,layout);
 if(position<=currLayoutN)
 currLayoutN++;// keep current layout
 }


 functionsetCharsToConvert(charStr){
 charStr=charStr||'';

 // each character code of the chars in charStr is converted
 // to a property name in charsToConvertMap with the value being
 // the position of the char in charStr.
 for(vari=0;i<charStr.length;i++){
 charsToConvertMap[charStr.charCodeAt(i)]=i;
 }
 }


 functionerror(msg){
 if(console&&console.error)
 console.error('Simple Keyboard Layout Changer: '+msg);
 }


 functionlayoutNameToIndex(layoutName){
 if(typeoflayoutName!='string'){
 error('layoutNameToIndex: layout-name string expected, got: '+layoutName);
 return-1;
 }
 for(vari=0;i<keyboardLayouts.length;i++)
 if(keyboardLayouts[i].name===layoutName)
 returni;
 error('layoutNameToIndex: Can\'t find layout with name "'+layoutName+'"');
 return-1;
 }


 functionturnOn(layoutName){
 if(keyboardLayouts.length==0){
 error('No keyboard layouts defined.');
 return;
 }

 if(typeoflayoutName=='undefined')
 currLayoutN=0;
 else{
 currLayoutN=layoutNameToIndex(layoutName);
 if(currLayoutN==-1){
 turnOff();
 return;
 }
 }

 $(inputBoxesSelector).on('keypress.sKLChanger',keyPressed);
 if(window.wikEd){
 wikEd.AddEventListener(wikEd.frameDocument,'keypress',keyPressed,true);
 }
 setIndicatorLabel();
 }


 functionturnOff(){
 $(inputBoxesSelector).off('keypress.sKLChanger');
 currLayoutN=-1;
 setIndicatorLabel();
 if(window.wikEd){
 wikEd.RemoveEventListener(wikEd.frameDocument,'keypress',keyPressed,true);
 }
 }


 functionnextLayout(){
 if(currLayoutN+1==keyboardLayouts.length)
 turnOff();
 else
 if(currLayoutN==-1)
 turnOn();
 else{
 currLayoutN++;
 setIndicatorLabel();
 }
 }


 functionsetIndicatorLabel(){
 setTimeout(safeSetIndicatorLabel,1);
 }


 functionsafeSetIndicatorLabel(){
 varlayoutName,layoutLabel;

 varlongIndicatorTitleAttr=(currLayoutN>-1&&showLayoutHint
 ?keyboardLayouts[currLayoutN].layoutStr
 :'');

 if(currLayoutN>-1){
 layoutName=keyboardLayouts[currLayoutN].name;
 layoutLabel=keyboardLayouts[currLayoutN].label;
 }
 else{
 layoutName=msgs.indicatorLabelWhenTurnedOff_short;
 layoutLabel=msgs.indicatorLabelWhenTurnedOff_long;
 }

 keyboardLayoutIndicator.each(function(index,indicatorEl){
 varindicator=$(indicatorEl);
 vartxt,title;

 if(indicator.hasClass(longLabelClassName)){
 txt=layoutLabel;
 title=longIndicatorTitleAttr;
 }
 else{
 txt=layoutName;
 title=layoutLabel;
 }

 setTimeout(function(){
 indicator.text('['+txt+']').attr('title',title);
 },1);
 });
 }


 functionkeyPressed(event){
 varcharCode=event.charCode||event.keyCode;
 if(event.charCode===0)
 charCode=0;// Firefox non-printable

 if(charCode==0||event.ctrlKey||event.altKey||event.metaKey)
 returntrue;

 switch(charCode){
 case0://Non-printable
 case13://Enter
 case27://Esc (for IE)
 case32://Space
 returntrue;
 }

 varcurrLayoutStr=keyboardLayouts[currLayoutN].layoutStr;
 varindex=charsToConvertMap[charCode];
 if(typeofindex=='undefined')
 returntrue;
 varcharToReplaceWith=currLayoutStr.charAt(index);

 if(charToReplaceWith){
 event.preventDefault();
 varinputBox=this;
 setTimeout(function(){// if the textarea value is changed directly, IE clears its undo history
 insertStr(charToReplaceWith,inputBox);
 },1);
 }

 }


 functioninsertStr(str,inputBox){
 if(window.wikEd&&wikEd.useWikEd&&inputBox==wikEd.frameDocument){
 wikEd.FrameExecCommand('inserthtml',
 str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'));
 }
 elseif(browser=='ok'){
 varstartPos=inputBox.selectionStart;
 varendPos=inputBox.selectionEnd;
 varscrollTop=inputBox.scrollTop;
 varpos=startPos+str.length;
 inputBox.value=inputBox.value.substring(0,startPos)
 +str
 +inputBox.value.substring(endPos,inputBox.value.length);
 inputBox.focus();
 inputBox.setSelectionRange(pos,pos);
 inputBox.scrollTop=scrollTop;
 }
 elseif(browser=='ie'){
 varrange=document.selection.createRange();
 if(range.text)
 document.selection.clear();
 range.text=str;
 }
 }


 if(window.sKLChangerConfig){
 if(document.readyState=='complete'||document.readyState=='interactive')
 init();
 else
 $(init);// but $() doesn't work after errors
 }


 return{
 init:init,
 removeLayout:removeLayout,
 removeAllLayouts:removeAllLayouts,
 getAllLayouts:getAllLayouts,
 addLayout:addLayout,
 setCharsToConvert:setCharsToConvert,
 turnOn:turnOn,
 turnOff:turnOff,
 nextLayout:nextLayout,
 // variables / constants:
 defaultCharsToConvert:defaultCharsToConvert
 };

 })(jQuery);

AltStyle によって変換されたページ (->オリジナル) /