User:喵/refresh.js
外观
< User:喵
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
/*
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/>.
*/
(function () {
window.AutoRefresh = window.AutoRefresh || {};
var AutoRefresh = window.AutoRefresh;
//Utilities
AutoRefresh._addKeysIfNotPresent = function (set, keys) {
for(var i in keys) {
if (!set[i]) set[i] = keys[i];
}
};
AutoRefresh.doNothing = function () {};
//Internationalization
AutoRefresh.i18n = AutoRefresh.i18n || {};
AutoRefresh._addKeysIfNotPresent(AutoRefresh.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:最近更改',
},
});
//Set fallbacks
AutoRefresh._addKeysIfNotPresent(AutoRefresh.i18n, {
'zh': AutoRefresh.i18n['zh-cn'],
'': AutoRefresh.i18n['en'],
});
//Detect language
AutoRefresh.langCode = AutoRefresh.langCode || $('html').attr('lang') ||
navigator.language.toLowerCase() || '';
var lang = AutoRefresh.i18n[AutoRefresh.langCode];
if (!lang) {
AutoRefresh.langCode = AutoRefresh.langCode.substring(0, AutoRefresh.langCode.indexOf('-'));
lang = AutoRefresh.i18n[AutoRefresh.langCode];
if (!lang) {
lang = AutoRefresh.i18n[''];
}
}
//Refresh modes
AutoRefresh.Modes = AutoRefresh.Modes || {};
AutoRefresh.Modes.base = AutoRefresh.Modes.base || 'base';
AutoRefresh.Modes.jumpToH4 = AutoRefresh.Modes.jumpToH4 || 'jumpToH4';
//The pages where AutoRefresh is on. Default to Special:RecentChanges.
if (!AutoRefresh.pages) {
AutoRefresh.pages = {};
AutoRefresh.pages[lang.specialRecentChanges] = AutoRefresh.Modes.jumpToH4;
}
//Use MediaWiki JS API to get the current page name.
AutoRefresh.currentPage = AutoRefresh.currentPage || mw.config.get('wgPageName');
if (AutoRefresh.pages[AutoRefresh.currentPage]) {
//Consts
AutoRefresh.STORAGE_MODE = AutoRefresh.STORAGE_MODE || 'autoRefresh_mode_';
AutoRefresh.STORAGE_INTERVAL = AutoRefresh.STORAGE_INTERVAL || 'autoRefresh_interval_';
AutoRefresh.DEFAULT_INTERVAL = AutoRefresh.DEFAULT_INTERVAL || 120000;
AutoRefresh.DUMMY_PARAM = AutoRefresh.DUMMY_PARAM || '_reqid=';
AutoRefresh.storage = AutoRefresh.storage || localStorage;
AutoRefresh.getBypassCacheURL = AutoRefresh.getBypassCacheURL || function (url) {
if (url.indexOf('?') >= 0) {
var paramPos = url.indexOf('&' + AutoRefresh.DUMMY_PARAM);
if (paramPos < 0) {
paramPos = url.indexOf('?' + AutoRefresh.DUMMY_PARAM);
if (paramPos < 0) {
url += '&';
}
}
if (paramPos >= 0) {
url = url.substring(0, paramPos + 1);
}
} else {
url += '?';
}
return url + AutoRefresh.DUMMY_PARAM + new Date().getTime();
}
//Refresh mode handlers
AutoRefresh.modesHandlers = AutoRefresh.modesHandlers || {};
var defaultHandlers = {};
//The base handler also acts as a fallback if no hooks are defined in other modes.
var baseHandler = {};
baseHandler.settings = AutoRefresh.doNothing;
baseHandler.load = AutoRefresh.doNothing;
baseHandler.update = AutoRefresh.doNothing;
baseHandler.dispose = AutoRefresh.doNothing;
baseHandler.refresh = function() {
location.href = AutoRefresh.getBypassCacheURL(location.href);
};
defaultHandlers[AutoRefresh.Modes.base] = baseHandler;
defaultHandlers[AutoRefresh.Modes.jumpToH4] = {};
defaultHandlers[AutoRefresh.Modes.jumpToH4].load = function() {
var subTitle = $('h4')[0];
if (subTitle) {
subTitle.scrollIntoView();
}
};
//Add the default handlers to the set if not overridden.
AutoRefresh._addKeysIfNotPresent(AutoRefresh.modesHandlers, defaultHandlers);
//Add the hook funtions in the default handlers if not overridden.
for(var mode in defaultHandlers) {
AutoRefresh._addKeysIfNotPresent(AutoRefresh.modesHandlers[mode], defaultHandlers[mode]);
}
AutoRefresh.storageKey = AutoRefresh.storageKey || (AutoRefresh.STORAGE_MODE + AutoRefresh.currentPage);
//Update everything including the timer according to the settings.
AutoRefresh.updateState = AutoRefresh.updateState || function(mode) {
AutoRefresh.button.text(mode ? lang.buttonOn : lang.buttonOff);
//If a timer has been started, let's stop it.
if (AutoRefresh._timeoutID) {
window.clearTimeout(AutoRefresh._timeoutID);
}
if (mode) {
var handler = AutoRefresh.modesHandlers[mode] || baseHandler;
if (!handler.isLoaded) {
(handler.load || baseHandler.load)();
handler.isLoaded = true;
}
(handler.update || baseHandler.update)();
var interval = AutoRefresh.storage.getItem(AutoRefresh.STORAGE_INTERVAL + AutoRefresh.currentPage) || AutoRefresh.DEFAULT_INTERVAL;
AutoRefresh._timeoutID = window.setTimeout(handler.refresh || baseHandler.refresh, interval);
}
};
AutoRefresh.showSettings = AutoRefresh.showSettings || function() {
var mode = AutoRefresh.storage.getItem(AutoRefresh.storageKey);
var handler = AutoRefresh.modesHandlers[mode] || baseHandler;
if (mode) {
if (confirm(lang.turnOff)) {
AutoRefresh.storage.removeItem(AutoRefresh.storageKey);
AutoRefresh.storage.removeItem(AutoRefresh.STORAGE_INTERVAL + AutoRefresh.currentPage);
(handler.dispose || baseHandler.dispose)();
handler.isLoaded = false;
AutoRefresh.updateState(null);
}
} else {
if (confirm(lang.turnOn)) {
mode = AutoRefresh.pages[AutoRefresh.currentPage];
AutoRefresh.storage.setItem(AutoRefresh.storageKey, mode);
handler = AutoRefresh.modesHandlers[mode] || baseHandler;
(handler.settings || baseHandler.settings)();
var interval = prompt(lang.promptInterval, AutoRefresh.DEFAULT_INTERVAL) || AutoRefresh.DEFAULT_INTERVAL;
AutoRefresh.storage.setItem(AutoRefresh.STORAGE_INTERVAL + AutoRefresh.currentPage, interval);
AutoRefresh.updateState(mode);
}
}
};
$(document).ready(function () {
var AutoRefresh = window.AutoRefresh;
if (!AutoRefresh.button) {
AutoRefresh.button = $('<button/>');
AutoRefresh.button.click(AutoRefresh.showSettings);
AutoRefresh.button.appendTo(AutoRefresh.buttonAppendTo || '.firstHeading');
}
AutoRefresh.updateState(AutoRefresh.storage.getItem(AutoRefresh.storageKey));
});
}
})();
// </nowiki>