LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-UTCLiveClock.js”的版本间差异
跳转到导航
跳转到搜索
小 (导入1个版本) |
小 (new personal menu) 标签:移动版网页编辑 移动版编辑 |
||
(未显示3个用户的29个中间版本) | |||
第1行: | 第1行: | ||
//<nowiki> |
|||
mw.loader.load('//www.mediawiki.wikimirror.org/w/index.php?title=MediaWiki:Gadget-UTCLiveClock.js&action=raw&ctype=text/javascript'); |
|||
/** |
|||
* 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.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(), |
|||
timezone = window.LiveClockTimeZone || 'local', |
|||
// Set the time. |
|||
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.error( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name); |
|||
hh = now.getHours(); |
|||
mm = now.getMinutes(); |
|||
ss = now.getSeconds(); |
|||
} |
|||
} |
|||
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() { |
|||
// 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. |
|||
$( '.skin-vector #p-personal ul' ).css( 'margin-right', 0 ); |
|||
// Add the portlet link. |
|||
var node; |
|||
if(mw.config.get('skin') == 'vector' && window.LiveClockMode != 'mobile') { |
|||
node = mw.util.addPortletLink( |
|||
'p-personal', |
|||
mw.util.getUrl( null, { action: 'purge' } ), |
|||
'', |
|||
'utcdate' |
|||
); |
|||
} else if ( mw.config.get('skin') == 'minerva' && window.LiveClockMode != 'desktop' ) { |
|||
node = $('<div id="utcdate"><a href="#"></a></div>').insertAfter('.minerva-user-menu'); |
|||
} |
|||
// 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. |
|||
if(!node) { return; } |
|||
$( node ).on( 'click', function ( e ) { |
|||
if( mw.config.get('wgIsArticle') ) { |
|||
new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () { |
|||
location.reload(); |
|||
}, function () { |
|||
mw.notify( wgULS('清除缓存失败!', '清除快取失敗!'), { type: 'error' } ); |
|||
} ); |
|||
} |
|||
else { |
|||
mw.notify( wgULS('当前页面不是内容页面', '當前頁面不是內容頁面'), { type: 'error' } ); |
|||
} |
|||
e.preventDefault(); |
|||
} ); |
|||
// Show the clock. |
|||
showTime( $( node ).children( 'a' ) ); |
|||
} |
|||
$( liveClock ); |
|||
//</nowiki> |
2023年2月26日 (日) 17:14的最新版本
//<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.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(), timezone = window.LiveClockTimeZone || 'local', // Set the time. 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.error( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name); hh = now.getHours(); mm = now.getMinutes(); ss = now.getSeconds(); } } 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() { // 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. $( '.skin-vector #p-personal ul' ).css( 'margin-right', 0 ); // Add the portlet link. var node; if(mw.config.get('skin') == 'vector' && window.LiveClockMode != 'mobile') { node = mw.util.addPortletLink( 'p-personal', mw.util.getUrl( null, { action: 'purge' } ), '', 'utcdate' ); } else if ( mw.config.get('skin') == 'minerva' && window.LiveClockMode != 'desktop' ) { node = $('<div id="utcdate"><a href="#"></a></div>').insertAfter('.minerva-user-menu'); } // 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. if(!node) { return; } $( node ).on( 'click', function ( e ) { if( mw.config.get('wgIsArticle') ) { new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () { location.reload(); }, function () { mw.notify( wgULS('清除缓存失败!', '清除快取失敗!'), { type: 'error' } ); } ); } else { mw.notify( wgULS('当前页面不是内容页面', '當前頁面不是內容頁面'), { type: 'error' } ); } e.preventDefault(); } ); // Show the clock. showTime( $( node ).children( 'a' ) ); } $( liveClock ); //</nowiki>