// ==UserScript== // @name ChatGPT: 英文翻譯工具 // @description 自動修改textarea後送出、論文翻譯、語句更改、文法檢查、字典 // @version 1.1.0 // @source https://github.com/iewihc/GPTTranslateScript/blob/main/userSrcipt.js // @namespace https://github.com/iewihc/GPTTranslateScript/blob/main/userSrcipt.js // @website https://fullstackladder.dev/ // @author Chi-Wei Lin // @run-at document-end // @license MIT // @match *://chat.openai.com/chat* // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com // @downloadURL none // ==/UserScript== // 預設要顯示的按鈕和文字範本 const fillAndSubmitText = (test) => { // 取得 textarea 元素並設定 value const textarea = document.querySelector("textarea"); // 觸發 input 事件 textarea.value = test; // 取得送出按鈕並點擊 textarea.dispatchEvent(new Event("input", { bubbles: true })); const button = textarea.parentElement.querySelector("button:last-child"); button.click(); }; const defaultManualSubmitText = [ { text: '翻譯論文', template:`從現在開始我會陸續給您文章, 並分成三個部份回答我,每次回答完請直接忽略之前傳的內容,保持一致的回覆格式 第一個部份請幫我翻譯成流暢的繁體中文, 第二個部份使用項目符號幫我列出繁體中文和英文的摘要,五個項目就可以了,中文與英文麻煩幫我同時列在同一個點上,其格式為:中文句子 (English Sentence)。 第三個部份請你幫我整理文章中除了地名和人名之外的專業術語,也應包括英文和繁體中文,用逗號分隔。比如說:蘋果 (Apple), 香蕉 (Banana)的格式。 文章內容如下: {replace_text} ` }, { text: 'PARAPHRASE', template: `請你幫我使用英文Paraphrase這段句子,並使用項目符號說明您修改的內容: {{replace_text}} `}, { text: 'DICTIONARY', template: `請您幫我列出這個單字的中文和英文並且 1. 該單字的兩個例句,例句需要包含繁體中文和英文 2. 該單字的五個vocabulary collocations用法 2. 該單字不同詞性,包含其名詞、代名詞、形容詞、動詞、副詞 3. 該單字同義詞和反義詞,單字為:{replace_text} ` }, { text: 'GRAMMAR', template:`請幫此段 {{replace_text}} 1. 翻譯成繁體中文 2. 請修正這段句子的文法錯誤,並使用項目符號說明您修改的內容 3. 請使用英語,將此句翻寫為更加學術` }, { text: 'TESTCASE', template: `你現在是一個程式語言專家,我有一段程式碼 {{replace_text}} ,請幫我寫一個測試,請至少提供五個測試案例,同時要包含到極端的狀況,讓我能夠確定這段程式碼的輸出是正確的。`, }, { text: 'REFACTOR', template: `你現在是一個 Clean Code 專家,我有以下的程式碼,請用更乾淨簡潔的方式改寫,讓我的同事們可以更容易維護程式碼。另外,也解釋為什麼你要這樣重構,讓我能把重構的方式的說明加到 Pull Request 當中。 {{replace_text}`, } ]; // 透過傳入的文字來替換 textarea 中的指定文字後再提交 const addTextToTextarea = (test) => { const textarea = document.querySelector("textarea"); const text = textarea.value; const newText = test.replace('{replace_text}', text); fillAndSubmitText(newText); }; // 新增底部按鈕,並設定格式,點擊按鈕時會將對應的文字插入到 textarea 中 const addBottomButtonAndFormatting = ()=>{ setTimeout(function() { // 建立包含按鈕的容器 let btnDivContainer = document.createElement('div'); // 建立每個按鈕的 HTML 元素並設定樣式、文字、點擊事件 defaultManualSubmitText.forEach((item) => { const button = document.createElement("button"); button.style.border = "1px solid #d1d5db"; button.style.borderRadius = "5px"; button.style.padding = "0.5rem 1rem"; button.style.margin = "0.5rem"; button.innerText = item.text; button.addEventListener('click', function() { addTextToTextarea(item.template); }); btnDivContainer.append(button); }); // 找到底部的 div 元素,如果存在就清空原有內容,並加入按鈕容器 const bottomDiv = document.querySelector('.px-3.pt-2.pb-3.text-center.text-xs.text-black\\/50.dark\\:text-white\\/50.md\\:px-4.md\\:pt-3.md\\:pb-6'); if (bottomDiv) { bottomDiv.innerHTML = ''; bottomDiv.appendChild(btnDivContainer); } }, 5000) } (function () { "use strict"; addBottomButtonAndFormatting(); })();