-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
137 lines (126 loc) · 5.26 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get('power', function(result) {
setBadgeText(result.power ? "ON" : "OFF");
});
});
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) {
if (changeInfo.status === 'complete' && changeInfo.url) {
chrome.tabs.sendMessage(tabId, { "message": "url_changed" });
}
});
// メッセージリスナーを設定
chrome.runtime.onMessage.addListener(
async function (request, sender, sendResponse) {
if (request.message === "original_text_string") {
let response = await sendTranslationToContent(request.textString, sender);
sendResponse(response);
} else if (request.message === "on") {
chrome.action.setBadgeText({text: "ON"});
} else if (request.message === "off") {
chrome.action.setBadgeText({text: "OFF"});
}
return true; // indicates that the response is sent asynchronously
}
);
function setBadgeText(text) {
chrome.action.setBadgeText({text: text});
}
// Function to call the translation API
async function sendTranslationToContent(textString, sender) {
const textLists = splitTextString(textString);
for (let text of textLists) {
const response = await translate(text);
await processResponse(response, sender.tab.id);
}
}
function splitTextString(textString) {
const textLists = [];
if (textString.length > 3000) {
let start = 0;
while (start < textString.length) {
let end = start + 3000;
if (end < textString.length) {
end = textString.lastIndexOf('UUID', end);
if (end != -1) {
end = textString.indexOf(' ', end);
} else {
end = textString.length;
}
}
textLists.push(textString.substring(start, end));
start = end;
}
} else {
textLists.push(textString);
}
return textLists;
}
// Function to call the translation API
async function translate(text) {
return new Promise((resolve, reject) => {
try {
const url = 'https://api.openai.com/v1/chat/completions';
chrome.storage.local.get(['api_key', 'language'], async function(result) {
if (result.api_key) {
const apiKey = result.api_key;
const language = result.language || 'japanese';
console.log('language: ' + language)
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
},
body: JSON.stringify({
"model": "gpt-3.5-turbo-0613",
"messages": [
{ "role": "system", "content": `You are a translation AI. Paragraphs, sentences, words, and uuid tags will be sent to you. Translate the paragraphs, sentences, and words into ${language} and output them, and output the uuid tags as they are. Do not change the order of sentences within a paragraph unless absolutely necessary.Do not output any additional punctuation such as periods, commas, or punctuation marks that do not exist in the original text. The alignment of uuid tags is critical and must not be disrupted under any circumstances. Misalignment of uuid tags will cause serious problems. Never exceed the boundaries of the uuid tags. Deep Breath. Let's do our best!` },
{ "role": "user", "content": text }
],
"temperature": 0.7,
"stream": true,
}),
});
resolve(response);
} else {
reject('API Key not found.');
}
});
} catch (error) {
reject(error);
}
});
}
async function processResponse(response, tabId) {
// Create a TextDecoder to decode the response body stream
const decoder = new TextDecoder("utf-8");
const reader = response.body.getReader();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const lines = decoder.decode(value).split("\n");
for (const line of lines) {
if (line.startsWith('data:')) {
const data = line.slice(5);
try {
const jsonData = JSON.parse(data);
if (jsonData.choices[0].delta.content) {
buffer += jsonData.choices[0].delta.content;
if (/UUID\d+/.test(buffer)) {
sendToContent(tabId, buffer);
buffer = '';
}
}
} catch (error) {
console.error('Invalid JSON:', data);
}
}
}
}
}
async function sendToContent(tabId, translation) {
chrome.tabs.sendMessage(tabId, { message: "translation_result", translation: translation });
}