support custom model
This commit is contained in:
@@ -29,6 +29,10 @@
|
|||||||
<label class="block text-xs text-gray-500">Apps Script 链接</label>
|
<label class="block text-xs text-gray-500">Apps Script 链接</label>
|
||||||
<input type="text" id="scriptUrl" class="w-full mt-1 border border-gray-300 rounded px-2 py-1 text-sm focus:ring-1 focus:ring-indigo-500 outline-none" placeholder="输入 Google Apps Script 链接">
|
<input type="text" id="scriptUrl" class="w-full mt-1 border border-gray-300 rounded px-2 py-1 text-sm focus:ring-1 focus:ring-indigo-500 outline-none" placeholder="输入 Google Apps Script 链接">
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block text-xs text-gray-500">AI 模型名称</label>
|
||||||
|
<input type="text" id="modelInput" class="w-full mt-1 border border-gray-300 rounded px-2 py-1 text-sm focus:ring-1 focus:ring-indigo-500 outline-none" placeholder="例如:gemini-1.5-flash">
|
||||||
|
</div>
|
||||||
<button id="saveConfig" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white text-sm py-2 rounded transition-colors">保存设置</button>
|
<button id="saveConfig" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white text-sm py-2 rounded transition-colors">保存设置</button>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
22
sidepanel.js
22
sidepanel.js
@@ -1,6 +1,7 @@
|
|||||||
// DOM Elements
|
// DOM Elements
|
||||||
const apiKeyInput = document.getElementById('apiKey');
|
const apiKeyInput = document.getElementById('apiKey');
|
||||||
const scriptUrlInput = document.getElementById('scriptUrl');
|
const scriptUrlInput = document.getElementById('scriptUrl');
|
||||||
|
const modelInput = document.getElementById('modelInput');
|
||||||
const saveConfigBtn = document.getElementById('saveConfig');
|
const saveConfigBtn = document.getElementById('saveConfig');
|
||||||
const extractLaptopBtn = document.getElementById('extractLaptop');
|
const extractLaptopBtn = document.getElementById('extractLaptop');
|
||||||
const extractPeripheralBtn = document.getElementById('extractPeripheral');
|
const extractPeripheralBtn = document.getElementById('extractPeripheral');
|
||||||
@@ -20,7 +21,7 @@ toggleConfig.addEventListener('click', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Load settings on startup
|
// Load settings on startup
|
||||||
chrome.storage.local.get(['geminiApiKey', 'googleScriptUrl'], (data) => {
|
chrome.storage.local.get(['geminiApiKey', 'googleScriptUrl', 'geminiModel'], (data) => {
|
||||||
if (data.geminiApiKey) {
|
if (data.geminiApiKey) {
|
||||||
apiKeyInput.value = data.geminiApiKey;
|
apiKeyInput.value = data.geminiApiKey;
|
||||||
// Fold by default if already configured
|
// Fold by default if already configured
|
||||||
@@ -32,13 +33,19 @@ chrome.storage.local.get(['geminiApiKey', 'googleScriptUrl'], (data) => {
|
|||||||
configChevron.classList.add('rotate-180');
|
configChevron.classList.add('rotate-180');
|
||||||
}
|
}
|
||||||
if (data.googleScriptUrl) scriptUrlInput.value = data.googleScriptUrl;
|
if (data.googleScriptUrl) scriptUrlInput.value = data.googleScriptUrl;
|
||||||
|
if (data.geminiModel) modelInput.value = data.geminiModel;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save settings
|
// Save settings
|
||||||
saveConfigBtn.addEventListener('click', () => {
|
saveConfigBtn.addEventListener('click', () => {
|
||||||
const apiKey = apiKeyInput.value.trim();
|
const apiKey = apiKeyInput.value.trim();
|
||||||
const scriptUrl = scriptUrlInput.value.trim();
|
const scriptUrl = scriptUrlInput.value.trim();
|
||||||
chrome.storage.local.set({ geminiApiKey: apiKey, googleScriptUrl: scriptUrl }, () => {
|
const model = modelInput.value.trim();
|
||||||
|
chrome.storage.local.set({
|
||||||
|
geminiApiKey: apiKey,
|
||||||
|
googleScriptUrl: scriptUrl,
|
||||||
|
geminiModel: model
|
||||||
|
}, () => {
|
||||||
updateStatus('已保存', 'bg-green-500 text-white');
|
updateStatus('已保存', 'bg-green-500 text-white');
|
||||||
setTimeout(() => updateStatus('待机', 'bg-gray-200 text-gray-600'), 2000);
|
setTimeout(() => updateStatus('待机', 'bg-gray-200 text-gray-600'), 2000);
|
||||||
});
|
});
|
||||||
@@ -50,15 +57,16 @@ extractPeripheralBtn.addEventListener('click', () => handleExtraction('periphera
|
|||||||
sendCustomBtn.addEventListener('click', () => handleExtraction('custom', customInput.value));
|
sendCustomBtn.addEventListener('click', () => handleExtraction('custom', customInput.value));
|
||||||
|
|
||||||
async function handleExtraction(type, customText = '') {
|
async function handleExtraction(type, customText = '') {
|
||||||
const { geminiApiKey, googleScriptUrl } = await chrome.storage.local.get(['geminiApiKey', 'googleScriptUrl']);
|
const { geminiApiKey, googleScriptUrl, geminiModel } = await chrome.storage.local.get(['geminiApiKey', 'googleScriptUrl', 'geminiModel']);
|
||||||
|
|
||||||
if (!geminiApiKey) {
|
if (!geminiApiKey) {
|
||||||
alert('请先输入 Gemini API 密钥。');
|
alert('请先输入 Gemini API 密钥。');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const selectedModel = geminiModel || 'gemini-1.5-flash';
|
||||||
updateStatus('提取中...', 'bg-blue-500 text-white');
|
updateStatus('提取中...', 'bg-blue-500 text-white');
|
||||||
resultsArea.textContent = '正在读取页面内容...';
|
resultsArea.textContent = `正在使用 ${selectedModel} 读取内容...`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. Get current tab content
|
// 1. Get current tab content
|
||||||
@@ -89,7 +97,7 @@ Title: ${pageData.title}`;
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. Call Gemini API
|
// 3. Call Gemini API
|
||||||
const geminiResult = await callGemini(geminiApiKey, systemPrompt, pageData.text, userPrompt);
|
const geminiResult = await callGemini(geminiApiKey, selectedModel, systemPrompt, pageData.text, userPrompt);
|
||||||
const cleanedJson = parseGeminiJson(geminiResult);
|
const cleanedJson = parseGeminiJson(geminiResult);
|
||||||
|
|
||||||
resultsArea.textContent = JSON.stringify(cleanedJson, null, 2);
|
resultsArea.textContent = JSON.stringify(cleanedJson, null, 2);
|
||||||
@@ -110,8 +118,8 @@ Title: ${pageData.title}`;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callGemini(apiKey, systemPrompt, contextText, userPrompt) {
|
async function callGemini(apiKey, model, systemPrompt, contextText, userPrompt) {
|
||||||
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key=${apiKey}`;
|
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`;
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
Reference in New Issue
Block a user