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...

158 lines
4.8 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 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);
}
function createToken() {
var api = new mw.Api();
return api.postWithToken('csrf', {
action: 'aitoolbox',
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');
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,
});
frame.control.on('hid', (frame, info) => {
frame.closeFrame();
frame = null;
onFrameClosed();
});
frame.on('minimizeButton', 'click', () => {
frame.hide();
onFrameClosed();
mw.notify(mw.msg('isekai-ai-toolbox-minimized'));
});
frame.on('openInNewTab', 'click', () => {
window.open(url, '_blank');
frame.closeFrame();
frame = null;
onFrameClosed();
});
}
}
function launchAIToolbox() {
if (frame) {
// 恢复已经打开的窗口
frame.show();
onFrameOpened();
} 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);
}
}
}
$(function() {
if (mw.config.get('wgIsArticle')) {
var menuIcon = new OO.ui.IconWidget({ icon: 'isekai-aitoolbox-iconAI' });
isekai.fab.addButton({
id: 'ai-toolbox-launcher',
label: mw.msg('isekai-ai-toolbox-menubutton'),
icon: menuIcon.$element[0],
priority: 90,
onClick: function() {
launchAIToolbox();
}
});
var bottomMenuIcon = new OO.ui.IconWidget({ icon: 'isekai-aitoolbox-iconAI' });
bottomNavBtn = isekai.bottomNav.addButton({
id: 'ai-toolbox-launcher',
label: mw.msg('isekai-ai-toolbox-menubutton'),
icon: bottomMenuIcon.$element[0],
priority: 90,
onClick: function() {
launchAIToolbox();
}
});
}
if ($('#btn-enter-ai-toolbox').length > 0) {
$('#btn-enter-ai-toolbox').on('click', function() {
launchAIToolbox();
});
}
});