![刷新 刷新](/images/thumb/b/b0/OOjs_UI_icon_reload-progressive.svg/55px-OOjs_UI_icon_reload-progressive.svg.png)
请注意:更新本页面后,您可能需要清除浏览器缓存才能看到所作变更的影响。
Google Chrome、Microsoft Edge、Mozilla Firefox、Safari:按住⇧ Shift,同时单击“刷新”。
Google Chrome、Microsoft Edge、Mozilla Firefox、Safari:按住⇧ Shift,同时单击“刷新”。
![Cc.logo.circle.svg](/images/thumb/5/5d/CC_Logo.svg/40px-CC_Logo.svg.png)
![署名 署名](/images/thumb/1/11/Cc-by_new_white.svg/24px-Cc-by_new_white.svg.png)
![相同方式共享 相同方式共享](/images/thumb/d/df/Cc-sa_white.svg/24px-Cc-sa_white.svg.png)
本文件采用知识共享署名-相同方式共享 4.0 国际(CC BY-SA 4.0)许可协议授权。
- 您可以自由地:
- 分享 – 在任何媒介以任何形式复制、发行本作品
- 演绎 – 修改、转换或以本作品为基础进行创作
- 惟须遵守下列条件:
- 署名 – 您必须给出适当的署名(标明原作者),提供指向本许可协议的链接,同时标明是否(对原始作品)作了修改。您可以用任何合理的方式来署名,但是不得以任何方式暗示许可人为您或您的使用背书(即诱导原作者在不了解协议情况下给你授权)。
- 相同方式共享 – 如果您再混合、转换或者基于本作品进行创作,您必须基于与原先许可协议相同或相兼容的许可协议分发您贡献的作品。
/**
* SPDX-License-Identifier: CC BY-SA-4.0
* _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}'
*
* @source <zh.wikipedia.org/wiki/MediaWiki:Gadget-UTCLiveClock.js>
* @dependency ext.gadget.SiteCommonJS, ext.gadget.morebits, mediawiki.api, oojs-ui
*/
"use strict";
/**
* Warning! Global gadget file!
*
* 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
*
* Installation:
*
* 1. Copy the JS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js
* to the page [[MediaWiki:Gadget-UTCLiveClock.js]] on your wiki.
*
* 2. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.css
* to the page [[MediaWiki:Gadget-UTCLiveClock.css]] on your wiki.
*
* 3. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock-pagestyles.css
* to the page [[MediaWiki:Gadget-UTCLiveClock-pagestyles.css]] on your wiki.
*
* 4. Add a description of the gadget to the page [[MediaWiki:Gadget-UTCLiveClock]]
* on your wiki. You can use https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock
* as a template.
*
* 5. Add the following code to your wiki's [[MediaWiki:Gadgets-definition]]:
*
* UTCLiveClock[ResourceLoader|type=general|dependencies=mediawiki.util,mediawiki.api|peers=UTCLiveClock-pagestyles]|UTCLiveClock.js|UTCLiveClock.css
* UTCLiveClock-pagestyles[hidden|skins=vector]|UTCLiveClock-pagestyles.css
*
*
* To set the timezone used to one other than UTC, set window.LiveClockTimeZone to
* the desired timezone. For example, adding the following to your common.js
* window.LiveClockTimeZone = 'America/Los_Angeles';
* would result in the local time in Los Angeles being shown. See
* TZ database for valid options.
*/
mw.loader.using(["mediawiki.util", "mediawiki.api"]).then(function () {
var padWithZeroes = function padWithZeroes(num) {
// Pad a number with zeroes. The number must be an integer where
// 0 <= num < 100.
return num < 10 ? "0".concat(num.toString()) : num.toString();
};
var showTime = function showTime($target) {
var now = new Date();
var timezone = window.LiveClockTimeZone || "Asia/Shanghai";
// 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 '".concat(timezone, "': ").concat(err.name));
timezone = "UTC";
newNow = now;
hh = now.getUTCHours();
mm = now.getUTCMinutes();
ss = now.getUTCSeconds();
}
}
var time = "".concat(padWithZeroes(hh), ":").concat(padWithZeroes(mm), ":").concat(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);
};
var liveClock = 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;font-size:120%}");
// 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");
// Add the portlet link.
var node = mw.util.addPortletLink("p-personal", mw.util.getUrl(null, {
action: "purge"
}), "", "utcdate");
if (!node) {
return;
}
// 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({
ajax: {
headers: {
"Api-User-Agent": "Qiuwen/1.1 (UTCLiveClock/1.1; ".concat(mw.config.get("wgWikiID"), ")")
}
}
}).post({
action: "purge",
titles: mw.config.get("wgPageName")
}).then(function () {
location.reload();
}, function () {
mw.notify("清除缓存失败", {
type: "error"
});
});
e.preventDefault();
});
// Show the clock.
showTime($(node).find("a:first"));
};
$(liveClock);
});