-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
101 lines (84 loc) · 3.06 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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// When the browser action is clicked, `addToClipboard()` will use an offscreen
// document to write the value of `textToCopy` to the system clipboard.
chrome.action.onClicked.addListener(async () => {
await addToClipboard(textToCopy);
});
chrome.contextMenus.create({
id: "sayitbetter",
title: "Say it better!",
contexts:["selection"],
});
async function getConfiguration() {
return new Promise((resolve) => {
chrome.storage.local.get('config', (data) => {
resolve(data.config);
});
});
}
chrome.contextMenus.onClicked.addListener(async function(clickData, tab){
if (clickData.menuItemId == "sayitbetter" && clickData.selectionText){
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
}).then(() => {
chrome.tabs.sendMessage(tab.id, {
action: 'createDiv',
text: "Asking to ChatGPT, please wait..."
});
}).catch(err => console.error(err));
const tone = await getConfiguration() || "plain";
console.error(tone);
const url = "https://say-it-better.vercel.app/api/query";
const res = await fetch(url, {
method: "POST",
body: JSON.stringify({tone: tone, prompt: clickData.selectionText}),
});
const body = await res.text();
addToClipboard(body);
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
}).then(() => {
chrome.tabs.sendMessage(tab.id, {
action: 'updateDiv',
text: "Copied to the clipboard."
});
}).catch(err => console.error(err));
}
});
// Solution 1 - As of Jan 2023, service workers cannot directly interact with
// the system clipboard using either `navigator.clipboard` or
// `document.execCommand()`. To work around this, we'll create an offscreen
// document and pass it the data we want to write to the clipboard.
async function addToClipboard(value) {
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: [chrome.offscreen.Reason.CLIPBOARD],
justification: 'Write text to the clipboard.'
});
// Now that we have an offscreen document, we can dispatch the
// message.
chrome.runtime.sendMessage({
type: 'copy-data-to-clipboard',
target: 'offscreen-doc',
data: value
});
}
// Solution 2 – Once extension service workers can use the Clipboard API,
// replace the offscreen document based implementation with something like this.
// async function addToClipboardV2(value) {
// navigator.clipboard.writeText(value);
// }