User:Writ Keeper/Scripts/autoping.js
Appearance
< User:Writ Keeper | Scripts
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. |
Documentation for this user script can be added at User:Writ Keeper/Scripts/autoping. |
textboxHasGottenFocus = false;
previousIndex = 0;
prefixArray=["@[[User:","@[[User:","[[User:","[[User:","<span style='display:none'>[[User:"];
suffixArray=["]]: ","]]","]]","]]","]]</span>"];
if(typeof teahouseNotified == "undefined")
{
teahouseNotified = false;
}
$(document).ready(function()
{
if(mw.config.get("wgAction") == "edit" && document.URL.indexOf("section") >= 0)
{
var uniqueUserNames = new Object();
var regex = /\[\[User:([^|#\/\]]+)/g;
var regexResult=$("#wpTextbox1").val().match(regex);
if(regexResult != null)
{
for(i = 0; i < regexResult.length; i++)
{
uniqueUserNames[regexResult[i].substr(7).replace("_", " ")] = "1";
}
$("#wpSummaryLabel").before('<select id="pingMenu" style="margin-right: 0.3em;" title="Ping:"><option>Select a user to ping:</option><option>' + Object.keys(uniqueUserNames).join("</option><option>") + '</option></select> ');
$("#pingMenu").after('<select id="pingStyleMenu" style="margin-right: 0.3em;" title="Ping style:"><option>@[[User:Foo|Foo]]:</option><option>@[[User:Foo|Foo]]</option><option>[[User:Foo|Foo]]</option><option>[[User:Foo|bar]]</option><option>(hidden)</option></select> <br/>');
$("#wpTextbox1").focus(function(){textboxHasGottenFocus = true;});
previousIndex = $("#wpTextbox1").val().length;
$("#pingMenu").change(function()
{
if(this.selectedIndex > 0)
{
var pingStyle = $("#pingStyleMenu").prop("selectedIndex");
if(pingStyle < 0 || pingStyle > 4)
{
pingStyle = 0;
}
var pingPrefix = prefixArray[pingStyle] + $(this).val();
var pingPipe = $(this).val();
var pingSuffix = suffixArray[pingStyle];
if(pingStyle == 4)
{
pingPipe = "";
}
else
{
pingPrefix += "|";
if(pingStyle == 3)
{
pingPipe = "piped text";
}
}
//if mw.toolbar api is available, use that to insert the ping
if(typeof mw.toolbar != "undefined")
{
if(pingStyle == 3)
{
mw.toolbar.insertTags(pingPrefix,pingSuffix,pingPipe);
}
else
{
mw.toolbar.insertTags(pingPrefix + pingPipe + pingSuffix, "", "");
}
}
else
{
//This section of code adapted from Ryan and Maximilian Ruta's answer at http://stackoverflow.com/questions/1891444/cursor-position-in-a-textarea-character-index-not-x-y-coordinates
var el = $("#wpTextbox1").get(0);
var pos = 0;
//if the textbox hasn't gotten focus yet, or since the last ping, we default to the end of the last ping (or just the end). if we don't handle this specifically, things get weird
if(!textboxHasGottenFocus)
{
pos = previousIndex;
}
else if('selectionStart' in el)
{
pos = el.selectionStart;
} else if('selection' in document)
{
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
//end attribution
$("#wpTextbox1").val($("#wpTextbox1").val().substr(0, pos) + pingPrefix + pingPipe + pingSuffix + $("#wpTextbox1").val().substr(pos));
previousIndex = ($("#wpTextbox1").val().substr(0, pos) + pingPrefix + pingPipe + pingSuffix).length;
textboxHasGottenFocus = false;
}
teahouseNotified = true;
}
});
}
}
});