FlightGear wiki:Instant-Refs: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(JavaScript is almost Nasal, right ... no more unattributed quoting please :-))
 
mNo edit summary
Line 1: Line 1:
* A simple userscript extension implemented in JavaScript
* A simple userscript extension implemented in JavaScript
* Supported by FireFox, Chrome, Chromium and probably Opera and others
* Supported by FireFox, Chrome, Chromium, probably Opera and others
* in case of doubt, just install the GreaseMonkey/TamperMonkey extension
* in case of doubt, just install the GreaseMonkey/TamperMonkey extension
* install the script
* install the script
* go to some mail-archive.com URL, for example: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg40124.html
* go to some mail-archive.com URL, for example: http://www.mail-archive.com/flightgear-devel@lists.sourceforge.net/msg40124.html
* copy some relevant portion of text
* copy/mark some relevant portion of text
* and directly get a full cquote paragraph that you can add to the wiki here
* and directly get a full cquote paragraph that you can add to the wiki here
* [http://wiki.flightgear.org/User_talk:Hooray stop whining that proper cquotes are too painful] :-)
* [http://wiki.flightgear.org/User_talk:Hooray stop whining that proper cquotes are too painful] :-)

Revision as of 05:37, 26 May 2013

// ==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 );