Skip to content

Commit bcf13a1

Browse files
committed
增加AI自动生成插件的相关代码
1 parent 6352f12 commit bcf13a1

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

iframe/script/User_config/message.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,58 @@ async function activateECTAI() {
496496
}
497497
});
498498
}
499+
500+
/**
501+
* 调用后端服务,将用户提供的 JavaScript 代码注入到立创 EDA 扩展模板中,
502+
* 生成并下载修改后的 .eext 文件。
503+
*
504+
* @async
505+
* @function generateAndDownloadEext
506+
* @param {string} jsCode - 要注入到 index.html 中的 JavaScript 代码字符串。
507+
* 不得为空或仅包含空白字符。
508+
* @returns {Promise<void>} 无返回值。成功时触发浏览器下载 .eext 文件;
509+
* 失败时抛出错误(如网络错误、后端返回错误等)。
510+
* @throws {Error} 当 jsCode 无效、网络请求失败或后端返回错误状态时抛出。
511+
*
512+
* @example
513+
* try {
514+
* const userScript = "console.log('Hello from EDA extension!');";
515+
* await generateAndDownloadEext(userScript);
516+
* } catch (err) {
517+
* console.error('生成扩展失败:', err.message);
518+
* }
519+
*/
520+
async function generateAndDownloadEext(jsCode) {
521+
if (!jsCode || typeof jsCode !== 'string' || !jsCode.trim()) {
522+
throw new Error('jsCode must be a non-empty string');
523+
}
524+
525+
const response = await fetch('http://localhost:5000/generate-eext', {
526+
method: 'POST',
527+
headers: {
528+
'Content-Type': 'application/json',
529+
},
530+
body: JSON.stringify({ js_code: jsCode.trim() }),
531+
});
532+
533+
if (!response.ok) {
534+
let errorMessage = `请求失败: ${response.status} ${response.statusText}`;
535+
try {
536+
const errorData = await response.json();
537+
errorMessage = errorData.error || errorMessage;
538+
} catch (e) {
539+
// 忽略 JSON 解析错误,使用默认消息
540+
}
541+
throw new Error(errorMessage);
542+
}
543+
544+
const blob = await response.blob();
545+
const url = window.URL.createObjectURL(blob);
546+
const a = document.createElement('a');
547+
a.href = url;
548+
a.download = 'modified_tool.eext';
549+
document.body.appendChild(a);
550+
a.click();
551+
window.URL.revokeObjectURL(url);
552+
document.body.removeChild(a);
553+
}

0 commit comments

Comments
 (0)