/
content.js
76 lines (71 loc) · 2.85 KB
/
content.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
function isTextInputElement(element) {
return element.tagName === 'TEXTAREA' || (element.tagName === 'INPUT' && element.type === 'text');
}
function replaceSelectedText(newText) {
const activeElement = document.activeElement;
if(isTextInputElement(activeElement)) {
const start = activeElement.selectionStart;
const end = activeElement.selectionEnd;
const replacementText = newText;
const textBefore = activeElement.value.substring(0, start);
const textAfter = activeElement.value.substring(end);
activeElement.value = textBefore + replacementText + textAfter;
activeElement.focus();
activeElement.setSelectionRange(start, start + replacementText.length);
} else {
let selection = window.getSelection();
selection.baseNode.textContent = newText;
}
}
chrome.runtime.onMessage.addListener(function (request) {
chrome.storage.sync.get(['apiKey'], function (result) {
const API_KEY = result.apiKey;
if (!API_KEY) {
alert('APIキーが設定されていません。オプションページで設定してください。');
return;
}
if (request.method === 'softenText') {
const overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
overlay.style.zIndex = '9999';
overlay.style.display = 'flex';
overlay.style.justifyContent = 'center';
overlay.style.alignItems = 'center';
overlay.innerHTML = '<p style="color: white; font-size: 24px;">言い換え中...</p>';
document.body.appendChild(overlay);
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{role: "system", content: 'あなたはSNSの専門家です。特にTwitterに詳しいです'},
{role: "user", content: `
以下の文章はTwitterに投稿されようとしているものです。
表現として社会的に好ましくないものがあれば、より柔らかく言い換えてください。解説などほかの文言は一切いれないでください:
${request.text}
`}],
max_tokens: 1000,
temperature: 0.1
})
})
.then(response => response.json())
.then(data => {
const softenedText = data.choices[0].message.content.trim();
document.body.removeChild(overlay);
replaceSelectedText(softenedText);
})
.catch(error => {
console.error('Error:', error);
});
}
});
});