コンテンツにスキップ

利用者:Yuukin0248/ChangeTimeoffset.js

お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。

多くの WindowsLinux のブラウザ

  • Ctrl を押しながら F5 を押す。

Mac における Safari

  • Shift を押しながら、更新ボタン をクリックする。

Mac における ChromeFirefox

  • Cmd Shift を押しながら R を押す。

詳細についてはWikipedia:キャッシュを消すをご覧ください。

/**
  [[User:Waiesu]] さん作成の [[User:Waiesu/Clock.js]] を改変したもの

  ライセンス: クリエイティブ・コモンズ 表示-継承 3.0 非移植([[Wikipedia:Text of Creative Commons Attribution-ShareAlike 3.0 Unported License]])およびGFDL([[Wikipedia:Text of GNU Free Documentation License]])のライセンスのもと使用可能
 */

mw.loader.using('mediawiki.api').then(function () {
  function utcClock() {
    const objDate = new Date();
    const hNumber = objDate.getUTCHours();
    const mNumber = objDate.getUTCMinutes();
    const sNumber = objDate.getUTCSeconds();
    const fff = objDate.getMilliseconds();
    const hh = hNumber < 10 ? '<span style="visibility:hidden">0</span>' + hNumber : hNumber;
    const mm = mNumber < 10 ? ':0' + mNumber : ':' + mNumber;
    const ss = sNumber < 10 ? ':0' + sNumber : ':' + sNumber;
    span.html(hh + mm + ss);
    timer = setTimeout(utcClock, 1000 - fff);
  }

  function localClock() {
    const objDate = new Date();
    const hNumber = objDate.getHours();
    const mNumber = objDate.getMinutes();
    const sNumber = objDate.getSeconds();
    const fff = objDate.getMilliseconds();
    const hh = hNumber < 10 ? '<span style="visibility:hidden">0</span>' + hNumber : hNumber;
    const mm = mNumber < 10 ? ':0' + mNumber : ':' + mNumber;
    const ss = sNumber < 10 ? ':0' + sNumber : ':' + sNumber;
    span.html(hh + mm + ss);
    timer = setTimeout(localClock, 1000 - fff);
  }

  function toUTC() {
    func = utcClock;
    small.html('(UTC)<span style="visibility:hidden">+0</span>');
  }

  function toLocal() {
    func = localClock;
    const timezone = new Date().getTimezoneOffset() / -60;
    if (timezone < 0) {
      small.text('(UTC' + timezone + ')');
    } else {
      small.text('(UTC+' + timezone + ')');
    }
  }

  const api = new mw.Api();

  const type = localStorage.jawpClockType || 'utcClock'; //初期設定
  let func;
  let timer;

  // clock
  const span = $('<span></span>'); //数字部分
  const small = $('<small style="margin-left: 0.25em"></small>'); //タイムゾーン部分
  const clock = $('<li></li>')
    .attr({
      id: 'pt-clock',
      title: 'クリック: UTC/ローカル切替  ダブルクリック: キャッシュ破棄',
      style: 'cursor: pointer;',
    })
    .append(span, small)
    .insertBefore('#pt-logout');

  if (type === 'utcClock') {
    toUTC();
  } else {
    toLocal();
  }

  func();

  clock.on({
    click: function () {
      clearInterval(timer);

      // ローカルへ
      if (func == utcClock) {
        toLocal();
        localStorage.jawpClockType = 'localClock';

        api.postWithToken('csrf', {
          action: 'options',
          optionname: 'timecorrection',
          optionvalue: 'ZoneInfo|60|Asia/Tokyo',
          format: 'json',
        });
      }

      // UTCへ
      else {
        toUTC();
        localStorage.jawpClockType = 'utcClock';

        api.postWithToken('csrf', {
          action: 'options',
          change: 'timecorrection',
          format: 'json',
        });
      }

      func();
    },
    dblclick: function () {
      api
        .post({
          action: 'purge',
          titles: mw.config.get('wgPageName'),
        })
        .then(
          function () {
            location.reload();
          },
          function () {
            mw.notify('キャッシュを破棄できませんでした');
          }
        );
    },
  });
});