-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
116 lines (112 loc) · 4.47 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
chrome.runtime.onInstalled.addListener(() => {
console.log("AI答题辅助系统 Chrome Extension installed.");
// Create context menu for asking GPT
chrome.contextMenus.create({
id: "askGpt",
title: "Ask Gomoon",
contexts: ["selection"]
}, () => {
if (chrome.runtime.lastError) {
console.error("Error creating context menu for 'Ask GPT':", chrome.runtime.lastError);
} else {
console.log("Context menu for 'Ask GPT' created successfully.");
}
});
// Create context menu for toggling the history panel
// chrome.contextMenus.create({
// id: "toggleHistoryPanel",
// title: "Toggle History Panel",
// contexts: ["all"]
// }, () => {
// if (chrome.runtime.lastError) {
// console.error("Error creating context menu for 'Toggle History Panel':", chrome.runtime.lastError);
// } else {
// console.log("Context menu for 'Toggle History Panel' created successfully.");
// }
// });
});
// Listen for context menu item click
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "toggleHistoryPanel") {
// Ensure content script is injected before sending message
injectContentScriptIfNeeded(tab).then(() => {
sendMessageToToggleHistoryPanel(tab);
}).catch(error => console.error("Error injecting content script for toggling history panel:", error));
} else if (info.menuItemId === "askGpt") {
chrome.storage.sync.get(["activationKey", "gptRole"], function(result) {
if (result.activationKey) {
console.log('Activation key found. Proceeding with GPT request.');
fetch('http://api.gomoon.pro/askgpt', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: info.selectionText,
activationKey: result.activationKey,
role: result.gptRole
})
})
.then(response => response.json())
.then(data => {
// Ensure content script is injected before sending GPT response
injectContentScriptIfNeeded(tab).then(() => {
chrome.tabs.sendMessage(tab.id, {gptResponse: data.message, selectionText: info.selectionText}, () => {
if (chrome.runtime.lastError) {
console.error("Error sending GPT response to content script:", chrome.runtime.lastError.message);
} else {
console.log("GPT response and question sent to content script successfully.");
}
});
}).catch(error => console.error("Error injecting content script for sending GPT response:", error));
})
.catch(error => {
console.error("Error making GPT request:", error);
// Send an error message to the content script
chrome.tabs.sendMessage(tab.id, {error: "GPT request failed. Please try again later."});
});
} else {
console.log('Extension is not activated. Please enter an activation key.');
injectContentScriptIfNeeded(tab).then(() => {
chrome.tabs.sendMessage(tab.id, {error: "Invalid activation key."});
}).catch(error => console.error("Error injecting content script for sending GPT response:", error));
}
});
}
});
function sendMessageToToggleHistoryPanel(tab) {
chrome.tabs.sendMessage(tab.id, {toggleHistoryPanel: true}, response => {
if (chrome.runtime.lastError) {
console.error("Error toggling history panel:", chrome.runtime.lastError.message);
} else {
console.log("Toggle history panel message sent successfully.");
}
});
}
// Updated function to inject content script if needed
function injectContentScriptIfNeeded(tab) {
return new Promise((resolve, reject) => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: () => !!window.isContentScriptInjected,
}, (injectionResults) => {
if (chrome.runtime.lastError || !injectionResults[0].result) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['contentScript.js']
}, () => {
if (chrome.runtime.lastError) {
console.error("Error injecting content script:", chrome.runtime.lastError);
reject(chrome.runtime.lastError);
} else {
console.log("Content script injected successfully.");
resolve();
}
});
} else {
console.log("Content script already injected.");
resolve();
}
});
});
}