tkgshn
public.icon
https://scrapbox.io/files/615d2e0e039a8b001fda2920.png
高木俊輔(tkgshn)
Position
Skill
Interest
/icons/hr.icon
code:script.js
import '/api/code/tkgshn-extension/script_all/script.js';
code:script.js
scrapbox.PopupMenu.addButton({
title: "🤖",
onClick: () => {
const userQuote = window.getSelection().toString(); // ユーザーが選択したテキストを取得
console.log("userQuote", userQuote)
const userInput = prompt("ChatGPTに質問する:"); // promptウィンドウを空で開く
console.log("userInput", userInput)
if (userInput) { // もしuser inputが存在すれば、
const pageContent = scrapbox.Page.lines.map(line => line.text).join('\n'); //ページの内容を結合
console.log("pageContent", pageContent)
console.log(window.askChatGPT); // これは関数が正しく定義されていれば、関数をログに出力します。
askChatGPT({ userInput, userQuote, pageContent }).then((response) => {
if (response.choices && response.choices.length > 0) {
const result = "ChatGPTからの回答" + '\n' + "" + response.choices[0].message.content.trim() + "";
console.log('GPT Response:', result);
//window.chatgpt_result = result; //回答はブラウザのalertで表示する
//alert(result);
// モーダルダイアログを作成
const modal = document.createElement('div');
modal.style.position = 'fixed';
modal.style.top = '50%';
modal.style.left = '50%';
modal.style.transform = 'translate(-50%, -50%)';
modal.style.backgroundColor = 'white';
modal.style.padding = '20px';
modal.style.zIndex = '1000';
modal.style.borderRadius = '5px';
modal.style.boxShadow = '0 4px 6px rgba(0, 0, 0, 0.1)';
// 結果を表示するテキストエリア
const textArea = document.createElement('textarea');
textArea.value = result;
textArea.style.width = '600px';
textArea.style.height = '400px';
// モーダルを閉じるボタン
const closeButton = document.createElement('button');
closeButton.textContent = '閉じる';
closeButton.onclick = function() {
document.body.removeChild(modal);
};
modal.appendChild(textArea);
modal.appendChild(closeButton);
document.body.appendChild(modal);
} else {
console.error('GPT Response Error:', response);
alert('エラーが発生しました。');
}
});
}
},
});