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.

356 lines
10 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

var offsetSize = (function() {
if (mw.config.get('skin') === 'timeless' && window.innerWidth > 850) {
return 55;
} else if (mw.config.get('skin') === 'minerva') {
return 56;
} else {
return 0;
}
})();
var scrollBehaviorAvaliable = (function() {
// 检测Chrome
var v = navigator.userAgent.match(/Chrome\/(?<version>\S+)/);
if (v && v.groups.version) {
var chromeVersion = parseInt(v.groups.version);
return chromeVersion >= 61;
}
// 检测Firefox
v = navigator.userAgent.match(/Firefox\/(?<version>\S+)/);
if (v && v.groups.version) {
var firefoxVersion = parseInt(v.groups.version);
return firefoxVersion >= 36;
}
// 检测Safari
v = navigator.userAgent.match(/Version\/(?<version>\S+)/);
if (v && v.groups.version) { // Safari
var safariVersion = parseFloat(v.groups.version);
return safariVersion >= 14;
}
return false;
})();
function scrollToAnchor(link){
var el = document.getElementById(link.replace(/^#/, ''));
if (el) {
var target = $(el);
target.click(function(){ return false; });
var position = target.offset().top - offsetSize;
if (scrollBehaviorAvaliable) {
window.scrollTo({
top: position,
behavior: 'smooth'
});
} else {
$('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 setDismissLoginPrompt(){
var time = Date.now() / 1000;
localStorage.setItem('dismissLoginPrompt', time.toFixed(0));
}
function shouldShowLoginPrompt(){
if(!mw.user.isAnon()){ //用户已登录
setDismissLoginPrompt();
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 setDismissAdsPrompt(){
var time = Date.now() / 1000;
localStorage.setItem('dismissAdsPrompt', time.toFixed(0));
}
function shouldShowAdsPrompt(){
if(mw.config.get('wgCanonicalNamespace') === 'Special' && mw.config.get('wgAction') === 'view'){ // 仅查看页面弹出
return false;
}
var time = Date.now() / 1000;
var expire = 86400 * 7; //7天内不提醒
var dismissTime = localStorage.getItem('dismissAdsPrompt');
if(dismissTime !== null){
dismissTime = parseInt(dismissTime);
if(dismissTime + expire > time){
return false;
}
}
return true;
}
$(function(){
//锚链接偏移
$('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.substring(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);
});
// 修复使用中文输入法不会触发搜索提示的问题
$('#searchInput').on('compositionend', function() {
$(this).trigger('keypress')
});
// 初始化dialog
var messageDialog = new OO.ui.MessageDialog();
var windowManager = new OO.ui.WindowManager();
window._isekaiMessageDialog = messageDialog;
window._isekaiWindowManager = 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],
});
});
}
//显示登录引导框
if (shouldShowLoginPrompt()) {
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(){
setDismissLoginPrompt();
});
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')});
});
windowManager.openWindow(messageDialog, {
message: mw.message('isekai-login-prompt-content').parse(),
actions: [cancelBtn, loginBtn], // 去除注册按钮
});
}
// 广告存活检测
if (mw.config.get('wgIsekaiShowAds')) {
setTimeout(function() {
if ($('#ad-dec-7kVsyacCEKVGBLOiLVQ').length === 0) {
console.log('广告似乎被屏蔽了', $('#ad-dec-7kVsyacCEKVGBLOiLVQ'));
if (shouldShowAdsPrompt()) {
//显示广告引导框
var cancelBtn = new OO.ui.ActionWidget({
action: 'cancel',
label: mw.message('isekai-ads-prompt-btn-cancel').text(),
active: true,
flags: ['safe', 'close'],
});
cancelBtn.on('click', function(){
setDismissAdsPrompt();
});
windowManager.openWindow(messageDialog, {
title: mw.message('isekai-ads-prompt-title').parse(),
message: '',
size: 'medium',
actions: [cancelBtn],
});
messageDialog.$body.find('> .oo-ui-messageDialog-container > .oo-ui-messageDialog-text > .oo-ui-messageDialog-message')
.append(mw.message('isekai-ads-prompt-content').parse());
}
}
}, 500);
}
/* 展示Google的Adblock提示但是在大陆貌似没有用
if (mw.config.get('wgIsekaiShowAds')) {
window.googlefc = window.googlefc || {};
window.googlefc.callbackQueue = window.googlefc.callbackQueue || [];
googlefc.controlledMessagingFunction = (message) => {
if (shouldShowAdsPrompt()) {
message.proceed(true);
} else {
message.proceed(false);
}
};
function signalGooglefcPresent() {
if (!window.frames['googlefcPresent']) {
if (document.body) {
const iframe = document.createElement('iframe');
iframe.style = 'width: 0; height: 0; border: none; z-index: -1000; left: -1000px; top: -1000px;';
iframe.style.display = 'none';
iframe.name = 'googlefcPresent';
document.body.appendChild(iframe);
setDismissAdsPrompt();
} else {
window.requestAnimationFrame(signalGooglefcPresent);
}
}
}
signalGooglefcPresent();
}*/
// 显示用户头像
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">`);
}
// 更改ContentTranslation的机器翻译名字
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);
};
}
// 加载完成,开始动画
requestAnimationFrame(function() {
$('html').removeClass('content-loading').addClass('content-loaded');
}, 0);
window.addEventListener('beforeunload', function() {
$('html').addClass('content-unloading');
setTimeout(function() {
$('html').removeClass('content-unloading');
}, 5000);
});
});