Skip to content

Commit

Permalink
Update version to 1.10.2, add context menu for copying mathematical f…
Browse files Browse the repository at this point in the history
…ormulas, and enhance sidebar styles

- Bumped version number in manifest.json to 1.10.2.
- Introduced a new context menu item for copying mathematical formulas in index.html.
- Updated sidebar.css to improve context menu visibility and styling.
- Enhanced main.js to handle context menu interactions for MathJax elements, allowing users to copy LaTeX content directly.
- Added configuration for MathJax menu options to disable default context menu behavior.
  • Loading branch information
yym68686 committed Jan 18, 2025
1 parent 7e0deed commit 3038b90
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
</svg>
停止更新
</div>
<div class="context-menu-item" id="copy-math">
<svg viewBox="0 0 24 24">
<path fill="currentColor" d="M6 19.5c.2 0 .32-.08.45-.25l5.75-8.5 5.65-8.2c.08-.1.1-.2.1-.28 0-.23-.2-.38-.43-.38-.2 0-.33.06-.48.28l-5.65 8.2-5.75 8.5c-.08.1-.1.2-.1.29 0 .24.2.34.46.34zM5.2 8.1v2.1c0 .27.17.42.41.42.23 0 .41-.15.41-.42V8.1h2c.27 0 .41-.17.41-.41 0-.23-.14-.41-.41-.41h-2V4.6c0-.27-.18-.42-.41-.42-.24 0-.41.15-.41.42v2.68h-2c-.27 0-.41.18-.41.41 0 .24.14.41.41.41h2zm9.2 8.6c0 .23.15.4.35.4h5.1c.27 0 .41-.17.41-.4 0-.23-.14-.4-.41-.4h-5.1c-.2 0-.35.17-.35.4z"/>
</svg>
复制公式
</div>
</div>
<div id="input-container">
<button id="settings-button">
Expand Down
5 changes: 5 additions & 0 deletions lib/mathjax-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ window.MathJax = {
},
chtml: {
fontURL: 'lib/fonts/woff-v2'
},
menuOptions: {
settings: {
contextMenu: false
}
}
};
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cerebr",
"version": "1.10.1",
"version": "1.10.2",
"description": "Cerebr - 智能AI聊天助手",
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
Expand Down
9 changes: 7 additions & 2 deletions sidebar.css
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,16 @@ input:checked + .slider:before {
padding: 6px;
min-width: 140px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
z-index: 1000;
z-index: 2147483647;
display: none;
backdrop-filter: blur(var(--cerebr-blur-radius));
-webkit-backdrop-filter: blur(var(--cerebr-blur-radius));
border: 1px solid rgba(255, 255, 255, 0.1);
touch-action: none; /* 防止触摸事件引起页面滚动 */
touch-action: none;
}

#context-menu.visible {
display: block;
}

.context-menu-item {
Expand All @@ -797,6 +801,7 @@ input:checked + .slider:before {
border-radius: 6px;
margin: 2px 0;
transition: background-color 0.2s ease;
white-space: nowrap;
}

.context-menu-item:hover {
Expand Down
111 changes: 111 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,17 @@ document.addEventListener('DOMContentLoaded', async () => {
currentMessageElement = messageElement;
currentCodeElement = codeElement;

// 获取菜单元素
const copyMessageButton = document.getElementById('copy-message');
const copyCodeButton = document.getElementById('copy-code');
const copyMathButton = document.getElementById('copy-math');
const stopUpdateButton = document.getElementById('stop-update');

// 根据右键点击的元素类型显示/隐藏相应的菜单项
copyMessageButton.style.display = 'flex';
copyCodeButton.style.display = codeElement ? 'flex' : 'none';
copyMathButton.style.display = 'none'; // 默认隐藏复制公式按钮
stopUpdateButton.style.display = messageElement.classList.contains('updating') ? 'flex' : 'none';

showContextMenu({
event: e,
Expand Down Expand Up @@ -932,6 +940,109 @@ document.addEventListener('DOMContentLoaded', async () => {
}
});

document.addEventListener('contextmenu', (event) => {
// 检查是否点击了 MathJax 3 的任何元素
const isMathElement = (element) => {
const isMjx = element.tagName && element.tagName.toLowerCase().startsWith('mjx-');
const hasContainer = element.closest('mjx-container') !== null;
return isMjx || hasContainer;
};

if (isMathElement(event.target)) {
event.preventDefault();
event.stopPropagation();

// 获取最外层的 mjx-container
const container = event.target.closest('mjx-container');

if (container) {
const mathContextMenu = document.getElementById('copy-math');
const copyMessageButton = document.getElementById('copy-message');
const copyCodeButton = document.getElementById('copy-code');
const stopUpdateButton = document.getElementById('stop-update');

if (mathContextMenu) {
// 设置菜单项的显示状态
mathContextMenu.style.display = 'flex';
copyMessageButton.style.display = 'flex'; // 显示复制消息按钮
copyCodeButton.style.display = 'none';
stopUpdateButton.style.display = 'none';

// 获取包含公式的 AI 消息元素
const aiMessage = container.closest('.ai-message');
currentMessageElement = aiMessage; // 设置当前消息元素为 AI 消息

// 调用 showContextMenu 函数
showContextMenu({
event,
messageElement: aiMessage, // 使用 AI 消息元素
contextMenu,
stopUpdateButton
});

// 设置数学公式内容
const assistiveMml = container.querySelector('mjx-assistive-mml');
let mathContent;

// 获取原始的 LaTeX 源码
const mjxTexElement = container.querySelector('script[type="math/tex; mode=display"]') ||
container.querySelector('script[type="math/tex"]');

if (mjxTexElement) {
mathContent = mjxTexElement.textContent;
} else {
// 如果找不到原始 LaTeX,尝试从 MathJax 内部存储获取
const mjxInternal = container.querySelector('mjx-math');
if (mjxInternal) {
const texAttr = mjxInternal.getAttribute('aria-label');
if (texAttr) {
// 移除 "TeX:" 前缀(如果有的话)
mathContent = texAttr.replace(/^TeX:\s*/, '');
}
}
}

// 如果还是没有找到,尝试其他方法
if (!mathContent) {
if (assistiveMml) {
const texAttr = assistiveMml.getAttribute('aria-label');
if (texAttr) {
mathContent = texAttr.replace(/^TeX:\s*/, '');
}
}
}

mathContextMenu.dataset.mathContent = mathContent || container.textContent;
}
}
}
}, { capture: true, passive: false });

// 复制数学公式
document.getElementById('copy-math')?.addEventListener('click', async () => {
try {
// 获取数学公式内容
const mathContent = document.getElementById('copy-math').dataset.mathContent;

if (mathContent) {
await navigator.clipboard.writeText(mathContent);
console.log('数学公式已复制:', mathContent);

// 隐藏上下文菜单
hideContextMenu({
contextMenu,
onMessageElementReset: () => {
currentMessageElement = null;
}
});
} else {
console.error('没有找到可复制的数学公式内容');
}
} catch (err) {
console.error('复制公式失败:', err);
}
});

// 点击复制按钮
copyMessageButton.addEventListener('click', () => {
copyMessageContent({
Expand Down

0 comments on commit 3038b90

Please sign in to comment.