User:Bluesoul/common.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
$(document).ready(function() {
setTimeout(function() {
if (Math.random() < 0.01) {
var gifPath = "https://deltarune.wiki/images/Rouxls_Kaard_overworld_appear.gif";
var pngPath = "https://deltarune.wiki/images/Rouxls_Kaard_overworld.png";
var reversePath = "https://zh.deltarune.wiki/images/Rouxls_Kaard_overworld_disappear.gif";
var character = null;
var floatInterval = null;
var isFloating = false;
function createCharacter() {
if (character) {
character.remove();
if (floatInterval) {
clearInterval(floatInterval);
floatInterval = null;
}
isFloating = false;
}
var windowHeight = $(window).height();
var windowWidth = $(window).width();
var left = Math.random() * (windowWidth - 200);
var top = Math.random() * (windowHeight * 0.4);
top = Math.max(top, 50);
character = $('<div>').css({
position: 'absolute',
left: left + 'px',
top: top + 'px',
zIndex: 9999,
cursor: 'pointer'
});
var img = $('<img>')
.attr('src', gifPath)
.css({
maxWidth: '200px',
maxHeight: '200px',
width: 'auto',
height: 'auto',
display: 'block'
});
character.append(img);
$('body').append(character);
setTimeout(function() {
if (character && img.attr('src') === gifPath) {
img.attr('src', pngPath);
startFloating();
}
}, 1500);
character.click(function() {
if (floatInterval) {
clearInterval(floatInterval);
floatInterval = null;
}
isFloating = false;
img.attr('src', reversePath);
$('<div>').text('后会有期,吾之佳虫!').css({
position: 'absolute',
left: (character.position().left + character.width()/2) + 'px',
top: (character.position().top - 30) + 'px',
color: '#9c27b0',
fontSize: '14px',
fontWeight: 'bold',
zIndex: 10000,
textShadow: '0 0 3px white',
pointerEvents: 'none',
transform: 'translateX(-50%)'
}).appendTo('body').animate({ top: '-=20px', opacity: 0 }, 1500, function() {
$(this).remove();
});
setTimeout(function() {
if (character) {
character.remove();
character = null;
}
}, 2000);
});
}
function startFloating() {
if (!character || isFloating) return;
isFloating = true;
var originalTop = parseFloat(character.css('top'));
var amplitude = 5;
var speed = 0.003;
floatInterval = setInterval(function() {
if (!character || !isFloating) {
clearInterval(floatInterval);
return;
}
var time = Date.now() * speed;
var offset = Math.sin(time) * amplitude;
character.css('top', (originalTop + offset) + 'px');
}, 16);
}
createCharacter();
}
}, 2000);
});
mw.loader.using([
'vue',
'mediawiki.api',
'mediawiki.util'
]).then(() => {
if (mw.config.get('wgArticleId') <= 0) {
return;
}
const { computed, createMwApp, onMounted, ref } = mw.loader.require('vue');
const api = new mw.Api();
function getEditInfo(rvdir) {
return api.get({
titles: mw.config.get('wgPageName'),
prop: 'revisions',
rvprop: 'ids|timestamp|user|size|parsedcomment|flags',
rvlimit: 2,
rvdir,
formatversion: 2
}).then(data => {
if (
!data ||
!data.query ||
!data.query.pages ||
!data.query.pages[0] ||
!data.query.pages[0].revisions
) {
return;
}
const revs = data.query.pages[0].revisions;
if (revs[0] && revs[1]) {
revs[0].diff = revs[0].size - revs[1].size;
}
return revs[0];
});
}
const editInfo = createMwApp({
name: 'edit-info',
template: `
<p v-if="lastEdit">
<user-links :user="lastEdit.user" />
于
<a :href="diffLink" class="mw-changeslist-date">
{{ lastEditDate }}
</a>
修改了此页面。
</p>
<p v-if="lastEdit && lastEdit.parsedcomment">
编辑摘要:<span class="comment" v-html="lastEdit.parsedcomment"></span>
</p>
`,
setup() {
const scriptPath = mw.config.get('wgScriptPath');
const lastEdit = ref(null);
onMounted(() => {
getEditInfo().then(data => {
if (!data) {
return;
}
lastEdit.value = data;
});
});
return {
lastEdit,
lastEditDate: computed(() =>
new Date(lastEdit.value.timestamp).toLocaleString()
),
diffLink: computed(() =>
`${scriptPath}/?diff=${lastEdit.value.revid}`
)
};
}
});
editInfo.component('user-links', {
name: 'user-links',
template: `
<span class="history-user">
<a :href="userpageLink" class="mw-userlink">{{user}}</a>
<span class="mw-usertoollinks mw-changeslist-links">
<span><a :href="talkLink">讨论</a></span>
<span><a :href="contribsLink">贡献</a></span>
<span><a :href="blockLink">封禁</a></span>
</span>
</span>
`,
props: {
user: {
type: String,
required: true
}
},
setup: ({ user }) => ({
userpageLink: mw.util.getUrl(`User:${user}`),
talkLink: mw.util.getUrl(`User talk:${user}`),
contribsLink: mw.util.getUrl(`Special:Contributions/${user}`),
blockLink: mw.util.getUrl(`Special:Block/${user}`),
user
})
});
mw.hook('wikipage.content').add(() => {
if (document.getElementById('edit-info')) {
return;
}
const root = document.createElement('div');
root.id = 'edit-info';
const subtitle = document.getElementById('mw-content-subtitle');
if (!subtitle) {
return;
}
subtitle.appendChild(root);
editInfo.mount(root);
});
});
const url = new URL(window.location.href);
url.searchParams.set('action', 'purge');
const link = mw.util.addPortletLink(
'p-cactions',
url.href,
'刷新',
'ca-purge',
'清除本页缓存',
'n'
);
if (link) {
link.addEventListener('click', event => {
event.preventDefault();
(new mw.Api()).post({
action: 'purge',
titles: mw.config.get('wgPageName'),
forcerecursivelinkupdate: true
}).done(() =>
window.location.reload(true))
.fail(code =>
mw.notify(`无法刷新缓存,错误码:${code}。`)
);
});
}