Greasemonkey mediawiki script

For writing the business plan of my current project (the gniddewthere.com website where groom, brides, relatives & friends can gather to prepare a wedding) I recently set up a private wiki. I may open this wiki public later to gain community feedback, but this is not today’s matter. So in order to easily edit this wiki based on the Mediawiki engine (Wikipedia’s), I felt I needed to have shortcuts. I was fed with having to click on the “edit” and “save” buttons all the time. So I wrote a little Greasemonkey script for using CTRL+E to edit a page and CTRL+S to save the modifications.

What is funny is while writing the script, I discovered that Mediawiki already has such built-in shortcuts ! I worked for nothing, but it was fun hacking again a little javascript and xpath expressions🙂

Nevertheless, here is the GreaseMonkey script:



// ==UserScript==
// @name          Wikimedia Shortcuts (ctrl+e to edit, ctrl+s to save)
// @namespace     http://www.fraaargh.wordpress.com
// @description   Add keyboard shortcuts for common wikimedia functions
// @include       http://yourOwnWiki.com/wiki/*
// @include       http://www.wikipedia.org/*
// ==/UserScript==

/* var editLink = document.evaluate(“//a[@accesskey=’e’]”,
document, null, XPathResult.ANY_TYPE,null).iterateNext(); */
var editLink = document.evaluate(“//li[@id=’ca-edit’]”,
document, null, XPathResult.ANY_TYPE,null).iterateNext().childNodes[0];

var fnCheckShortcut = function(e){
// set to true if event was handled by this script (thus it will cancel default behaviour)
var wasHandled = false;

var codeForS = 83;
var codeForE = 69;

if(e.ctrlKey && e.keyCode == codeForE){
// Simulate click on the ‘edit’ link
window.location.href = editLink;
wasHandled = true;
} else if(e.ctrlKey && e.keyCode == codeForS){

var saveEvent = document.createEvent(“MouseEvents”);
saveEvent.initMouseEvent(“click”, true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var saveButton = document.getElementById(“wpSave”);
// Simulate click on the ‘save’ button
saveButton.dispatchEvent(saveEvent);
wasHandled = true;
}

// prevent firefox from lauching default actions for shortcut (ie the “save page” dialog will not  be shown for CTRL+s
if ( wasHandled ) {
//alert(‘wasHandled’);
e.stopPropagation();
e.preventDefault();
}
}

document.addEventListener(“keydown”,fnCheckShortcut,1);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s