MediaWiki:Common.js

De La Coppermind
Revisión del 08:39 8 sep 2018 de Fbstj (discusión | contribs.) (fix those links)
Ir a la navegación Ir a la búsqueda

Nota: tras guardar, quizás necesites actualizar la caché de tu navegador para ver los cambios.

  • Firefox/Safari: Mantén presionada la tecla Mayús mientras pulsas el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presiona Ctrl+Shift+R (⌘+Mayús+R en Mac)
  • Internet Explorer: mantén presionada Ctrl mientras pulsas Actualizar, o presiona Ctrl+F5
  • Opera: dirígete a Menú → Configuración (Opera → Preferencias en Mac) y luego a Privacidad y seguridad → Borrar datos de navegación → Imágenes y archivos en caché.
/* Any JavaScript here will be loaded for all users on every page load. */

/*
  wrapper for creating new html elements
*/
function ne(tag, attrs) {
  var _ = $(document.createElement(tag));
  for (var i in attrs)
    _.attr(i, attrs[i]);
  return _;
}

/* push Notes down below infoboxes */
$('h2:contains("Notes")').attr('class','notes');

/* replace "Coppermind:Welcome" with "Welcome to the Coppermind" */
$('h1:contains("Coppermind:Welcome")').text("Welcome to the Coppermind");

/* 2014-05
  add title attributes to all ref tags, containing the text shown in the ref
*/
$('[id*="cite_ref"]').each(function (i, el) {
    var el = $(el)
    var a = $('[href="#'+el.attr('id')+'"]')
    el.attr('title', a.next('.reference-text').text() || a.parent().nextAll('.reference-text').text())
})

/* 2017-11 - protect roll back links */
$('.mw-rollback-link').attr('onclick', "return confirm('definitely roll back?')")

/* 2014-04
  augment actionpaths edits to LocalSetting.php 
*/
$('[href*="title="]').attr('href', function (i, value) {
  return value.replace(/\/w\/index.php\?title=(.+?)\&(.+?)/, "/wiki/$1?$2")
})
$('#searchform').attr('action', '/wiki/Special:Search')
$('form').attr('action', function (i, value) {
  return value.replace("/w/index.php", "/wiki/" + $('[name="title"]', this).attr('value'))
})
$("[name='title']",$('form')).remove()

/* 2015-01
  add links to Brandon's site and the interview database to the sidebar
*/
jQuery(function () {

  function ModifySidebar( action, section, name, link ) {
  /* from https://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Add_or_remove_toolbox_sections_.28JavaScript.29 */
	try {
		switch ( section ) {
			case 'languages':
				var target = 'p-lang';
				break;
			case 'toolbox':
				var target = 'p-tb';
				break;
			case 'navigation':
				var target = 'p-navigation';
				break;
			default:
				var target = 'p-' + section;
				break;
		}
 
		if ( action == 'add' ) {
			var node = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];
 
			var aNode = document.createElement( 'a' );
			var liNode = document.createElement( 'li' );
 
			aNode.appendChild( document.createTextNode( name ) );
			aNode.setAttribute( 'href', link );
			liNode.appendChild( aNode );
			liNode.className = 'plainlinks';
			node.appendChild( liNode );
		}
 
		if ( action == 'remove' ) {
			var list = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];
 
			var listelements = list.getElementsByTagName( 'li' );
 
			for ( var i = 0; i < listelements.length; i++ ) {
				if (
					listelements[i].getElementsByTagName( 'a' )[0].innerHTML == name ||
					listelements[i].getElementsByTagName( 'a' )[0].href == link
				)
				{
					list.removeChild( listelements[i] );
				}
			}
		}
 
	} catch( e ) {
		// let's just ignore what's happened
		return;
	}
  }

  if (!mw.config.get('wgIsArticle') || mw.config.get('wgCanonicalNamespace') !== '')
    return;
  if (mw.config.get('wgCategories').includes('Meta'))
    return;
  var title = mw.config.get('wgTitle');
  ModifySidebar( 'add', 'toolbox', "Search Brandon's website for " + title, 'https://brandonsanderson.com/?s='+ title );
  ModifySidebar( 'add', 'toolbox', 'Search Arcanum for '+ title, 'https://wob.coppermind.net/adv_search/?query='+ title );
});

/* 2017-11: functions for generating links and buttons */
// make an <a> with a url and some text
function make_a(url, text) {
 const el = document.createElement('a')
 el.setAttribute('href', url)
 el.innerText = text
 return el
}

// make a nice link button for the edit box
function make_edit_button(url, text, title) {
 // create link element
 const ln = make_a(url, text)
 ln.setAttribute('target', '_blank')
 // create wrap element
 const el = document.createElement('span')
 el.setAttribute('title', title)
 el.appendChild(ln)
 // style elements
 // FIXME: these are copied from the oo-ui elements in the edit box
 ln.className = 'oo-ui-inputWidget-input oo-ui-buttonElement-button'
 el.className = 'oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-buttonInputWidget'
 // return element for insertion
 return el
}

// create a link that looks like .editsection
function make_editsection_link(url) {
 // generate an edit link
 const ln = make_a(url, 'edit')
 // wrap the link in an '.editsection' link floated right
 const el = document.createElement('span')
 el.className = 'mw-editsection'
 el.style.float = 'right'
 // push [] around the A element into the span
 el.appendChild(document.createTextNode('['))
 el.appendChild(ln)
 el.appendChild(document.createTextNode(']'))
 return el
}

/* add links to edit box */
$(function () {

const edit_box = document.querySelector('.editButtons')
// check that there is an edit box
if (!edit_box || mw.config.get('wgCanonicalNamespace') !== '') return

const page_name = mw.config.get('wgPageName').replace(/[_]/g, '+')

// add a link to the Arcanum
const wob_url = make_edit_button('https://wob.coppermind.net/adv_search/?query='+ page_name, 'Search Arcanum', 'Search Arcanum for "'+ page_name +'"')
edit_box.appendChild(wob_url)

});

/* 2018-08-22 auto-generate Cite:Arcanum pages for {{wob ref}} */
$(function () {
  // determine if this page is editing a Cite: Arcanum page
  const edit_box = document.querySelector('#editform textarea');
  // check that there is an edit box
  if (!edit_box || mw.config.get('wgCanonicalNamespace') !== 'Cite') return -1;
  // check that it's a page creation
  if (mw.config.get('wgArticleId') != 0) return -2;
  // check that it's an Arcanum page
  const page_name = mw.config.get('wgPageName');
  if (!page_name.startsWith('Cite:Arcanum-')) return -3;
  // work out the api url
  const entry = page_name.split('-')[1];
  const url = 'https://wob.coppermind.net/api/entry/'+ entry +'/';
  // start fetching from Arcanum
  fetch(url)
    .then(function(resp) { return resp.json() })
    .then(function(json) {
  // the <arcanum-entry-info> tag
  const tag = '<arcanum-entry-info date="'+ (json.date || json.event_date) +'" event="'+ json.event +'">'+ json.event_name +'</arcanum-entry-info>';
  // the rest of the page
  const page = tag +"<noinclude>\n\n== Users ==\n{{Special:WhatLinksHere/{{FULLPAGENAME}}|}}\n";
  // only fill in the edit box if it is currently blank
  if (edit_box.value == '') {
    edit_box.value = page;
    // preview the page
    document.querySelector('[name="wpPreview"]').click()
  }
    })
})