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.

67 lines
2.1 KiB
JavaScript

2 years ago
var authToken = '';
var tokenRefreshTime = 0;
function createToken() {
var api = new mw.Api();
return api.postWithToken('csrf', {
action: 'aitoolbox',
2 years ago
method: 'createtoken'
});
}
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);
window.open(url, '_blank');
}
function launchAIToolbox() {
2 years ago
var currentTime = new Date().getTime();
if (currentTime - tokenRefreshTime > 3600000) {
mw.notify(mw.msg('isekai-ai-toolbox-loading'), {
id: "loading-ai-toolbox-notify"
2 years ago
});
createToken().done(function(data) {
if (data.aitoolbox && data.aitoolbox.createtoken) {
authToken = data.aitoolbox.createtoken.token;
2 years ago
tokenRefreshTime = new Date().getTime();
openAIToolboxPage(authToken);
$('#loading-ai-toolbox-notify').trigger('click');
2 years ago
}
});
} else {
openAIToolboxPage(authToken);
2 years ago
}
}
$(function() {
if (mw.config.get('wgIsArticle')) {
1 week 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
}
});
1 week 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
});