User:Esby/link-helper.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Esby/link-helper. |
/**
* to benefit of this script
* you need to load this javascript into your monobook.js
* by putting in it
*
* importScript('User:Esby/link-helper.js');
*
*
* Implements various displaying of wiki and non wiki links for copy/pasting.
*
*
* author esby
*/
/* Configuration: */
// in your monobook.js, set this variable to true to disable this script.
var nsl_disable = false;
var nsl_base;
var nsl_sec_base;
nsl_base = "http://commons.wikimedia.org/wiki/";
nsl_sec_base = "https://secure.wikimedia.org/wikipedia/commons/wiki/";
if ( wgNoticeProject == 'wikipedia')
{
nsl_base = "http://" + wgContentLanguage + ".wikipedia.org/wiki/";
nsl_sec_base = "https://secure.wikimedia.org/wikipedia/" + wgContentLanguage + "/wiki/";
}
// define some meta-variables
var nsl_first = true; // the first iteration?
var nsl_g_form = false;
var nsl_wikified_url;
var nsl_wikified_url_prefix;
var nsl_normal_url;
var nsl_secure_url
var use_prefixed;
var use_secure;
use_prefixed = true;
use_secure = false;
function nsl_calc_strings()
{
nsl_wikified_url = '[[' + wgPageName + '|' + wgFormattedNamespaces[wgNamespaceNumber] + ':' + wgTitle + ']]';
if (wgNamespaceNumber == 0 ) // no name space
nsl_wikified_url = '[[' + wgPageName + '|' + wgTitle + ']]';
if (wgNamespaceNumber == 14 ) // categories
nsl_wikified_url = '[[' + wgPageName + '|' + wgCanonicalNamespace + ':' + wgTitle + ']]';
if (wgNamespaceNumber == 6 ) // files
nsl_wikified_url = '[[' + wgPageName + '|' + 'thumb|right|' + wgCanonicalNamespace + ':' + wgTitle + ']]';
if (wgNamespaceNumber == 10 ) // templates
nsl_wikified_url = '{{' + wgTitle + '|'+ '}}';
nsl_wikified_url_prefix = '[[:' + wgPageName + '|' + wgFormattedNamespaces[wgNamespaceNumber] + ':' + wgTitle + ']]';
if (wgNamespaceNumber == 0 )
nsl_wikified_url_prefix = '[[:' + wgPageName + '|' + wgTitle + ']]';
if (wgNamespaceNumber == 10 )
nsl_wikified_url_prefix = '<nowiki>{{</nowiki>[[:' + wgPageName + '|'+ ']]<nowiki>}}</nowiki>';
nsl_normal_url = nsl_base + wgPageName;
nsl_secure_url = nsl_sec_base + wgPageName;
}
function nsl_get_wikitext()
{
use_prefixed = !(use_prefixed);
if (!use_prefixed)
return nsl_wikified_url;
else
return nsl_wikified_url_prefix;
}
function nsl_get_url()
{
use_secure = !(use_secure);
if (!use_secure)
return nsl_secure_url;
else
return nsl_normal_url;
}
// build widget
function nsl_buildWidget() {
// preventing multiple widget to be build per page
if (nsl_g_form) return;
// set up the floating form
var form = document.createElement('form');
form.appendSpace = function()
{
this.appendChild(document.createTextNode(' '));
}
form.appendBR = function()
{
this.appendChild(document.createElement('br'));
}
form.className = 'nsl_info';
form.onsubmit = function() {
return false; // don't perform action
};
// determining the urls
nsl_calc_strings();
// button to swap wiki url fields.
var submit = document.createElement('input');
submit.setAttribute('type', 'submit');
submit.value = '!';
submit.className = 'nsl_swap_wikitext';
submit.onclick = function() {
nsl_box1.value = nsl_get_wikitext();
exit;
};
form.appendChild(submit);
form.appendSpace()
// wikisyntax
// input box for the nsl info
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('size', '80');
input.setAttribute('maxlength', '250');
input.onclick = function() { this.select(); };
input.className = 'nsl_output_wikiname';
input.value = nsl_get_wikitext();
nsl_box1 = input;
form.appendChild(input);
form.appendSpace();
form.appendBR();
// button to swap url fields
var submit = document.createElement('input');
submit.setAttribute('type', 'submit');
submit.value = '!';
submit.className = 'nsl_swap_url';
submit.onclick = function() {
nsl_box2.value = nsl_get_url();
exit;
};
form.appendChild(submit);
form.appendSpace()
// non secure information
// input box for the nsl info
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('size', '80');
input.setAttribute('maxlength', '250');
input.onclick = function() { this.select(); };
input.className = 'nsl_output';
input.value = nsl_get_url();
nsl_box2 = input;
form.appendChild(input);
form.appendSpace()
nsl_g_form = form;
var bodyContent = document.getElementById('bodyContent');
bodyContent.insertBefore(nsl_g_form, bodyContent.firstChild);
}
function nsl_init()
{
// possible site for cookie checking to disable language select
if (nsl_disable)
return;
// if it's the first iteration...
if (nsl_first)
{
nsl_buildWidget();
}
nsl_first = false;
}
function nsl_register() {
nsl_init();
}
// register as onload function (there must be a better way)
if (window.addEventListener) {
window.addEventListener("load", nsl_register, true);
} else if (window.attachEvent) {
window.attachEvent("onload", nsl_register);
}