Wikipedysta:Nux/search n replace.js


Wikipedysta:Nux/search n replace.js w encyklopedii

Z Wikipedii, wolnej encyklopedii < Wikipedysta:Nux Przejdź do nawigacji Przejdź do wyszukiwania

Uwaga: aby zobaczyć zmiany po zapisaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5 lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Przejdź do Menu → Ustawienia (Opera → Preferencje w Mac), a następnie Prywatność i bezpieczeństwo → Wyczyść dane przeglądania → Opróżnij pamięć podręczną.
/* <pre> */ /* ======================================================================== *\ Search&Replace toolbar functions version: 0.3b copyright: (C) 2006 Maciej Jaros (pl:User:Nux, en:User:EcceNux) licence: GNU General Public License v2, http://opensource.org/licenses/gpl-license.php \* ======================================================================== */ addOnloadHook(initSearchReplaceToolbar); /* ===================================================== *\ Various settings. \* ===================================================== */ // // language var // var langSearchRep = { 'search in atricle' : 'szukaj w artykule', 'search from beginning' : 'szukaj od początku', 'text not found' : 'Nie znaleziono tekstu.' } /** English /** var langSearchRep = { 'search in atricle' : 'search in atricle', 'search from beginning' : 'search from beginning', 'text not found' : 'The text was not found.' } /**/ // line-height for textarea to search in var intSearchReplaceLineHeight = 16; /* ===================================================== *\ Function: initSearchReplaceToolbar Adding elements needed for Search&Replace toolbar Params ------ none \* ===================================================== */ function initSearchReplaceToolbar() { // // Check if there is an element to insert before (if this is an edit) // and which should we insert before // var beforeElem = document.getElementById('toolbar'); if (!beforeElem) { // as sometimes toolbar doesn't exist (protected pages) beforeElem = document.getElementById('wpTextbox1'); if (!beforeElem) { return; } } if (!beforeElem.insertBefore) { return; } // // Makes it easier to predict line-height :-) // var txtarea = document.getElementById('wpTextbox1'); txtarea.style.lineHeight = ''+intSearchReplaceLineHeight+'px'; // // Creating elements // var search_rep_bar; search_rep_bar = document.createElement("div"); var newNode; // // <input type="text" // name="js_art_search" id="js_art_search" /> newNode = document.createElement("input"); newNode.setAttribute('type','text'); newNode.setAttribute('name','js_art_search'); newNode.setAttribute('id','js_art_search'); newNode.setAttribute('value','\\#[0-9]'); search_rep_bar.appendChild(newNode); // // <input type="button" // name="js_art_search_submit" id="js_art_search_submit" // value="szukaj w artykule" onclick="jsArtSearch()" /> newNode = document.createElement("input"); newNode.setAttribute('type','button'); newNode.setAttribute('name','js_art_search_submit'); newNode.setAttribute('id','js_art_search_submit'); newNode.setAttribute('value',langSearchRep['search in atricle']); newNode.setAttribute('onclick','jsArtSearch()'); search_rep_bar.appendChild(newNode); // // <label><input type="checkbox" // name="js_art_search_beginning" id="js_art_search_beginning" // value="1"/>szukaj od początku</label> // // label var newNodeParent; newNodeParent = document.createElement("label"); // // input newNode = document.createElement("input"); newNode.setAttribute('type','checkbox'); newNode.setAttribute('name','js_art_search_beginning'); newNode.setAttribute('id','js_art_search_beginning'); newNode.setAttribute('value','1'); newNodeParent.appendChild(newNode); // // text = szukaj od początku newNode = document.createTextNode(langSearchRep['search from beginning']); newNodeParent.appendChild(newNode); search_rep_bar.appendChild(newNodeParent); // // Add search_rep_bar somewhere before textarea // document.getElementById('bodyContent').insertBefore(search_rep_bar, beforeElem); } /* ===================================================== *\ Function: jsArtSearch Searching for text in textarea with id=wpTextbox1 Params ------ none \* ===================================================== */ function jsArtSearch() { // var lineheight = Math.floor(12*1.2)+2; // fontsize * lineheight_normal + margins // let's just skip that :> (below should be set in init or user CSS) var lineheight = intSearchReplaceLineHeight; // // Settings // var objFromBeginning = document.getElementById('js_art_search_beginning'); var ifFromBeginning = objFromBeginning.checked; // always uncheck to enable searching further in text objFromBeginning.checked = false; var txtarea = document.getElementById('wpTextbox1'); var search = document.getElementById('js_art_search').value; var myReg = new RegExp(search,'ig'); // wielkosc liter pomijana // // search from the end of current selection // // doesn't work in IE! // if (!ifFromBeginning && txtarea.textLength != txtarea.selectionStart) myReg.lastIndex = txtarea.selectionEnd ; // // search var myOdp = myReg.exec(txtarea.value); // // if found then change the selection if (myOdp) { // // Select // // txtarea.setSelectionRange(myOdp.index,myReg.lastIndex); mySetSelectionRange(txtarea, myOdp.index, myReg.lastIndex); // // Set scroll so that the found element will be // visible without any scrolling from user // mySetScroll(txtarea, myOdp.index, lineheight) /* printDebug('' + 'scrl:' + txtarea.scrollTop + '<br/>' ); */ } else { alert(langSearchRep['text not found']); } } /* ===================================================== *\ Function: mySetSelectionRange Should select (mark) text in given input from selectionStart to selectionEnd Params ------ objInput - input object selectionStart - index of a first character in the text to select selectionEnd - index of a last character in the text to select \* ===================================================== */ function mySetSelectionRange(objInput, selectionStart, selectionEnd) { // // Mozilla + Opera // if (objInput.setSelectionRange) { objInput.focus (); objInput.setSelectionRange (selectionStart, selectionEnd); } // // IE // else if (objInput.createTextRange) { var range = objInput.createTextRange(); range.collapse (true); range.moveEnd ('character', selectionEnd); range.moveStart ('character', selectionStart); range.select (); } } // // Nie bierze pod uwagę przenoszenia długich wyrazów // /* ===================================================== *\ Function: mySetScroll Sets scroll of (multiline) objInput so selectionStart will be visible. This is a little slow because there is no easy way to compute exact row number of some character. Problems with that: * simply no atrribute that would give you this * firstly you need to count \n before the character * secondy you need to take into account word wrapping by num of columns * thirdly you should take into account that word wrapping doesn't simply cut words but moves them to another line (living empty space on the end of line) The last problem is solved by making columns a little shorter then they really are (more or less with an avarage word length divided by 2). More problems: * How to determine real number of cols? objInput.cols is not working correctly e.g. when you have width=100%. Params ------ objInput - (multiline) input object with a scroll bar selectionStart - index of a first character which should be made visible lineheight - exact height of line (in pixels) in the given input object \* ===================================================== */ function mySetScroll(objInput, selectionStart, lineheight) { var inputCols = objInput.cols - 5; // 5 = (average word length)/2 //var inputCols = 92 - 5; // 5 = (average word length)/2 // // Mozilla + Opera // // if (txtarea.scrollTop) // // Fast but this a very rough guess // var sel_line = myOdp.index/(0.9*txtarea.cols); // 90% chars per line // // Slow&acurate (more or less) // var strInput = objInput.value; var sel_line = 0; var prev_pos = 0; var pos = -1; /** printDebug('<hr/>' + 'p-p:' + (pos) + '<br/>' + 'cols' + (inputCols) + '<br/>' + 'realCols' + (objInput.cols) + '<br/>' + 'selStart' + (selectionStart) + '<br/>' ); /**/ do { pos = strInput.indexOf("\n",pos+1); /** printDebug('' + 'p-p='+pos+'-'+prev_pos+'='+ (pos-prev_pos) + '<br/>' ); /**/ if (pos>selectionStart) pos = selectionStart ; if (pos-prev_pos>inputCols) sel_line += Math.ceil((pos-prev_pos-1)/inputCols) else sel_line ++ ; prev_pos = pos; } while (pos!=-1 && pos<selectionStart); /** printDebug('' + 'line:' + sel_line + '<br/>' ); flushDebug(); /**/ // just to be sure it will be visible sel_line -= 3; // // Set it finally! // objInput.scrollTop = sel_line * lineheight; } /* </pre> */ 
Na podstawie artykułu: "Wikipedysta:Nux/search n replace.js" pochodzącego z Wikipedii
OryginałEdytujHistoria i autorzy