MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
创建页面,内容为“→这里的任何JavaScript将为所有用户在每次页面加载时加载。: <script> // 以下代码应在 MediaWiki:Common.js 中加载 // 为了演示,这里直接嵌入;实际使用时请移至 Common.js (function () { 'use strict'; // 仅在首页执行(检测页面名称) var pageName = mw.config.get('wgPageName') || ''; if (pageName !== '首页' && pageName !== 'Main_Page') { return; } // ---------- 1.…” |
无编辑摘要 |
||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */ | /* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */ | ||
(function () { | |||
'use strict'; | 'use strict'; | ||
// | // 只在已登录用户浏览主页时显示。 | ||
if ( | |||
!mw.config.get('wgUserName') || | |||
!mw.config.get('wgIsMainPage') || | |||
mw.config.get('wgAction') !== 'view' | |||
) { | |||
return; | |||
} | } | ||
// ---------- | $(function () { | ||
if (document.getElementById('wiki-ai-widget')) return; | |||
var root = document.createElement('div'); | |||
root.id = 'wiki-ai-widget'; | |||
// 这里只插入固定界面,不插入用户输入或模型回答。 | |||
root.innerHTML = [ | |||
'<button type="button" class="wai-launcher"', | |||
' aria-label="打开 AI 聊天" aria-expanded="false"', | |||
' aria-controls="wiki-ai-panel">AI</button>', | |||
'<section id="wiki-ai-panel" class="wai-panel" hidden', | |||
' role="dialog" aria-labelledby="wiki-ai-title">', | |||
'<div class="wai-header">', | |||
'<strong id="wiki-ai-title">AI 助手</strong>', | |||
'<button type="button" class="wai-close"', | |||
' aria-label="关闭聊天">×</button>', | |||
'</div>', | |||
'<div class="wai-messages" role="log"', | |||
' aria-live="polite" aria-label="聊天记录"></div>', | |||
'<p class="wai-status" role="status"></p>', | |||
'<form class="wai-form">', | |||
'<input class="wai-input" type="text"', | |||
' aria-label="输入问题" placeholder="输入你的问题…"', | |||
' maxlength="2000" autocomplete="off" required>', | |||
'<button class="wai-send" type="submit">发送</button>', | |||
'</form>', | |||
'</section>' | |||
].join(''); | |||
document.body.appendChild(root); | |||
var launcher = root.querySelector('.wai-launcher'); | |||
var panel = root.querySelector('.wai-panel'); | |||
var closeButton = root.querySelector('.wai-close'); | |||
var log = root.querySelector('.wai-messages'); | |||
var status = root.querySelector('.wai-status'); | |||
var form = root.querySelector('.wai-form'); | |||
var input = root.querySelector('.wai-input'); | |||
var sendButton = root.querySelector('.wai-send'); | |||
var history = []; | |||
var busy = false; | |||
function setOpen(open) { | |||
panel.hidden = !open; | |||
launcher.setAttribute('aria-expanded', String(open)); | |||
launcher.setAttribute( | |||
'aria-label', | |||
open ? '收起 AI 聊天' : '打开 AI 聊天' | |||
); | |||
if (open) { | |||
log.scrollTop = log.scrollHeight; | |||
(busy ? closeButton : input).focus(); | |||
} else { | |||
launcher.focus(); | |||
} | |||
} | } | ||
function addMessage(role, text) { | |||
var message = document.createElement('div'); | |||
message.className = 'wai-message wai-' + role; | |||
var label = document.createElement('strong'); | |||
label.textContent = role === 'user' ? '你' : 'AI'; | |||
var content = document.createElement('div'); | |||
content.textContent = text; | |||
message.appendChild(label); | |||
message.appendChild(content); | |||
log.appendChild(message); | |||
log.scrollTop = log.scrollHeight; | |||
} | |||
launcher.addEventListener('click', function () { | |||
setOpen(panel.hidden); | |||
}); | |||
closeButton.addEventListener('click', function () { | |||
setOpen(false); | |||
}); | |||
root.addEventListener('keydown', function (event) { | |||
if (event.key === 'Escape' && !panel.hidden) { | |||
event.preventDefault(); | |||
setOpen(false); | |||
} | |||
}); | }); | ||
form.addEventListener('submit', async function (event) { | |||
event.preventDefault(); | |||
var question = input.value.trim(); | |||
if (!question || busy) return; | |||
var messages = history.concat([ | |||
{ role: 'user', content: question } | |||
]); | |||
addMessage('user', question); | |||
input.value = ''; | |||
busy = true; | |||
input.disabled = true; | |||
sendButton.disabled = true; | |||
status.textContent = '正在思考…'; | |||
var controller = new AbortController(); | |||
var timer = setTimeout(function () { | |||
controller.abort(); | |||
}, 60000); | |||
try { | |||
var response = await fetch('/ai/chat', { | |||
method: 'POST', | |||
credentials: 'same-origin', | |||
headers: { | |||
'Content-Type': 'application/json' | |||
}, | |||
body: JSON.stringify({ messages: messages }), | |||
signal: controller.signal | |||
}); | |||
if (response.status === 401) { | |||
throw new Error('登录已失效,请重新登录后刷新页面。'); | |||
} | |||
if (response.status === 429) { | |||
throw new Error('请求过于频繁,请稍后再试。'); | |||
} | |||
if (!response.ok) { | |||
throw new Error('服务暂时不可用,请稍后重试。'); | |||
} | |||
var data = await response.json(); | |||
if ( | |||
typeof data.reply !== 'string' || | |||
!data.reply.trim() | |||
) { | |||
throw new Error('未收到有效回答,请重试。'); | |||
} | |||
addMessage('assistant', data.reply); | |||
// 保留最近 10 轮成功对话作为后续请求的上下文。 | |||
history = messages.concat([ | |||
{ role: 'assistant', content: data.reply } | |||
]).slice(-20); | |||
status.textContent = ''; | |||
} catch (error) { | |||
status.textContent = error.name === 'AbortError' | |||
? '请求超时,请重试。' | |||
: (error.message || '连接失败,请稍后重试。'); | |||
input.value = question; | |||
} finally { | |||
clearTimeout(timer); | |||
busy = false; | |||
input.disabled = false; | |||
sendButton.disabled = false; | |||
// 仅当焦点仍在组件内时恢复输入焦点。 | |||
if (!panel.hidden && root.contains(document.activeElement)) { | |||
input.focus(); | |||
} | |||
2026年10月2日 (五) 01:10的最新版本
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
(function () {
'use strict';
// 只在已登录用户浏览主页时显示。
if (
!mw.config.get('wgUserName') ||
!mw.config.get('wgIsMainPage') ||
mw.config.get('wgAction') !== 'view'
) {
return;
}
$(function () {
if (document.getElementById('wiki-ai-widget')) return;
var root = document.createElement('div');
root.id = 'wiki-ai-widget';
// 这里只插入固定界面,不插入用户输入或模型回答。
root.innerHTML = [
'<button type="button" class="wai-launcher"',
' aria-label="打开 AI 聊天" aria-expanded="false"',
' aria-controls="wiki-ai-panel">AI</button>',
'<section id="wiki-ai-panel" class="wai-panel" hidden',
' role="dialog" aria-labelledby="wiki-ai-title">',
'<div class="wai-header">',
'<strong id="wiki-ai-title">AI 助手</strong>',
'<button type="button" class="wai-close"',
' aria-label="关闭聊天">×</button>',
'</div>',
'<div class="wai-messages" role="log"',
' aria-live="polite" aria-label="聊天记录"></div>',
'<p class="wai-status" role="status"></p>',
'<form class="wai-form">',
'<input class="wai-input" type="text"',
' aria-label="输入问题" placeholder="输入你的问题…"',
' maxlength="2000" autocomplete="off" required>',
'<button class="wai-send" type="submit">发送</button>',
'</form>',
'</section>'
].join('');
document.body.appendChild(root);
var launcher = root.querySelector('.wai-launcher');
var panel = root.querySelector('.wai-panel');
var closeButton = root.querySelector('.wai-close');
var log = root.querySelector('.wai-messages');
var status = root.querySelector('.wai-status');
var form = root.querySelector('.wai-form');
var input = root.querySelector('.wai-input');
var sendButton = root.querySelector('.wai-send');
var history = [];
var busy = false;
function setOpen(open) {
panel.hidden = !open;
launcher.setAttribute('aria-expanded', String(open));
launcher.setAttribute(
'aria-label',
open ? '收起 AI 聊天' : '打开 AI 聊天'
);
if (open) {
log.scrollTop = log.scrollHeight;
(busy ? closeButton : input).focus();
} else {
launcher.focus();
}
}
function addMessage(role, text) {
var message = document.createElement('div');
message.className = 'wai-message wai-' + role;
var label = document.createElement('strong');
label.textContent = role === 'user' ? '你' : 'AI';
var content = document.createElement('div');
content.textContent = text;
message.appendChild(label);
message.appendChild(content);
log.appendChild(message);
log.scrollTop = log.scrollHeight;
}
launcher.addEventListener('click', function () {
setOpen(panel.hidden);
});
closeButton.addEventListener('click', function () {
setOpen(false);
});
root.addEventListener('keydown', function (event) {
if (event.key === 'Escape' && !panel.hidden) {
event.preventDefault();
setOpen(false);
}
});
form.addEventListener('submit', async function (event) {
event.preventDefault();
var question = input.value.trim();
if (!question || busy) return;
var messages = history.concat([
{ role: 'user', content: question }
]);
addMessage('user', question);
input.value = '';
busy = true;
input.disabled = true;
sendButton.disabled = true;
status.textContent = '正在思考…';
var controller = new AbortController();
var timer = setTimeout(function () {
controller.abort();
}, 60000);
try {
var response = await fetch('/ai/chat', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ messages: messages }),
signal: controller.signal
});
if (response.status === 401) {
throw new Error('登录已失效,请重新登录后刷新页面。');
}
if (response.status === 429) {
throw new Error('请求过于频繁,请稍后再试。');
}
if (!response.ok) {
throw new Error('服务暂时不可用,请稍后重试。');
}
var data = await response.json();
if (
typeof data.reply !== 'string' ||
!data.reply.trim()
) {
throw new Error('未收到有效回答,请重试。');
}
addMessage('assistant', data.reply);
// 保留最近 10 轮成功对话作为后续请求的上下文。
history = messages.concat([
{ role: 'assistant', content: data.reply }
]).slice(-20);
status.textContent = '';
} catch (error) {
status.textContent = error.name === 'AbortError'
? '请求超时,请重试。'
: (error.message || '连接失败,请稍后重试。');
input.value = question;
} finally {
clearTimeout(timer);
busy = false;
input.disabled = false;
sendButton.disabled = false;
// 仅当焦点仍在组件内时恢复输入焦点。
if (!panel.hidden && root.contains(document.activeElement)) {
input.focus();
}