XQuery by default

Volume 19, Issue 12; 27 May 2016

Using TamperMonkey to “fix” QConsole.

This posting is only likely to be of interest if you use MarkLogic. I use MarkLogic all the time.

Long ago, when QConsole was first introduced, it was an editing environment for XQuery. In more recent times, support for JavaScript, SPARQL, and SQL has been added. This is great, and QConsole gets better and better in every release. Except for this one thing: it defaults to JavaScript mode and, whilst I’m delighted that the server supports server-side JavaScript, it’s not my everyday bag.

There’s no way to define an initial default language, which is ok. If you change the language, that change persists across sessions; most folks will probably only ever have to change it once. The thing is, I often rebuild the server and start over with a clean install, so I get the default over and over again.

Until today. Up at 4am due to jet lag, I hacked up this TamperMonkey script to automatically switch to XQuery mode:

// ==UserScript==
// @name         Switch to XQuery mode
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Effectively, make XQuery the default in QConsole
// @author       Norman Walsh
// @match        http://localhost:8000/qconsole/
// ==/UserScript==

(function() {
    'use strict';

function frobMode() {
    var zwspace ="\u200B";
    var lines = unsafeWindow.jQuery("#query-text-space div.CodeMirror-lines div.CodeMirror-code > div pre");
    var defContent = (lines.length === 3) && (lines[0].innerText === "'use strict'") && (lines[1].innerText === zwspace) && (lines[2].innerText === zwspace);
    if (defContent) {
        unsafeWindow.jQuery("#mode-selection select").val("xquery");
        unsafeWindow.jQuery("#mode-selection select").change();

It only switches the mode if you’re on the initial, default page. Even so, it’s a bit crude. In particular, waiting for one second is a hack. The actual QConsole page is populated by AJAX calls to the server. The recommended way to deal with this situation appears to be waitForKeyElements, but I couldn’t find an expression that worked reliably.

It works for me and I’m probably the only person who will ever use it, and I’ve been up since 4am, so I’m calling it done.