User:AngelinaBelle/smfcurve.js From Online Manual

< User:AngelinaBelle
Revision as of 22:41, 12 April 2011 by AngelinaBelle (talk | contribs) (testing code select)
Jump to: navigation, search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//HotCat won't work unless gadgets are installed?
//importScriptURI('http://commons.wikimedia.org/wiki/MediaWiki:Gadget-HotCat.js');
window.onload = add_code_select;

function add_code_select(){
var cont = document.getElementById('content');
var codes = getElementsByClass('code', cont, 'div');

if(codes != null){
for (var a in codes) {
var aSpan = document.createElement('span');
var aLink = document.createElement('a');
var aText = document.createTextNode("Code: [");

aSpan.appendChild(aText);
aText = document.createTextNode("Select");
aLink.appendChild(aText);
aLink.onclick = new Function("return smfWikiSelectText(this,true)");
aLink.id = 'link_code_'+a;
aSpan.appendChild(aLink);
var aText = document.createTextNode("]");
aSpan.appendChild(aText);

codes[a].insertBefore(aSpan, codes[a].firstChild);
codes[a].id = 'code_'+a;
}
}
}

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

// Get the text in a code tag.
function smfWikiSelectText(oElement, bActOnElement)
{
	oCurElement = oElement.id.substr(5);
	// The place we're looking for is one div up, and next door - if it's auto detect.
	if (typeof(bActOnElement) == 'boolean' && bActOnElement)
		var oCodeArea = document.getElementById(oCurElement);
	else
		var oCodeArea = oCurElement.parentNode.nextSibling;

var els = oCodeArea.getElementsByTagName('pre');
oCodeArea = els[0];

	if (typeof(oCodeArea) != 'object' || oCodeArea == null)
		return false;

	// Start off with my favourite, internet explorer.
	if ('createTextRange' in document.body)
	{
		var oCurRange = document.body.createTextRange();
		oCurRange.moveToElementText(oCodeArea);
		oCurRange.select();
	}
	// Firefox at el.
	else if (window.getSelection)
	{
		var oCurSelection = window.getSelection();
		// Safari is special!
		if (oCurSelection.setBaseAndExtent)
		{
			var oLastChild = oCodeArea.lastChild;
			oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, 'innerText' in oLastChild ? oLastChild.innerText.length : oLastChild.textContent.length);
		}
		else
		{
			var curRange = document.createRange();
			curRange.selectNodeContents(oCodeArea);

			oCurSelection.removeAllRanges();
			oCurSelection.addRange(curRange);
		}
	}

	return false;
}


Advertisement: