把 XML 编辑器嵌入你的网站
面向开发者:如何把 XML 编辑器放进你自己的页面(放在 iframe 中、在单独的窗口中打开,或作为只读查看器),以及文档、保存内容和 AI 结果如何在编辑器和你的服务器之间传递。无需注册,也不需要我们提供的密钥。
任何 https 页面(以及 localhost)都可以用 <iframe> 和 ?embed=1 嵌入编辑器,无需注册,无需密钥。嵌入后,编辑器会隐藏页面文字并显示“保存”按钮。有四种方式传入和取出文档。
1. 从链接打开文件,保存到你的服务器
src= 打开一个文件(你的服务器必须允许 CORS),webhook= 让“保存”(Ctrl+S)把文档发送到该地址。添加 &auto 可在每次修改后保存。
<iframe src="https://xml.giit.ch/zh/?embed=1&src=https://your-site.com/data/catalog.xml&webhook=https://your-site.com/save.php"
allow="clipboard-write" style="width:100%;height:800px;border:0"></iframe>
Webhook 会收到一个带 JSON 的 POST 请求 { editor: "xml", event: "save", id, name, content, updated }。链接不携带令牌,因为令牌会留在服务器日志和浏览器历史记录中。因此,在地址中指定 Webhook 适合不需要令牌的保存地址,例如 localhost 或你的内网。对于公共服务器,请通过 xmleditor:load 中的 hook: { url, token } 传入 Webhook 和令牌,并像下面这个简短的 PHP 脚本一样验证令牌:
<?php
header('Access-Control-Allow-Origin: https://xml.giit.ch');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Methods: POST, OPTIONS');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit;
if (($_SERVER['HTTP_AUTHORIZATION'] ?? '') !== 'Bearer SECRET') { http_response_code(403); exit; }
$d = json_decode(file_get_contents('php://input'), true);
$name = preg_replace('/[^\w\- ]/u', '_', $d['name']);
file_put_contents(__DIR__ . "/data/$name.xml", $d['content']);
echo '{"ok":true}';
2. 由你的页面保存文档(postMessage)
编辑器发送 xmleditor:ready,你的页面以 xmleditor:load 回应,编辑器报告 xmleditor:loaded,有实际编辑时发送 xmleditor:change,用户保存时发送 xmleditor:save。你的页面可以发送 xmleditor:get(获取当前状态)或 xmleditor:save(相当于点击“保存”)。这样就能通过你自己的登录验证和会话来保存。
<iframe id="ed" src="https://xml.giit.ch/zh/?embed=1" allow="clipboard-write" style="width:100%;height:800px;border:0"></iframe>
<script>
const ed = document.getElementById('ed');
const ORIGIN = 'https://xml.giit.ch';
addEventListener('message', async e => {
if (e.source !== ed.contentWindow || e.origin !== ORIGIN) return;
const m = e.data;
if (m.type === 'xmleditor:ready') { // 编辑器已就绪:把文档交给它
ed.contentWindow.postMessage({
type: 'xmleditor:load',
name: 'Catalog',
content: await (await fetch('/data/catalog.xml')).text()
}, ORIGIN);
}
if (m.type === 'xmleditor:change') document.title = '● not saved'; // 一次实际编辑
if (m.type === 'xmleditor:save') { // 用户点击了“保存”
await fetch('/api/save', { method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: m.name, content: m.content }) });
document.title = 'Saved';
}
});
// 你自己页面上的保存按钮:回应以 xmleditor:save 返回
function saveNow() { ed.contentWindow.postMessage({ type: 'xmleditor:get' }, ORIGIN); }
</script>
页面内的 AI 助手:在框架中同样可用。在 iframe 上设置 allow="clipboard-write" 后,“复制提示词”会直接使用剪贴板(否则编辑器会退回旧的复制命令,或显示提示词供手动复制)。AI 发送的链接无法打开嵌入其他网站的编辑器,因此编辑器会提示 AI 和用户把 AI 的完整回答粘贴到编辑器中,或在单独的窗口中打开编辑器。
3. 在单独的窗口中
window.open("https://xml.giit.ch/zh/?embed=window", "xmleditor-17") 会在单独的窗口中打开完整的编辑器,并通过 window.opener 使用相同的消息。AI 的链接可以在该窗口中打开。xmleditor:load 还可以带上 context: { host, title, rules }(AI 提示词随后会说明文档属于哪里,并遵循你的规则),以及 hook: { url, token },即仅用于此文档且不会被保存的 Webhook。关闭有未保存更改的窗口时会发送 xmleditor:closing。
4. 通过你自己的服务器使用 AI
向编辑器提供你自己的提示词、你网站上的一个地址和本次编辑器会话的密钥。AI 对话框随后会显示“已连接到”你的网站、你告诉 AI 的内容,以及修改当前文档或新建一个文档的选项;复制提示词会把这些都组合起来。AI 带着密钥把结果发送到你的服务器,你的页面再在编辑器中打开它;新文档会单独打开,并保留在用户的浏览器中。每个编辑器会话一个密钥:编辑器打开期间有效,只有在没有密钥时,编辑器才会用 xmleditor:key-request 请求。编辑器自己从不发送密钥,也从不保存它;地址必须是你自己主机或其子域名上的 https 地址。
以下为示意伪代码。使用前请实现会话查找、定期存活检查、密钥撤销、权限验证和草稿存储。key-request 的处理代码应放在消息监听器内部,位于发送窗口和来源检查之后。
const key = async () => (await fetch('/api/ai-key.php?doc=17')).json(); // 每个编辑器会话一个密钥
const k = await key();
ed.contentWindow.postMessage({ type: 'xmleditor:load', name: 'Catalog', content, ai: {
label: 'My Site', prompt: 'Keep the sku attributes. Prices in CHF.',
endpoint: 'https://your-site.com/api/ai-result.php', key: k.key } }, ORIGIN);
// 记住 xmleditor:loaded 中的 id,忽略 id 不同的 change/save/closing
// 仅在编辑器没有密钥时:
if (m.type === 'xmleditor:key-request') { const k = await key(); ed.contentWindow.postMessage({ type: 'xmleditor:ai', key: k.key }, ORIGIN); }
// 编辑器打开期间,每分钟通知你的服务器;关闭编辑器或页面会使密钥作废
setInterval(() => fetch('/api/ai-key.php?alive=' + k.key.slice(0, 8)), 60000);
// 你的服务器有了结果(轮询、SSE …):target 为 "current" 或 "new"
ed.contentWindow.postMessage({ type: 'xmleditor:load', name: 'Catalog', content: draft, from: 'ai', target }, ORIGIN);
<?php // api/ai-key.php:已登录用户本次编辑器会话的密钥(由你的页面调用)
session_start(); // 检查用户是否可以编辑此文档
$key = bin2hex(random_bytes(24));
$_SESSION['ai'][hash('sha256', $key)] = ['doc' => (int)$_GET['doc'], 'seen' => time()]; // 或存入你的数据库;?alive= 会重新设置 seen
echo json_encode(['key' => $key]);
<?php // api/ai-result.php:AI 向这里发送(无需 CORS 头:AI 从服务器调用它)
$key = preg_replace('/^Bearer\s+/', '', $_SERVER['HTTP_AUTHORIZATION'] ?? '');
$row = find_key(hash('sha256', $key)); // 你的数据库:未作废,且在最近 3 分钟内出现过
if (!$row) { http_response_code(401); exit('{"ok":false,"error":"key not valid"}'); }
$d = json_decode(file_get_contents('php://input', false, null, 0, 2000000), true);
save_draft($row['doc'], $d['content'], $d['target'] === 'new' ? 'new' : 'current'); // 你的页面用 xmleditor:load 显示它
echo '{"ok":true}';
密钥至少使用 128 位随机数,只保存其哈希值,将每个密钥绑定到已登录用户、文档和本次编辑器会话,每个会话一个,仅在你的页面报告会话仍打开时有效,并在编辑器或页面关闭时作废。
你自己的 AI 对话框和回答说明:在 ai 中使用 dialog: "page" 时,点击 AI 助手会发送带有 request id 的 xmleditor:ai-click;在 300 ms 内用 { type: "xmleditor:ai-click", request, handled: true } 回应并显示你自己的对话框 - 否则编辑器会打开自己的对话框。answer: "…" 会用你的文本替换复制的提示词中“HOW TO ANSWER”部分("none" 会省略该部分,"" 会恢复编辑器的原有内容);此时提示词不含密钥。如果你的服务器没有提供密钥,用 { type: "xmleditor:ai", decline: true, message: "…" } 回应 xmleditor:key-request:编辑器会立即停止等待,并在你的主机后面显示你的消息 - 请用你的页面所用的语言编写。在提示词中,文档始终放在一组围栏中,并被标记为数据;围栏比文档中任何连续反引号都长。
完整协议见 llms.txt,把它交给 AI,AI 就能为你的系统建立连接。其他 giit 编辑器的用法相同,只是各有自己的前缀,见 editors.giit.ch。
以只读方式显示文档(查看器)
?viewer=1 只显示完成的文档:没有代码,没有工具,无法更改任何内容,也不会在浏览器中保存任何内容。文档来自链接(#b64=、?src=)、你的页面通过 xmleditor:load 发送,或以普通表单 POST 到 post.php:字段 content 为文本或 b64 为 base64,可选 name,文本最多 20 MB。
<form method="post" action="https://xml.giit.ch/post.php?viewer=1" target="_blank">
<input type="hidden" name="lang" value="zh">
<input type="hidden" name="name" value="Report">
<input type="hidden" name="b64" value="…">
<button>Open</button>
</form>
不带 viewer=1 时,文档在普通编辑器中打开。无论哪种方式,文档都只保留在内存中:不会保存到服务器,也不会进入“我的文档”。直接打开时,查看器会说明内容来自第三方。
带占位符的模板
如果你的服务器在使用前将占位符填入 XML,请为 {{name}}、{{#name}} … {{/name}} 和 {{^name}} … {{/name}} 添加 template=mustache,或为 <<name>> 和 <<?name>> … <</?name>> 添加 template=angle。问题列表将每个标签识别为一个占位符,并报告未闭合的区块、多余的结束标签、顺序错误的结束标签以及带有未知符号的标签;代码按原样显示这些标签。含有占位符的 id 或命名空间前缀会被视为稍后填入,格式化会保留每个标签。
发送占位符及其示例,树就会显示填入示例后的文档:区块按照示例中的状态显示,列表按示例的每一行显示一次。没有示例的占位符会在方括号中显示其标签,例如 [Name]。树栏中的模板开关显示以文本呈现标签的树,示例会恢复显示示例;代码下方的占位符框可在光标处插入标签。点击一行会在代码中选中其节点,列表的每一行也一样;显示示例的部分在代码中编辑。示例以文本显示,只保留在内存中,绝不会传到你的页面:保存、change、get、Webhook 和所有导出都保留原样的模板。
frame.contentWindow.postMessage({ type: 'xmleditor:template',
placeholders: [
{ name: 'name', kind: 'value', label: 'Name', example: 'John Doe' },
{ name: 'hasNote', kind: 'section', label: 'With a note', shown: true },
{ name: 'items', kind: 'list', label: 'Items',
fields: [ { name: 'label', label: 'Item' }, { name: 'quantity', label: 'Quantity' } ],
rows: [ { label: 'Chair', quantity: '2' }, { label: 'Table', quantity: '1' } ] } ] }, ORIGIN);
// or the same object as the field template of xmleditor:load; { type: 'xmleditor:template', clear: true } ends it
<iframe src="https://xml.giit.ch/zh/?embed=1&template=mustache" allow="clipboard-write" style="width:100%;height:800px;border:0"></iframe>