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.
IsekaiAIToolbox/modules/ext.isekai.ai-toolbox.launc...

157 lines
4.8 KiB
JavaScript

2 years ago
var authToken = '';
var tokenRefreshTime = 0;
var jsFrame = new JSFrame();
var frame = null;
try {
jsFrame.windowManager.parentElement.style.zIndex = 1000; // 设置jsFrame的z-index
} catch (e) {
console.error('Cannot set z-index for jsFrame', e);
}
2 years ago
function createToken() {
var api = new mw.Api();
return api.postWithToken('csrf', {
action: 'aitoolbox',
2 years ago
method: 'createtoken'
});
}
function onFrameOpened() {
$('body').addClass('isekai-aitoolbox-visible');
}
function onFrameClosed() {
$('body').removeClass('isekai-aitoolbox-visible');
}
function openAIToolboxPage(token) {
var urlTemplate = mw.config.get('wgIsekaiAIToolboxFrontendUrl');
2 years ago
var title = mw.config.get('wgTitle');
var url = urlTemplate.replace(/\{title\}/g, encodeURIComponent(title)).replace(/\{token\}/g, token);
if ($(window).width() < 768) { // 手机端,直接打开新窗口
window.open(url, '_blank');
} else { // PC端使用jsFrame打开
var frameWidth = 450;
var frameHeight = $(window).height() - 100;
var frameRight = 40;
var frameLeft = $(window).width() - frameWidth - frameRight;
frame = jsFrame.create({
name: `isekai-ai-toolbox`,
title: mw.msg('isekai-ai-toolbox-title'),
left: frameLeft, top: 40, width: frameWidth, height: frameHeight, minWidth: 350, minHeight: 550,
appearanceName: 'material',
appearanceParam: {
border: {
shadow: '2px 2px 10px rgba(0, 0, 0, 0.5)',
width: 0,
radius: 6,
},
titleBar: {
color: 'white',
background: '#333333',
leftMargin: 40,
buttonsOnLeft: [
{
fa: 'fas fa-external-link-alt',
name: 'openInNewTab',
visible: true,
},
],
}
},
style: {
backgroundColor: 'rgba(249,250,251,0.95)',
overflow: 'auto'
},
url: url,
}).show();
onFrameOpened();
frame.setControl({
maximizeButton: 'maximizeButton',
demaximizeButton: 'restoreButton',
hideButton: 'closeButton',
animation: true,
animationDuration: 150,
2 years ago
});
frame.control.on('hid', (frame, info) => {
frame = null;
onFrameClosed();
});
frame.on('minimizeButton', 'click', () => {
frame.hide();
onFrameClosed();
mw.notify(mw.msg('isekai-ai-toolbox-minimized'));
2 years ago
});
frame.on('openInNewTab', 'click', () => {
window.open(url, '_blank');
frame.closeFrame();
frame = null;
onFrameClosed();
});
}
}
function launchAIToolbox() {
if (frame) {
// 恢复已经打开的窗口
frame.show();
onFrameOpened();
2 years ago
} else {
var currentTime = new Date().getTime();
if (currentTime - tokenRefreshTime > 3600000) {
mw.notify(mw.msg('isekai-ai-toolbox-loading'), {
id: "loading-ai-toolbox-notify"
});
createToken().done(function(data) {
if (data.aitoolbox && data.aitoolbox.createtoken) {
authToken = data.aitoolbox.createtoken.token;
tokenRefreshTime = new Date().getTime();
openAIToolboxPage(authToken);
$('#loading-ai-toolbox-notify').trigger('click');
}
});
} else {
openAIToolboxPage(authToken);
}
2 years ago
}
}
$(function() {
if (mw.config.get('wgIsArticle')) {
2 months ago
var menuIcon = new OO.ui.IconWidget({ icon: 'isekai-aitoolbox-iconAI' });
2 years ago
isekai.fab.addButton({
id: 'ai-toolbox-launcher',
label: mw.msg('isekai-ai-toolbox-menubutton'),
2 years ago
icon: menuIcon.$element[0],
priority: 90,
onClick: function() {
launchAIToolbox();
2 years ago
}
});
2 months ago
var bottomMenuIcon = new OO.ui.IconWidget({ icon: 'isekai-aitoolbox-iconAI' });
2 years ago
bottomNavBtn = isekai.bottomNav.addButton({
id: 'ai-toolbox-launcher',
label: mw.msg('isekai-ai-toolbox-menubutton'),
2 years ago
icon: bottomMenuIcon.$element[0],
priority: 90,
onClick: function() {
launchAIToolbox();
2 years ago
}
});
}
if ($('#btn-enter-ai-toolbox').length > 0) {
$('#btn-enter-ai-toolbox').on('click', function() {
launchAIToolbox();
});
}
2 years ago
});