FlightGear wiki:Instant-Refs

From FlightGear wiki
Revision as of 00:32, 26 May 2013 by Hooray (Talk | contribs) (JavaScript is almost Nasal, right ... no more unattributed quoting please :-))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
// ==UserScript==
// @name       auto cquotes for mail-archive.com
// @namespace  http://wiki.flightgear.org/
// @version    0.1
// @description automatically create proper wikimedia cquotes for mail-archive.com text selections
// @match      http://www.mail-archive.com/*
// @copyright  2013+, FlightGear.org
// ==/UserScript==

var CONFIG = {
    // domain for the following profile:
    "www.mail-archive.com": { 
        // xpath expressions to extract fields required for the cquote from the document:
   author: "/html/body/div/div[2]/div/p/span[1]/a[1]/text()",
   title: "/html/body/div/div[2]/div/h1/span/a/text()",
   date: "/html/body/div/div[2]/div/p/span[2]/a[1]/text()"
    }
};

function extractInfo(expr) { 
 return document.evaluate( expr, document, null, XPathResult.STRING_TYPE, null ).stringValue; 
}

function CreateCquote(message) {
 var profile = CONFIG[window.location.hostname];
 var url = document.URL;
 var title = extractInfo( profile.title );
 var author = extractInfo( profile.author );
 var date = extractInfo( profile.date );
 //TODO: add template string to profile and make configurable through regex: $author, $title, $url, $date
 var template = "{{cquote|"+message+"<ref>{{cite web |url="+url+"|title="+title+"|author="+author+"|date="+date+"}}</ref>|"+author+"}}";
 return template;
}

// check if there's any selected text
function GetSelectedText () {    
    var text = "";
    
    if (document.getSelection) {    
        var sel = document.getSelection ();        
        text = sel;
    }
    else {
        if (document.selection) {   
            var textRange = document.selection.createRange ();
            text = textRange.text;
        }
        
    }
    
    // check if we got text
    if ( text != "" ) {
        window.prompt ("Copy to clipboard: Ctrl+C, Enter", CreateCquote( text ) );    // create the cquote and show it
    }
    
}

function register() {
    // check if we have a matching domain in the CONFIG hash
    if (CONFIG.hasOwnProperty(window.location.hostname) ) {        
        // if yes, register a callback to handle mouse events
        document.onmouseup = GetSelectedText;
    }        
}

window.addEventListener('load', register );