User:Quarl/advanced sig.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

// <pre><nowiki>

// [[User:Quarl/advanced_sig.js]] - allows advanced custom signatures.
//
//   - "~~~~" is replaced by anything you want (any string or function) when you hit 'submit'
//   - Defaults to nicely-formatted and [[ISO 8061]] timestamp
//       - Wiki-linked timestamp enables date preferences
//       - Uses <span class="user-Username"> tags around entire signature (including timestamp) - see [[User:HorsePunchKid]]
//   - the toolbar "--~~~~" button is replaced by anything you want
//
//   - You could even use this to vary your signature based on the article or namespace
//     (for example an extra link on AFD pages)

// Depends: datetime.js, autoreplace.js, wikipage.js

///////////////////////////////////////////////////////////////
// EXTERNAL ENTRY POINTS
//
// makeSignature():     Returns the custom signature.
//

///////////////////////////////////////////////////////////////
// USER-CONFIGURABLE SETTINGS

// signature:           This is your signature. (can be string or function)
//
//                      To emulate normal behavior:
//                          signature = '~~~~';
//
//                      Default:
//                          signature = build_default_signature;
//
//                      Other examples:
//                          signature = function() { return '--~~~ ' + wikiTimestampISO() }
//                          signature = '<i>&mdash;[[User:Quarl|Quarl]] ~~~~~</i>';
//
//
// toolbar_signature:   What the "signature" toolbar button inserts. (can be string or function)
//                      I recommend removing the '--' (or &mdash;) and having it instead be part of the signature.
//
//                      To emulate normal behavior:
//                          toolbar_signature = '--~~~~';
//
//                      Default:
//                          toolbar_signature = '~~~~';
//
//                      Other examples:
//                          toolbar_signature = makeSignature;
//                          toolbar_signature = '&mdash;~~~~';
//

//
// The following are used by build_default_signature.
//
// sig_username:        Used by build_default_signature as username in CSS span tag class name.
//                      Result is <span user-Quarl> for sig_user='Quarl'.  (can be string or function)
//
//                      Default (should work for most people):
//                          sig_username = wikiDoc.username; // read from Wikipedia page
//
//                      Other examples:
//                          sig_username = 'QuarlXYZ';
//
// signature_user:      Used by build_default_signature for username wiki link. (can be string or function)
//                      I recommend embedding the &mdash; so you don't have to type it every time.
//
//                      To emulate normal behavior:
//                          signature_user = '~~~';
//
//                      Default:
//                          signature_user = '&mdash;~~~';
//
//                      Other examples:
//                          signature_user = '[[User:Quarl|Quarl]] <sup>([[User talk:quarl|talk]])</sup>';
//
// signature_timestamp: Used by build_default_signature for timestamp (can be string or function).
//                      I recommend using wikiTimestampISO since I love [[ISO 8601]] time format, and
//                      this would allow everyone to see their preferred time format.
//
//                      To emulate normal behavior:
//                          signature_timestamp = '~~~~~';
//
//                      Default:
//                          signature_timestamp = wikiTimestampISO;
//

// To allow use of advanced_sig.js in other scripts but not require it, use the following:
//      if(typeof window.makeSignature=='undefined')makeSignature=function(){return "~~~~"};

// quarl 2005-12-30 initial version
// quarl 2006-01-04 add feature to replace "~~~~" in text; factored it so it's easy to customize

// If this script looks simple, it's because all the hard stuff has been factored out to the library :)

function getUsername() { return wikiDoc.username; }

function wikiTimestampISO() {
    return "[["+datestampUTCISO()+"]]&nbsp;"+timestampUTCISO()+"[[ISO 8601|Z]]";
}

function spansig(user, sigUser, sigTimestamp) {
    return "<span class=\"user-sig user-"+user+"\"><i>" + sigUser +  " <small>"+sigTimestamp+"</small></i></span>"
}

function build_default_signature() {
    return spansig(getSigUsername(), getSignatureUser(), getSignatureTimestamp());
}

// sig_newOption, originally based on newOption from Lupin's popups.js
//     this should expand to something like
//     if (typeof window.foo=='undefined') { window.foo=null; }; window.dfoo = 0.5;
function sig_newOption(x, def) {
    var a='window.'  + x;
    var b='window.default_' + x;
    eval('if (typeof '+ a + '==\'undefined\') {' + a + '=null; }; ' + b + '=' + def + ';');
    return sig_makeGetterFunction(x);
};

function sig_makeGetterFunction(varName) {
    return eval(
             'function() { '+
                 'return sig_evalParam("'+varName+'",'+
                                     '(window.'+varName+' || window.default_'+varName+')); }');
}

function sig_evalParam(varName, v) {
    if (typeof v == 'string') return v;
    if (typeof v == 'function') return v();
    if (v == null) alert("ERROR! Variable '"+varName+"' cannot be null.");
    else alert("ERROR! Variable '"+varName+"' cannot be of type '"+(typeof v)+"'; must be string or function.");
    return ""+v;
}

///////////////////////////////////////////////////////////////
// user-configurable settings (see top of page for commentary)

makeSignature = sig_newOption('signature', 'build_default_signature');

getSigUsername        = sig_newOption('sig_username', 'getUsername');
getSignatureUser      = sig_newOption('signature_user', '"&mdash;~~~"');
getSignatureTimestamp = sig_newOption('signature_timestamp', 'wikiTimestampISO');
getToolbarSignature   = sig_newOption('toolbar_signature', '"~~~~"');

function replaceSignatureButton() {
    // Note: a slightly more complicated version is required to replace the signature with the evaluation of getToolbarSignature() at the time of pressing the button rather than time of page load, but it doesn't matter if you are just replacing it with ~~~~ and using autoreplace as below.
    var toolbar = document.getElementById('toolbar');
    if (!toolbar) return;
    var links = toolbar.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        links[i].href = links[i].href.replace(/--~~~~/, getToolbarSignature());
    }
}

function replaceSignatureOnSubmit() {
    autoreplace.addReplacement('~~~~', makeSignature);
}

addOnloadHook(replaceSignatureButton);
addOnloadHook(replaceSignatureOnSubmit);

// </nowiki></pre>