You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.2 KiB
JavaScript

3 years ago
var offsetSize = 55;
function scrollToAnchor(link){
var target = $(link.replace(/\./g, '\\.'));
if(target.length > 0){
target.click(function(){ return false; });
var position = target.offset().top - offsetSize;
$('html, body').animate({
scrollTop: position,
}, 500);
return false;
} else {
return true;
}
}
function incPageVisited(){
var pageVisited = sessionStorage.getItem('pageVisited');
if(pageVisited){
pageVisited = parseInt(pageVisited) + 1;
} else {
pageVisited = 1;
}
sessionStorage.setItem('pageVisited', pageVisited.toString());
}
incPageVisited();
function getPageVisited(){
var pageVisited = sessionStorage.getItem('pageVisited');
if(pageVisited){
return parseInt(pageVisited);
} else {
return 1;
}
}
function setDismiss(){
var time = Date.now() / 1000;
localStorage.setItem('dismissLoginPrompt', time.toFixed(0));
}
function shouldShowPrompt(){
if(!mw.user.isAnon()){ //用户已登录
setDismiss();
return false;
}
if(getPageVisited() < 4){ // 直到打开第四个页面,才弹出注册邀请
return false;
}
if(mw.config.get('wgCanonicalNamespace') === 'Special' &&
['Userlogin', 'CreateAccount', 'Userlogout'].indexOf(mw.config.get('wgCanonicalSpecialPageName')) >= 0){ //屏蔽登录和注册页面
return false;
}
var time = Date.now() / 1000;
var expire = 86400 * 7; //7天不提醒
var dismissTime = localStorage.getItem('dismissLoginPrompt');
if(dismissTime !== null){
dismissTime = parseInt(dismissTime);
if(dismissTime + expire > time){
return false;
}
}
return true;
}
$(function(){
//折叠
$('.mw-collapse').each(function(){
var dom = $(this);
dom.find('.mw-collapse-title:first').append('<span class="toctoggle">&nbsp;<a role="button" tabindex="0" class="mw-collapse-toggle">显示</a>&nbsp;</span>');
var content = dom.find('.mw-collapse-content:first');
var toggleBtn = dom.find('.mw-collapse-toggle:first');
toggleBtn.click(function(){
if(content.css('display') === 'none'){
toggleBtn.text(mw.message('isekai-collapse-hide').parse());
content.fadeIn(250);
} else {
toggleBtn.text(mw.message('isekai-collapse-show').parse());
content.fadeOut(150);
}
});
});
//锚链接偏移
$('body').on('click', 'a', function(){
var href = $(this).prop('href');
var path = location.origin + location.pathname;
if(typeof href === 'string' && href.startsWith(path) && href[path.length] == '#' && href.length > path.length + 1){
return scrollToAnchor(href.substr(path.length));
}
});
// 替换搜索输入框的中文字符
function replaceChars(dom){
if(dom.value.indexOf('') !== -1 || dom.value.indexOf('`') !== -1){
var selectionStart = dom.selectionStart,
selectionEnd = dom.selectionEnd;
dom.value = dom.value.replace(//g, ':').replace(/`/g, '·');
dom.setSelectionRange(selectionStart, selectionEnd);
}
}
$('#searchInput, #wpNewTitleMain input, #searchText input[name="search"]').on('input', function(e){
if(!e.originalEvent.isComposing){
replaceChars(this);
}
});
$('#searchInput, #wpNewTitleMain input, #searchText input[name="search"]').on('compositionend', function(e){
replaceChars(this);
});
// 初始化dialog
var messageDialog = new OO.ui.MessageDialog();
var windowManager = new OO.ui.WindowManager();
$('body').append(windowManager.$element);
windowManager.addWindows([messageDialog]);
// 替换手机端编辑按钮
if (mw.config.get('skin') === 'minerva') {
$('#ca-edit, .mw-editsection a').click(function(e) {
e.preventDefault();
var cancelBtn = new OO.ui.ActionWidget({
action: 'cancel',
label: mw.message('isekai-editor-prompt-btn-cancel').text(),
flags: ['safe', 'close'],
});
var useMobileEditorBtn = new OO.ui.ActionWidget({
action: 'mobileEditor',
label: mw.message('isekai-editor-prompt-btn-use-mobile').text(),
});
var useDesktopEditorBtn = new OO.ui.ActionWidget({
action: 'desktopEditor',
label: mw.message('isekai-editor-prompt-btn-use-desktop').text(),
flags: ['primary', 'progressive'],
});
useMobileEditorBtn.on('click', function() {
location.href = e.target.href;
});
useDesktopEditorBtn.on('click', function() {
var title = new mw.Title(mw.config.get('wgTitle'), mw.config.get('wgNamespaceNumber'));
location.href = title.getUrl({ veaction: 'edit', mobileaction: 'toggle_view_desktop' });
});
windowManager.openWindow(messageDialog, {
message: mw.message('isekai-editor-prompt-content').parse(),
actions: [cancelBtn, useMobileEditorBtn, useDesktopEditorBtn],
});
});
}
//显示登录引导框
var cancelBtn = new OO.ui.ActionWidget({
action: 'cancel',
label: mw.message('isekai-login-prompt-btn-cancel').text(),
flags: ['safe', 'close'],
});
var registerBtn = new OO.ui.ActionWidget({
action: 'register',
label: mw.message('isekai-login-prompt-btn-createaccount').text(),
});
var loginBtn = new OO.ui.ActionWidget({
action: 'login',
active: true,
label: mw.message('nav-login-createaccount').text(),
flags: ['primary', 'progressive'],
});
cancelBtn.on('click', function(){
setDismiss();
});
registerBtn.on('click', function(){
location.href = mw.util.getUrl('Special:CreateAccount', {returnto: mw.config.get('wgPageName')});
});
loginBtn.on('click', function(){
location.href = mw.util.getUrl('Special:Userlogin', {returnto: mw.config.get('wgPageName')});
});
if(shouldShowPrompt()){
windowManager.openWindow(messageDialog, {
message: mw.message('isekai-login-prompt-content').parse(),
actions: [cancelBtn, loginBtn], // 去除注册按钮
});
}
// 显示用户头像
if(mw.config.get('wgNamespaceNumber') === 2 && mw.config.get('wgAvatarTemplate')) {
$('<div id="userHeading" class="user-heading"></div>').insertAfter("#firstHeading");
var firstHeading = $("#firstHeading");
var userHeading = $("#userHeading");
userHeading.append(firstHeading);
var avatarUrl = mw.config.get('wgAvatarTemplate')
.replace(/\{username\}/g, encodeURI(mw.config.get('wgTitle')));
var avatarAlt = mw.config.get('wgRelevantPageName');
userHeading.append(`<img src="${avatarUrl}" alt="${avatarAlt}" class="user-avatar">`);
}
if (mw.cx && mw.cx.MachineTranslationManager) {
let _getProviderLabel = mw.cx.MachineTranslationManager.prototype.getProviderLabel;
mw.cx.MachineTranslationManager.prototype.getProviderLabel = function ( provider ) {
if (mw.config.get('wgUserLanguage', 'en').indexOf('zh') === 0) {
var labels = {
Google: mw.msg( 'cx-tools-mt-provider-title', 'Google翻译' ),
Yandex: mw.msg( 'cx-tools-mt-provider-title', 'Yandex翻译' ),
Baidu: mw.msg( 'cx-tools-mt-provider-title', '百度翻译' ),
Youdao: mw.msg( 'cx-tools-mt-provider-title', '有道翻译' ),
LingoCloud: mw.msg( 'cx-tools-mt-provider-title', '彩云小译' ),
};
var label = labels[provider];
if (label) {
return label;
}
}
return _getProviderLabel.call(this, provider);
};
}
});