feat(debug): 报错页面增加AI友好格式和可配置一键复制功能

This commit is contained in:
augushong
2026-05-24 08:55:23 +08:00
parent 218810a7db
commit 0225e65975
2 changed files with 934 additions and 1 deletions

View File

@@ -0,0 +1,933 @@
<?php
/** @var array $traces */
if (!function_exists('parse_padding')) {
function parse_padding($source)
{
$length = strlen(strval(count($source['source']) + $source['first']));
return 40 + ($length - 1) * 8;
}
}
if (!function_exists('parse_class')) {
function parse_class($name)
{
$names = explode('\\', $name);
return '<abbr title="'.$name.'">'.end($names).'</abbr>';
}
}
if (!function_exists('parse_file')) {
function parse_file($file, $line)
{
return '<a class="toggle" title="'."{$file} line {$line}".'">'.basename($file)." line {$line}".'</a>';
}
}
if (!function_exists('parse_args')) {
function parse_args($args)
{
$result = [];
foreach ($args as $key => $item) {
switch (true) {
case is_object($item):
$value = sprintf('<em>object</em>(%s)', parse_class(get_class($item)));
break;
case is_array($item):
if (count($item) > 3) {
$value = sprintf('[%s, ...]', parse_args(array_slice($item, 0, 3)));
} else {
$value = sprintf('[%s]', parse_args($item));
}
break;
case is_string($item):
if (strlen($item) > 20) {
$value = sprintf(
'\'<a class="toggle" title="%s">%s...</a>\'',
htmlentities($item),
htmlentities(substr($item, 0, 20))
);
} else {
$value = sprintf("'%s'", htmlentities($item));
}
break;
case is_int($item):
case is_float($item):
$value = $item;
break;
case is_null($item):
$value = '<em>null</em>';
break;
case is_bool($item):
$value = '<em>' . ($item ? 'true' : 'false') . '</em>';
break;
case is_resource($item):
$value = '<em>resource</em>';
break;
default:
$value = htmlentities(str_replace("\n", '', var_export(strval($item), true)));
break;
}
$result[] = is_int($key) ? $value : sprintf('\'%s\' => %s', htmlentities($key), $value);
}
return implode(', ', $result);
}
}
if (!function_exists('echo_value')) {
function echo_value($val)
{
if (is_array($val) || is_object($val)) {
echo htmlentities(json_encode($val, JSON_PRETTY_PRINT));
} elseif (is_bool($val)) {
echo $val ? 'true' : 'false';
} elseif (is_scalar($val)) {
echo htmlentities($val);
} else {
echo 'Resource';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系统发生错误</title>
<meta name="robots" content="noindex,nofollow" />
<style>
/* Base */
body {
color: #333;
font: 16px Verdana, "Helvetica Neue", helvetica, Arial, 'Microsoft YaHei', sans-serif;
margin: 0;
padding: 0 20px 20px;
}
h1{
margin: 10px 0 0;
font-size: 28px;
font-weight: 500;
line-height: 32px;
}
h2{
color: #4288ce;
font-weight: 400;
padding: 6px 0;
margin: 6px 0 0;
font-size: 18px;
border-bottom: 1px solid #eee;
}
h3{
margin: 12px;
font-size: 16px;
font-weight: bold;
}
abbr{
cursor: help;
text-decoration: underline;
text-decoration-style: dotted;
}
a{
color: #868686;
cursor: pointer;
}
a:hover{
text-decoration: underline;
}
.line-error{
background: #f8cbcb;
}
.echo table {
width: 100%;
}
.echo pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #f7f7f7;
border: 0;
border-radius: 3px;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
.echo pre > pre {
padding: 0;
margin: 0;
}
/* Exception Info */
.exception {
margin-top: 20px;
}
.exception .message{
padding: 12px;
border: 1px solid #ddd;
border-bottom: 0 none;
line-height: 18px;
font-size:16px;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
font-family: Consolas,"Liberation Mono",Courier,Verdana,"微软雅黑",serif;
}
.exception .code{
float: left;
text-align: center;
color: #fff;
margin-right: 12px;
padding: 16px;
border-radius: 4px;
background: #999;
}
.exception .source-code{
padding: 6px;
border: 1px solid #ddd;
background: #f9f9f9;
overflow-x: auto;
}
.exception .source-code pre{
margin: 0;
}
.exception .source-code pre ol{
margin: 0;
color: #4288ce;
display: inline-block;
min-width: 100%;
box-sizing: border-box;
font-size:14px;
font-family: "Century Gothic",Consolas,"Liberation Mono",Courier,Verdana,serif;
padding-left: <?php echo (isset($source) && !empty($source)) ? parse_padding($source) : 40; ?>px;
}
.exception .source-code pre li{
border-left: 1px solid #ddd;
height: 18px;
line-height: 18px;
}
.exception .source-code pre code{
color: #333;
height: 100%;
display: inline-block;
border-left: 1px solid #fff;
font-size:14px;
font-family: Consolas,"Liberation Mono",Courier,Verdana,"微软雅黑",serif;
}
.exception .trace{
padding: 6px;
border: 1px solid #ddd;
border-top: 0 none;
line-height: 16px;
font-size:14px;
font-family: Consolas,"Liberation Mono",Courier,Verdana,"微软雅黑",serif;
}
.exception .trace h2:hover {
text-decoration: underline;
cursor: pointer;
}
.exception .trace ol{
margin: 12px;
}
.exception .trace ol li{
padding: 2px 4px;
}
.exception div:last-child{
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
/* Exception Variables */
.exception-var table{
width: 100%;
margin: 12px 0;
box-sizing: border-box;
table-layout:fixed;
word-wrap:break-word;
}
.exception-var table caption{
text-align: left;
font-size: 16px;
font-weight: bold;
padding: 6px 0;
}
.exception-var table caption small{
font-weight: 300;
display: inline-block;
margin-left: 10px;
color: #ccc;
}
.exception-var table tbody{
font-size: 13px;
font-family: Consolas, "Liberation Mono", Courier, "微软雅黑",serif;
}
.exception-var table td{
padding: 0 6px;
vertical-align: top;
word-break: break-all;
}
.exception-var table td:first-child{
width: 28%;
font-weight: bold;
white-space: nowrap;
}
.exception-var table td pre{
margin: 0;
}
/* Copyright Info */
.copyright{
margin-top: 24px;
padding: 12px 0;
border-top: 1px solid #eee;
}
/* SPAN elements with the classes below are added by prettyprint. */
pre.prettyprint .pln { color: #000 } /* plain text */
pre.prettyprint .str { color: #080 } /* string content */
pre.prettyprint .kwd { color: #008 } /* a keyword */
pre.prettyprint .com { color: #800 } /* a comment */
pre.prettyprint .typ { color: #606 } /* a type name */
pre.prettyprint .lit { color: #066 } /* a literal value */
/* punctuation, lisp open bracket, lisp close bracket */
pre.prettyprint .pun, pre.prettyprint .opn, pre.prettyprint .clo { color: #660 }
pre.prettyprint .tag { color: #008 } /* a markup tag name */
pre.prettyprint .atn { color: #606 } /* a markup attribute name */
pre.prettyprint .atv { color: #080 } /* a markup attribute value */
pre.prettyprint .dec, pre.prettyprint .var { color: #606 } /* a declaration; a variable name */
pre.prettyprint .fun { color: red } /* a function name */
/* AI Copy Toolbar */
#debug-toolbar {
position: fixed;
top: 10px;
right: 10px;
z-index: 99999;
background: rgba(255, 255, 255, 0.95);
padding: 8px 12px;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
font-size: 12px;
max-width: 90vw;
overflow-x: auto;
white-space: nowrap;
}
#debug-toolbar .toolbar-group {
display: inline-flex;
align-items: center;
gap: 4px;
margin-right: 10px;
padding-right: 10px;
border-right: 1px solid #ddd;
}
#debug-toolbar .toolbar-group:last-child {
border-right: none;
margin-right: 0;
padding-right: 0;
}
#debug-toolbar .toolbar-group label {
display: inline-flex;
align-items: center;
gap: 2px;
cursor: pointer;
padding: 2px 4px;
border-radius: 3px;
transition: background 0.15s;
font-size: 12px;
color: #555;
}
#debug-toolbar .toolbar-group label:hover {
background: #f0f0f0;
}
#debug-toolbar .toolbar-group label.disabled {
color: #ccc;
cursor: not-allowed;
}
#debug-toolbar .toolbar-group input[type="checkbox"] {
margin: 0;
cursor: pointer;
}
#debug-toolbar .toolbar-group label.disabled input[type="checkbox"] {
cursor: not-allowed;
}
#debug-toolbar button {
padding: 4px 10px;
cursor: pointer;
border: 1px solid #ccc;
background: #fff;
border-radius: 3px;
font-size: 12px;
transition: all 0.2s;
}
#debug-toolbar button:hover {
background: #f0f0f0;
border-color: #bbb;
}
#debug-toolbar button.active {
background: #4288ce;
color: white;
border-color: #4288ce;
}
#debug-toolbar button.copied {
background: #52c41a;
color: white;
border-color: #52c41a;
}
#markdown-preview {
display: none;
padding: 20px;
background: #fff;
position: relative;
margin-top: 20px;
border: 1px solid #ddd;
border-radius: 4px;
}
#markdown-preview pre {
white-space: pre-wrap;
word-break: break-word;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 13px;
background: #f8f9fa;
padding: 15px;
border-radius: 4px;
border: 1px solid #e8e8e8;
color: #333;
max-height: 600px;
overflow: auto;
}
</style>
</head>
<body>
<?php if (\think\facade\App::isDebug()) { ?>
<?php foreach ($traces as $index => $trace) { ?>
<div class="exception">
<div class="message">
<div class="info">
<div>
<h2><?php echo "#{$index} [{$trace['code']}]" . sprintf('%s in %s', parse_class($trace['name']), parse_file($trace['file'], $trace['line'])); ?></h2>
</div>
<div><h1><?php echo nl2br(htmlentities($trace['message'])); ?></h1></div>
</div>
</div>
<?php if (!empty($trace['source'])) { ?>
<div class="source-code">
<pre class="prettyprint lang-php"><ol start="<?php echo $trace['source']['first']; ?>"><?php foreach ((array) $trace['source']['source'] as $key => $value) { ?><li class="line-<?php echo "{$index}-"; echo $key + $trace['source']['first']; echo $trace['line'] === $key + $trace['source']['first'] ? ' line-error' : ''; ?>"><code><?php echo htmlentities($value); ?></code></li><?php } ?></ol></pre>
</div>
<?php }?>
<div class="trace">
<h2 data-expand="<?php echo 0 === $index ? '1' : '0'; ?>">Call Stack</h2>
<ol>
<li><?php echo sprintf('in %s', parse_file($trace['file'], $trace['line'])); ?></li>
<?php foreach ((array) $trace['trace'] as $value) { ?>
<li>
<?php
// Show Function
if ($value['function']) {
echo sprintf(
'at %s%s%s(%s)',
isset($value['class']) ? parse_class($value['class']) : '',
isset($value['type']) ? $value['type'] : '',
$value['function'],
isset($value['args'])?parse_args($value['args']):''
);
}
// Show line
if (isset($value['file']) && isset($value['line'])) {
echo sprintf(' in %s', parse_file($value['file'], $value['line']));
}
?>
</li>
<?php } ?>
</ol>
</div>
</div>
<?php } ?>
<?php } else { ?>
<div class="exception">
<div class="info"><h1><?php echo htmlentities($message); ?></h1></div>
</div>
<?php } ?>
<?php if (!empty($datas)) { ?>
<div class="exception-var">
<h2>Exception Datas</h2>
<?php foreach ((array) $datas as $label => $value) { ?>
<table>
<?php if (empty($value)) { ?>
<caption><?php echo $label; ?><small>empty</small></caption>
<?php } else { ?>
<caption><?php echo $label; ?></caption>
<tbody>
<?php foreach ((array) $value as $key => $val) { ?>
<tr>
<td><?php echo htmlentities($key); ?></td>
<td><?php echo_value($val); ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
</table>
<?php } ?>
</div>
<?php } ?>
<?php if (!empty($tables)) { ?>
<div class="exception-var">
<h2>Environment Variables</h2>
<?php foreach ((array) $tables as $label => $value) { ?>
<table>
<?php if (empty($value)) { ?>
<caption><?php echo $label; ?><small>empty</small></caption>
<?php } else { ?>
<caption><?php echo $label; ?></caption>
<tbody>
<?php foreach ((array) $value as $key => $val) { ?>
<tr>
<td><?php echo htmlentities($key); ?></td>
<td><?php echo_value($val); ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
</table>
<?php } ?>
</div>
<?php } ?>
<div class="copyright">
<a title="官方网站" href="http://www.thinkphp.cn">ThinkPHP</a>
<span>V<?php echo \think\facade\App::version(); ?></span>
<span>{ -API开发设计的高性能框架 }</span>
<span>- <a title="官方手册" href="https://doc.thinkphp.cn">官方手册</a></span>
</div>
<!-- AI Copy Toolbar -->
<div id="debug-toolbar">
<span class="toolbar-group" id="core-group">
<label><input type="checkbox" data-key="message" checked> 错误信息</label>
<label><input type="checkbox" data-key="location" checked> 出错位置</label>
<label><input type="checkbox" data-key="callstack" checked> 调用栈</label>
<label><input type="checkbox" data-key="source"> 源码</label>
<label data-dynamic="exception-data"><input type="checkbox" data-key="exceptiondata"> 异常数据</label>
</span>
<span class="toolbar-group" id="env-group">
<label data-dynamic="env" data-env-key="GET Data"><input type="checkbox" data-key="env_get"> GET</label>
<label data-dynamic="env" data-env-key="POST Data"><input type="checkbox" data-key="env_post"> POST</label>
<label data-dynamic="env" data-env-key="Files"><input type="checkbox" data-key="env_files"> Files</label>
<label data-dynamic="env" data-env-key="Cookies"><input type="checkbox" data-key="env_cookies"> Cookies</label>
<label data-dynamic="env" data-env-key="Session"><input type="checkbox" data-key="env_session"> Session</label>
<label data-dynamic="env" data-env-key="Server/Request Data"><input type="checkbox" data-key="env_server"> Server</label>
</span>
<span class="toolbar-group">
<button id="btn-copy" onclick="copyToClipboard()">复制给AI</button>
<button id="btn-preview" onclick="togglePreview()">预览</button>
<button id="btn-path" onclick="togglePathMode()">路径:简略</button>
</span>
</div>
<!-- Hidden root path -->
<div id="root-path" style="display:none;"><?php echo \think\facade\App::getRootPath(); ?></div>
<!-- Markdown Preview -->
<div id="markdown-preview">
<pre id="markdown-content"></pre>
</div>
<?php if (\think\facade\App::isDebug()) { ?>
<script>
function $(selector, node){
var elements;
node = node || document;
if(document.querySelectorAll){
elements = node.querySelectorAll(selector);
} else {
switch(selector.substr(0, 1)){
case '#':
elements = [node.getElementById(selector.substr(1))];
break;
case '.':
if(document.getElementsByClassName){
elements = node.getElementsByClassName(selector.substr(1));
} else {
elements = get_elements_by_class(selector.substr(1), node);
}
break;
default:
elements = node.getElementsByTagName();
}
}
return elements;
function get_elements_by_class(search_class, node, tag) {
var elements = [], eles,
pattern = new RegExp('(^|\\s)' + search_class + '(\\s|$)');
node = node || document;
tag = tag || '*';
eles = node.getElementsByTagName(tag);
for(var i = 0; i < eles.length; i++) {
if(pattern.test(eles[i].className)) {
elements.push(eles[i])
}
}
return elements;
}
}
$.getScript = function(src, func){
var script = document.createElement('script');
script.async = 'async';
script.src = src;
script.onload = func || function(){};
$('head')[0].appendChild(script);
}
;(function(){
var files = $('.toggle');
var ol = $('ol', $('.prettyprint')[0]);
var li = $('li', ol[0]);
//
for(var i = 0; i < files.length; i++){
files[i].ondblclick = function(){
var title = this.title;
this.title = this.innerHTML;
this.innerHTML = title;
}
}
(function () {
var expand = function (dom, expand) {
var ol = $('ol', dom.parentNode)[0];
expand = undefined === expand ? dom.attributes['data-expand'].value === '0' : undefined;
if (expand) {
dom.attributes['data-expand'].value = '1';
ol.style.display = 'none';
dom.innerText = 'Call Stack (展开)';
} else {
dom.attributes['data-expand'].value = '0';
ol.style.display = 'block';
dom.innerText = 'Call Stack (折叠)';
}
};
var traces = $('.trace');
for (var i = 0; i < traces.length; i ++) {
var h2 = $('h2', traces[i])[0];
expand(h2);
h2.onclick = function () {
expand(this);
};
}
})();
$.getScript('//cdn.bootcdn.net/ajax/libs/prettify/r298/prettify.min.js', function(){
prettyPrint();
});
})();
// PHP generates Markdown fragments as JS variables
var mdFragments = {};
<?php
// Message
if (isset($message)) {
$md = "# \u7cfb\u7edf\u53d1\u751f\u9519\u8bef\n\n## \u9519\u8bef\u4fe1\u606f\n> " . strip_tags((string)$message) . "\n\n";
echo 'mdFragments.message = ' . json_encode($md) . ";\n";
}
// Location
if (isset($file) && isset($line)) {
$fileEscaped = str_replace('\\', '\\\\', $file);
$md = "## \u51fa\u9519\u4f4d\u7f6e\n- **File:** `{$fileEscaped}`\n- **Line:** `{$line}`\n\n";
echo 'mdFragments.location = ' . json_encode($md) . ";\n";
}
// Call Stack (per trace)
if (isset($traces) && is_array($traces)) {
foreach ($traces as $index => $trace) {
$md = "## \u5f02\u5e38 #{$index}\n";
if (isset($trace['message'])) {
$md .= "**Message:** " . strip_tags($trace['message']) . "\n\n";
}
if (isset($trace['file']) && isset($trace['line'])) {
$md .= "**Location:** `{$trace['file']}` : {$trace['line']}\n\n";
}
if (!empty($trace['trace'])) {
$md .= "### \u8c03\u7528\u6808\n";
$count = 0;
$total = count($trace['trace']);
foreach ($trace['trace'] as $i => $item) {
if ($count >= 20) {
$md .= "... (\u5171 {$total} \u5c42\uff0c\u5df2\u622a\u65ad)\n";
break;
}
$func = $item['function'] ?? 'unknown';
$cls = $item['class'] ?? '';
$type = $item['type'] ?? '';
$f = $item['file'] ?? '';
$l = $item['line'] ?? '';
$call = $cls . $type . $func;
$location = $f ? " in `{$f}`:{$l}" : "";
$md .= ($count + 1) . ". `{$call}`{$location}\n";
$count++;
}
$md .= "\n";
}
echo "mdFragments.callstack = " . json_encode($md) . ";\n";
}
}
// Source Code
if (isset($traces) && is_array($traces)) {
foreach ($traces as $index => $trace) {
if (!empty($trace['source'])) {
$md = "### \u6e90\u7801\u7247\u6bb5\n```php\n";
foreach ((array) $trace['source']['source'] as $key => $value) {
$lineNum = $key + $trace['source']['first'];
$marker = ($trace['line'] === $lineNum) ? ' >>>' : '';
$md .= "{$lineNum}{$marker}: " . rtrim($value) . "\n";
}
$md .= "```\n\n";
echo "mdFragments.source = " . json_encode($md) . ";\n";
}
}
}
// Exception Data
if (isset($datas) && is_array($datas)) {
$hasData = false;
$md = "## \u5f02\u5e38\u6570\u636e\n";
foreach ($datas as $label => $value) {
if (!empty($value)) {
$hasData = true;
$md .= "### {$label}\n";
foreach ($value as $k => $v) {
$val_str = is_scalar($v) ? (string)$v : json_encode($v, JSON_UNESCAPED_UNICODE);
$md .= "- **{$k}:** `{$val_str}`\n";
}
$md .= "\n";
}
}
if ($hasData) {
echo "mdFragments.exceptiondata = " . json_encode($md) . ";\n";
echo "window.__hasExceptionData = true;\n";
}
}
// Environment Variables (6 separate blocks)
if (isset($tables) && is_array($tables)) {
$envMap = array(
'GET Data' => 'env_get',
'POST Data' => 'env_post',
'Files' => 'env_files',
'Cookies' => 'env_cookies',
'Session' => 'env_session',
'Server/Request Data' => 'env_server',
);
foreach ($envMap as $tableKey => $jsKey) {
if (isset($tables[$tableKey]) && !empty($tables[$tableKey])) {
$md = "### {$tableKey}\n";
foreach ($tables[$tableKey] as $k => $v) {
$val_str = is_scalar($v) ? (string)$v : json_encode($v, JSON_UNESCAPED_UNICODE);
if (strlen($val_str) > 200) {
$val_str = substr($val_str, 0, 200) . '...';
}
$md .= "- **{$k}:** `{$val_str}`\n";
}
$md .= "\n";
echo "mdFragments.{$jsKey} = " . json_encode($md) . ";\n";
echo "window.__hasEnv_{$jsKey} = true;\n";
}
}
}
?>
// AI Copy Toolbar JS
(function() {
var STORAGE_KEY = 'think_debug_copy_options';
var currentPathMode = 'simple';
var rootPath = '';
var rootPathEscaped = '';
// Initialize on DOM ready
function initToolbar() {
var rootDiv = document.getElementById('root-path');
if (rootDiv) {
rootPath = rootDiv.innerText.trim();
rootPathEscaped = rootPath.replace(/\\/g, '\\\\');
}
restoreCheckboxState();
updateDisabledState();
}
// Run init on DOMContentLoaded or immediately if already loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initToolbar);
} else {
initToolbar();
}
// Restore checkbox state from localStorage
function restoreCheckboxState() {
try {
var saved = localStorage.getItem(STORAGE_KEY);
if (saved) {
var state = JSON.parse(saved);
var checkboxes = document.querySelectorAll('#debug-toolbar input[type="checkbox"]');
checkboxes.forEach(function(cb) {
var key = cb.getAttribute('data-key');
if (key && state.hasOwnProperty(key)) {
cb.checked = state[key];
}
});
}
} catch(e) {}
}
// Save checkbox state to localStorage
function saveCheckboxState() {
try {
var state = {};
var checkboxes = document.querySelectorAll('#debug-toolbar input[type="checkbox"]');
checkboxes.forEach(function(cb) {
var key = cb.getAttribute('data-key');
if (key) {
state[key] = cb.checked;
}
});
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
} catch(e) {}
}
// Update disabled state for env checkboxes based on data availability
function updateDisabledState() {
// Exception data
var exDataLabel = document.querySelector('label[data-dynamic="exception-data"]');
if (exDataLabel && !window.__hasExceptionData) {
exDataLabel.classList.add('disabled');
var cb = exDataLabel.querySelector('input');
if (cb) { cb.checked = false; cb.disabled = true; }
}
// Env vars
var envLabels = document.querySelectorAll('label[data-dynamic="env"]');
var envKeyMap = {
'GET Data': 'env_get',
'POST Data': 'env_post',
'Files': 'env_files',
'Cookies': 'env_cookies',
'Session': 'env_session',
'Server/Request Data': 'env_server'
};
envLabels.forEach(function(label) {
var tableKey = label.getAttribute('data-env-key');
var jsKey = envKeyMap[tableKey];
if (jsKey && !window['__hasEnv_' + jsKey]) {
label.classList.add('disabled');
var cb = label.querySelector('input');
if (cb) { cb.checked = false; cb.disabled = true; }
}
});
}
// Generate Markdown based on current checkbox state
function generateMarkdown() {
var parts = [];
var checkboxes = document.querySelectorAll('#debug-toolbar input[type="checkbox"]:not(:disabled)');
checkboxes.forEach(function(cb) {
if (cb.checked) {
var key = cb.getAttribute('data-key');
if (key && mdFragments[key]) {
parts.push(mdFragments[key]);
}
}
});
var content = parts.join('\n');
// Path replacement
if (currentPathMode === 'simple' && rootPathEscaped) {
var regex = new RegExp(rootPathEscaped, 'gi');
content = content.replace(regex, '.../');
}
return content;
}
// Copy to clipboard
window.copyToClipboard = function() {
var content = generateMarkdown();
if (!content.trim()) {
alert('\u8bf7\u81f3\u5c11\u52fe\u9009\u4e00\u4e2a\u5185\u5bb9\u9009\u9879');
return;
}
var btn = document.getElementById('btn-copy');
if (navigator.clipboard) {
navigator.clipboard.writeText(content).then(function() {
btn.textContent = '\u5df2\u590d\u5236';
btn.classList.add('copied');
setTimeout(function() {
btn.textContent = '\u590d\u5236\u7ed9AI';
btn.classList.remove('copied');
}, 2000);
}, function(err) {
alert('\u590d\u5236\u5931\u8d25: ' + err);
});
} else {
var textarea = document.createElement('textarea');
textarea.value = content;
textarea.style.position = 'fixed';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
btn.textContent = '\u5df2\u590d\u5236';
btn.classList.add('copied');
setTimeout(function() {
btn.textContent = '\u590d\u5236\u7ed9AI';
btn.classList.remove('copied');
}, 2000);
} catch (err) {
alert('\u590d\u5236\u5931\u8d25');
}
document.body.removeChild(textarea);
}
saveCheckboxState();
};
// Toggle Markdown preview
window.togglePreview = function() {
var preview = document.getElementById('markdown-preview');
var btn = document.getElementById('btn-preview');
if (preview.style.display === 'none' || !preview.style.display) {
var content = generateMarkdown();
document.getElementById('markdown-content').textContent = content;
preview.style.display = 'block';
btn.textContent = '\u5173\u95ed\u9884\u89c8';
btn.classList.add('active');
} else {
preview.style.display = 'none';
btn.textContent = '\u9884\u89c8';
btn.classList.remove('active');
}
};
// Toggle path mode
window.togglePathMode = function() {
var btn = document.getElementById('btn-path');
if (currentPathMode === 'simple') {
currentPathMode = 'full';
btn.textContent = '\u8def\u5f84:\u5b8c\u6574';
btn.classList.add('active');
} else {
currentPathMode = 'simple';
btn.textContent = '\u8def\u5f84:\u7b80\u7565';
btn.classList.remove('active');
}
};
})();
</script>
<?php } ?>
</body>
</html>