-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
135 lines (121 loc) · 3.63 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
console.log("Background script started");
chrome.runtime.onInstalled.addListener(() => {
console.log("Extension installed");
chrome.storage.local.set({ previousPrompts: [] }, () => {
console.log("Initial storage set");
});
});
function logTabInfo(tab) {
console.log("Tab info:", {
id: tab.id,
url: tab.url || "undefined",
status: tab.status,
active: tab.active,
});
}
function checkAndLogUrl(url) {
if (!url) {
console.log(
"URL is undefined. This might be a special Chrome page or a page that the extension doesn't have permission to access.",
);
return false;
}
if (url.includes("chatgpt.com") || url.includes("claude.ai")) {
console.log("Matching URL found:", url);
return true;
}
console.log("Non-matching URL:", url);
return false;
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log("Tab updated event fired", { tabId, changeInfo });
logTabInfo(tab);
if (changeInfo.status === "complete") {
chrome.tabs.get(tabId, (currentTab) => {
if (chrome.runtime.lastError) {
console.error("Error getting tab:", chrome.runtime.lastError);
return;
}
logTabInfo(currentTab);
checkAndLogUrl(currentTab.url);
if (
currentTab.url &&
(currentTab.url.includes("chatgpt.com") ||
currentTab.url.includes("claude.ai"))
) {
chrome.tabs.sendMessage(
tabId,
{ action: "checkContentScript" },
function (response) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
} else if (response && response.status === "ok") {
console.log("Content script is loaded and responsive");
}
},
);
}
});
}
});
chrome.tabs.onActivated.addListener((activeInfo) => {
chrome.tabs.get(activeInfo.tabId, (tab) => {
if (chrome.runtime.lastError) {
console.error("Error getting activated tab:", chrome.runtime.lastError);
return;
}
logTabInfo(tab);
checkAndLogUrl(tab.url);
});
});
function checkPermissions() {
chrome.permissions.contains(
{
permissions: ["tabs"],
origins: ["https://chatgpt.com/*", "https://claude.ai/*"],
},
(result) => {
if (result) {
console.log("The extension has the necessary permissions.");
} else {
console.log("The extension doesn't have the necessary permissions.");
console.log(
"Please click the extension icon to grant the required permissions.",
);
}
},
);
}
setInterval(checkPermissions, 5 * 60 * 1000);
checkPermissions();
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "savePrompt") {
chrome.storage.local.get("previousPrompts", (result) => {
let prompts = result.previousPrompts || [];
prompts.unshift(request.prompt);
prompts = [...new Set(prompts)].slice(0, 1000);
chrome.storage.local.set({ previousPrompts: prompts }, () => {
console.log("Prompt saved:", request.prompt);
sendResponse({ status: "success" });
});
});
return true;
} else if (request.action === "requestPermissions") {
chrome.permissions.request(
{
permissions: ["tabs"],
origins: ["https://chatgpt.com/*", "https://claude.ai/*"],
},
(granted) => {
if (granted) {
console.log("Permissions granted");
sendResponse({ status: "granted" });
} else {
console.log("Permissions not granted");
sendResponse({ status: "denied" });
}
},
);
return true;
}
});