var authToken = ''; var tokenRefreshTime = 0; function createToken() { var api = new mw.Api(); return api.postWithToken('csrf', { action: 'aitoolbox', method: 'createtoken' }); } 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); window.open(url, '_blank'); } function launchAIToolbox() { 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: 'robot' }); 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: 'robot' }); bottomNavBtn = isekai.bottomNav.addButton({ id: 'ai-toolbox-launcher', label: mw.msg('isekai-ai-toolbox-menubutton'), icon: bottomMenuIcon.$element[0], priority: 90, onClick: function() { launchAIToolbox(); } }); } });