User:Cobaltcigs/DiffPreviewFindLine
With this script enabled, clicking on any of the cells on the right-hand ("your text") side of a preview diff (after clicking the "show changes" button) of a pending edit should locate, select, and scroll to that line in the editing textbox. The intended purpose is to assist manually correcting any mistakes noticed in a script-assisted edit, prior to submitting.
Should be faster than ctrl-C,F,V anyway.
To activate, call this function from your own script sometime after load:
DiffPreviewFindLineSetup();
Known limitations:
- The script uses the line numbers shown in the diff preview and does not attempt to match text by similarity. Any further editing that adds or removes newline characters (
\n) after the diff page is loaded may invalidate the line numbers and cause the wrong line to be selected. In that case, you'll need to click the "show changes" button again to update the line numbers.- Actually matching the text would be tricky to do right because the diff preview evaluates pre-save transforms (e.g. subst, pipe trick, signature tildes).
- Plus you may have changed the entire line beyond recognition anyway.
One possible improvement (not implemented) would be to monitor every keystroke to see if \n has been added or removed (and at what position) and internally offset the line numbers accordingly. But that sounds really complicated...
Source code:
function GetCharacterIndices(s, c) {
var a = [];
for(var i = 0; i < s.length; i++) if(s[i] === c) a.push(i);
return a;
}
function FindLine(num) {
var wpTextbox1 = document.getElementById("wpTextbox1");
var indices = GetCharacterIndices(wpTextbox1.value, '\n');
indices.unshift(-1);
var start = indices[num-1]+1, end = indices[num];
var tmp = wpTextbox1.value;
location.hash = "";
wpTextbox1.scrollTop = 0;
wpTextbox1.value = tmp.substring(0, end);
wpTextbox1.scrollTop = wpTextbox1.scrollHeight;
wpTextbox1.value = tmp;
location.hash = "#wpTextbox1";
wpTextbox1.selectionStart = wpTextbox1.selectionEnd = start;
wpTextbox1.blur();
wpTextbox1.focus();
wpTextbox1.setSelectionRange(start, end);
}
function DiffPreviewFindLineSetup() {
var wikiDiff = document.getElementById("wikiDiff");
var isShowChanges = wikiDiff && (mw.config.get('wgAction') == "submit");
if(!isShowChanges) return;
var trs = wikiDiff.getElementsByTagName("tr"), lineNum = 0;
for(var i = 1; i < trs.length; i++) {
var tds = trs[i].getElementsByTagName("td");
if(tds.length == 2) {
lineNum = parseInt(tds[1].innerText.replace(/\D/g, ""));
continue;
}
var last = tds[tds.length-1];
if(/\bdiff-empty\b/.test(last.className)) continue;
last.setAttribute("onclick", "FindLine(" + lineNum + ")");
lineNum++;
}
}
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.