LLWiki正在建设中,欢迎加入我们

“MediaWiki:Gadget-UTCLiveClock.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
标签移动版网页编辑 移动版编辑
(// Edit via Wikiplus)
第13行: 第13行:


function padWithZeroes( num ) {
function padWithZeroes( num ) {
// Pad a number with zeroes. The number must be an integer where
// Pad a number with zeroes. The number must be an integer where
// 0 <= num < 100.
// 0 <= num < 100.
return num < 10 ? '0' + num.toString() : num.toString();
return num < 10 ? '0' + num.toString() : num.toString();
}
}


function showTime( $target ) {
function showTime( $target ) {
var now = new Date();
var now = new Date();
var timezone = window.LiveClockTimeZone || 'local';
var timezone = window.LiveClockTimeZone || 'local';


// Set the time.
// Set the time.
var hh, mm, ss;
var hh, mm, ss;
if ( timezone === "UTC" ) {
if ( timezone === "UTC" ) {
hh = now.getUTCHours();
hh = now.getUTCHours();
mm = now.getUTCMinutes();
mm = now.getUTCMinutes();
ss = now.getUTCSeconds();
ss = now.getUTCSeconds();
} else if ( timezone === "local" ) {
} else if ( timezone === "local" ) {
hh = now.getHours();
hh = now.getHours();
mm = now.getMinutes();
mm = now.getMinutes();
ss = now.getSeconds();
ss = now.getSeconds();
} else {
} else {
var newNow;
var newNow;
try {
try {
newNow = new Date(
newNow = new Date(
now.toLocaleString(
now.toLocaleString(
"en-US",
"en-US",
{ timeZone: timezone }
{ timeZone: timezone }
)
)
);
);
hh = newNow.getHours();
hh = newNow.getHours();
mm = newNow.getMinutes();
mm = newNow.getMinutes();
ss = newNow.getSeconds();
ss = newNow.getSeconds();
} catch ( err ) {
} catch ( err ) {
console.log( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name);
console.log( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name);
timezone = "UTC";
timezone = "UTC";
newNow = now;
newNow = now;
hh = now.getUTCHours();
hh = now.getUTCHours();
mm = now.getUTCMinutes();
mm = now.getUTCMinutes();
ss = now.getUTCSeconds();
ss = now.getUTCSeconds();
}
}
}
}
var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss );
var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss );
$target.text( time );
$target.text( time );


// Schedule the next time change.
// Schedule the next time change.
//
//
// We schedule the change for 100 ms _after_ the next clock tick. The delay
// We schedule the change for 100 ms _after_ the next clock tick. The delay
// from setTimeout is not precise, and if we aim exactly for the tick, there
// from setTimeout is not precise, and if we aim exactly for the tick, there
// is a chance that the function will run slightly before it. If this
// is a chance that the function will run slightly before it. If this
// happens, we will display the same time for two seconds in a row - not
// happens, we will display the same time for two seconds in a row - not
// good. By scheduling 100 ms after the tick, we will always be about 100 ms
// good. By scheduling 100 ms after the tick, we will always be about 100 ms
// late, but we are also very likely to display a new time every second.
// late, but we are also very likely to display a new time every second.
var ms = now.getUTCMilliseconds();
var ms = now.getUTCMilliseconds();
setTimeout( function () {
setTimeout( function () {
showTime( $target );
showTime( $target );
}, 1100 - ms );
}, 1100 - ms );
}
}


function liveClock() {
function liveClock() {
// Set CSS styles. We do this here instead of on the CSS page because some
// Set CSS styles. We do this here instead of on the CSS page because some
// wikis load this page directly, without loading the accompanying CSS.
// wikis load this page directly, without loading the accompanying CSS.
mw.util.addCSS( '#utcdate a { font-weight:bolder; }' );
mw.util.addCSS( '#utcdate a { font-weight:bolder; }' );


// Reset whitespace that was set in the peer CSS gadget; this prevents the
// Reset whitespace that was set in the peer CSS gadget; this prevents the
// effect of the p-personal menu jumping to the left when the JavaScript
// effect of the p-personal menu jumping to the left when the JavaScript
// loads.
// loads.
$( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' );
$( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' );
$( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );
$( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );


// Add the portlet link.
// Add the portlet link.
var node = mw.util.addPortletLink(
var node = mw.util.addPortletLink(
'p-personal',
'p-personal',
mw.util.getUrl( null, { action: 'purge' } ),
mw.util.getUrl( null, { action: 'purge' } ),
'',
'',
'utcdate'
'utcdate'
);
);
if ( !node ) {
if ( !node ) {
node = $('<div><a href=' + mw.util.getUrl( null, { action: 'purge' } ) + '></a></div>').insertAfter('.minerva-user-notifications');
node = $('<div><a href=' + mw.util.getUrl( null, { action: 'purge' } ) + '></a></div>').insertAfter('.minerva-user-notifications');
}
}


// Purge the page when the clock is clicked. We have to do this through the
// Purge the page when the clock is clicked. We have to do this through the
// API, as purge URLs now make people click through a confirmation screen.
// API, as purge URLs now make people click through a confirmation screen.
$( node ).on( 'click', function ( e ) {
$( node ).on( 'click', function ( e ) {
new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
location.reload();
location.reload();
}, function () {
}, function () {
mw.notify( 'Purge failed', { type: 'error' } );
mw.notify( 'Purge failed', { type: 'error' } );
} );
} );
e.preventDefault();
e.preventDefault();
} );
} );


// Show the clock.
// Show the clock.
showTime( $( node ).find( 'a:first' ) );
showTime( $( node ).find( 'a:first' ) );
}
}



2020年8月13日 (四) 04:38的版本

//<nowiki>
/**
 * This gadget adds a clock in the personal toolbar that shows the current time
 * in UTC (or a different timezone of your choosing), and also provides a link
 * to purge the current page.
 *
 * Revision: July 2020
 * Source: https://www.mediawiki.wikimirror.org/wiki/MediaWiki:Gadget-UTCLiveClock.js (失效网址)
 * 
 * To set the timezone used to one other than UTC, set window.LiveClockTimeZone to
 * the desired timezone.
 */

function padWithZeroes( num ) {
    // Pad a number with zeroes. The number must be an integer where
    // 0 <= num < 100.
    return num < 10 ? '0' + num.toString() : num.toString(); 
}

function showTime( $target ) {
    var now = new Date();
    
    var timezone = window.LiveClockTimeZone || 'local';

    // Set the time.
    var hh, mm, ss;
    if ( timezone === "UTC" ) {
        hh = now.getUTCHours();
        mm = now.getUTCMinutes();
        ss = now.getUTCSeconds();
    } else if ( timezone === "local" ) {
        hh = now.getHours();
        mm = now.getMinutes();
        ss = now.getSeconds();
    } else {
        var newNow;
        try {
            newNow = new Date(
                now.toLocaleString(
                    "en-US",
                    { timeZone: timezone }
                )
            );
            hh = newNow.getHours();
            mm = newNow.getMinutes();
            ss = newNow.getSeconds();
        } catch ( err ) {
            console.log( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name);
            timezone = "UTC";
            newNow = now;
            hh = now.getUTCHours();
            mm = now.getUTCMinutes();
            ss = now.getUTCSeconds();
        }
    }
    var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss );
    $target.text( time );

    // Schedule the next time change.
    // 
    // We schedule the change for 100 ms _after_ the next clock tick. The delay
    // from setTimeout is not precise, and if we aim exactly for the tick, there
    // is a chance that the function will run slightly before it. If this
    // happens, we will display the same time for two seconds in a row - not
    // good. By scheduling 100 ms after the tick, we will always be about 100 ms
    // late, but we are also very likely to display a new time every second.
    var ms = now.getUTCMilliseconds();
    setTimeout( function () {
        showTime( $target );
    }, 1100 - ms );
}

function liveClock() {
    // Set CSS styles. We do this here instead of on the CSS page because some
    // wikis load this page directly, without loading the accompanying CSS.
    mw.util.addCSS( '#utcdate a { font-weight:bolder; }' );

    // Reset whitespace that was set in the peer CSS gadget; this prevents the
    // effect of the p-personal menu jumping to the left when the JavaScript
    // loads.
    $( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' );
    $( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );

    // Add the portlet link.
    var node = mw.util.addPortletLink(
        'p-personal',
        mw.util.getUrl( null, { action: 'purge' } ),
        '',
        'utcdate'
    );
    if ( !node ) {
        node = $('<div><a href=' + mw.util.getUrl( null, { action: 'purge' } ) + '></a></div>').insertAfter('.minerva-user-notifications');
    }

    // Purge the page when the clock is clicked. We have to do this through the
    // API, as purge URLs now make people click through a confirmation screen.
    $( node ).on( 'click', function ( e ) {
        new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
            location.reload();
        }, function () {
            mw.notify( 'Purge failed', { type: 'error' } );
        } );
        e.preventDefault();
    } );

    // Show the clock.
    showTime( $( node ).find( 'a:first' ) );
}

$( liveClock );
//</nowiki>