我们在跟AI对话过程中,有时候需要发给AI表格,处理起来太麻烦,我就想着,随便让Claude写个浏览器插件的源码吧,谁知,改了1次,就能用了,太牛了。
创建一个新文件夹,将下方代码分别保存为以下文件:
manifest.json
popup.html
popup.js
content.js
在Chrome浏览器中:
打开扩展管理页面(chrome://extensions/)
开启"开发者模式"
点击"加载已解压的扩展程序"
选择你创建的文件夹
content.js
// content.js chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === "convertTable") { try { const selectedTable = getSelectedTable(); if (!selectedTable) { sendResponse({ success: false, error: "No table selected" }); return; }
const markdown = convertTableToMarkdown(selectedTable); copyToClipboard(markdown); sendResponse({ success: true }); } catch (error) { sendResponse({ success: false, error: error.message }); } } return true; });
function getSelectedTable() { // 尝试获取用户选中的表格 const selection = window.getSelection(); let node = selection.anchorNode; while (node && node.nodeName !== 'TABLE') { node = node.parentNode; } return node; }
function convertTableToMarkdown(table) { const rows = table.querySelectorAll('tr'); let markdown = ''; rows.forEach((row, rowIndex) => { const cells = row.querySelectorAll('td, th'); cells.forEach((cell, colIndex) => { // 根据列号添加相应数量的#号 const hashes = '#'.repeat(colIndex + 1); const cellText = cell.textContent.trim(); markdown += `${hashes} ${cellText}\n`; }); markdown += '\n'; }); return markdown; }
function copyToClipboard(text) { navigator.clipboard.writeText(text).catch(err => { console.error('Failed to copy text: ', err); }); }
manifest.json
{
“manifest_version”: 3,
“name”: “Table to Markdown Converter”,
“version”: “1.0”,
“description”: “Convert table content to markdown format”,
“permissions”: [“clipboardWrite”],
“action”: {
“default_popup”: “popup.html”
}
}
popup.html
body { width: 400px; padding: 15px; font-family: Arial, sans-serif; } #input-area { width: 100%; height: 200px; margin: 10px 0; padding: 8px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; resize: vertical; } #output-area { width: 100%; height: 200px; margin: 10px 0; padding: 8px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; background-color: #f9f9f9; resize: vertical; } button { width: 100%; padding: 8px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #45a049; } .label { font-weight: bold; margin: 5px 0; }
粘贴表格内容: 转换为Markdown 转换结果:
popup.js
document.getElementById(‘convertBtn’).addEventListener(‘click’, () => {
const input = document.getElementById(‘input-area’).value;
const outputArea = document.getElementById(‘output-area’);
if (!input.trim()) {
outputArea.value = ‘请先粘贴表格内容’;
return;
}
try {
// 将输入的文本按行分割
const rows = input.trim().split(‘\n’);
let markdown = ‘’;
rows.forEach(row => { // 将每行按制表符或多个空格分割 const cells = row.split(/\t| +/); cells.forEach((cell, index) => { // 根据列号添加对应数量的#号 const hashes = '#'.repeat(index + 1); const cellText = cell.trim(); if (cellText) { markdown += `${hashes} ${cellText}\n`; } }); markdown += '\n'; });
// 显示结果 outputArea.value = markdown;
// 自动复制到剪贴板 navigator.clipboard.writeText(markdown) .then(() => { outputArea.value += '\n(已自动复制到剪贴板!)'; }) .catch(err => { outputArea.value += '\n(复制到剪贴板失败,请手动复制)'; });
} catch (error) {
outputArea.value = '转换出错: ’ + error.message;
}
});
// 添加自动调整文本框高度
function autoResize(textarea) {
textarea.style.height = ‘auto’;
textarea.style.height = textarea.scrollHeight + ‘px’;
}
document.getElementById(‘input-area’).addEventListener(‘input’, function() {
autoResize(this);
});
|