User:喵/refresh.js
外观
< User:喵
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
Wikipedia Auto Refresh
Copyright (C) 2012 Shyc2001 <http://zh.wikipedia.org/wiki/User:Shyc2001>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$(document).ready(function() {
var STORAGE_MODE = 'autoRefresh_mode_';
var STORAGE_INTERVAL = 'autoRefresh_interval_';
var DEFAULT_INTERVAL = 120000;
var TIMEOUT_ID_ATTR = 'autoRefresh_timeoutID';
var i18n = {
'en': {
buttonOn: 'Auto Refresh [ON]',
buttonOff: 'Auto Refresh [OFF]',
turnOn: 'Enable auto-refresh for this page?',
turnOff: 'Disable auto-refresh for this page?',
promptInterval: 'Please enter the refresh interval (in ms):',
specialRecentChanges: 'Special:RecentChanges',
},
'zh-cn': {
buttonOn: '[√]自动刷新',
buttonOff: '[ ]自动刷新',
turnOn: '在此页面上启用自动刷新吗?',
turnOff: '在此页面上禁用自动刷新吗?',
promptInterval: '请输入刷新频率(单位:毫秒):',
specialRecentChanges: 'Special:最近更改',
},
'zh-tw': {
buttonOn: '[√]自動刷新',
buttonOff: '[ ]自動刷新',
turnOn: '在此頁面上啟用自動刷新嗎?',
turnOff: '在此頁面上禁用自動刷新嗎?',
promptInterval: '請輸入刷新頻率(單位:毫秒):',
specialRecentChanges: 'Special:最近更改',
},
};
i18n[''] = i18n['en'];
i18n['zh'] = i18n['zh-cn'];
var langCode = $('html').attr('lang') || navigator.language.toLowerCase() || '';
var lang = i18n[langCode];
if(!lang) {
langCode = langCode.substring(0, langCode.indexOf('-'));
lang = i18n[langCode];
if(!lang) {
lang = i18n[''];
}
}
var AutoRefreshMode = {
jumpToH4: 'jumpToH4'
};
var autoRefreshPages = { };
autoRefreshPages[lang.specialRecentChanges] = 'jumpToH4';
var pageName = mw.config.get('wgPageName');
if (autoRefreshPages[pageName]) {
var storageKey = STORAGE_MODE + pageName;
var doAutoRefresh = function() {
autoRefreshButton.text(localStorage.getItem(storageKey) ? lang.buttonOn : lang.buttonOff);
var timeoutID = $('html').attr(TIMEOUT_ID_ATTR);
if(timeoutID) {
window.clearTimeout(timeoutID);
}
var storageItem = localStorage.getItem(storageKey);
if (storageItem) {
switch (storageItem) {
case AutoRefreshMode.jumpToH4:
var subTitle = $('h4')[0];
if(subTitle) {
subTitle.scrollIntoView();
}
break;
}
var interval = localStorage.getItem(STORAGE_INTERVAL + pageName) || DEFAULT_INTERVAL;
timeoutID = window.setTimeout(function() { history.go(0); }, interval);
$('html').attr(TIMEOUT_ID_ATTR, timeoutID);
}
};
var autoRefreshButton = $('<button/>');
autoRefreshButton.click(function() {
if(localStorage.getItem(storageKey)) {
if(confirm(lang.turnOff)) {
localStorage.removeItem(storageKey);
doAutoRefresh();
}
} else {
if(confirm(lang.turnOn)) {
localStorage.setItem(storageKey, AutoRefreshMode.jumpToH4);
var interval = prompt(lang.promptInterval, DEFAULT_INTERVAL) || DEFAULT_INTERVAL;
localStorage.setItem(STORAGE_INTERVAL + pageName, interval);
doAutoRefresh();
}
}
});
autoRefreshButton.appendTo('.firstHeading');
doAutoRefresh();
}
});