-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice_worker.js
More file actions
136 lines (122 loc) · 4.49 KB
/
service_worker.js
File metadata and controls
136 lines (122 loc) · 4.49 KB
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
// Initialize global variables
let windowDuration = 3 * 60 * 60 * 1000; // 3 hours
let max_num_requests = 40;
// Function to reset the request count if the current time exceeds the window duration
function checkAndReset() {
chrome.storage.local.get(
["first_message_timestamp", "request_count"],
function (data) {
let firstMessageTimestamp = data.first_message_timestamp;
if (!firstMessageTimestamp) {
chrome.storage.local.set({
first_message_timestamp: Date.now() - windowDuration - 1,
});
firstMessageTimestamp = Date.now() - windowDuration - 1;
}
const timePassed = Date.now() - firstMessageTimestamp;
if (timePassed > windowDuration) {
chrome.storage.local.set({ request_count: 0 }, function () {
console.log(
"3 hours have passed, resetting request count..."
);
updateBadgeCount(0);
return;
});
}
}
);
}
// Function to update the badge count
function updateBadgeCount(newCount) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length > 0 && tabs[0] && tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
actionType: "updateCount",
newCount: newCount,
});
}
});
}
// Function to update the time left
function updateTimeLeft(firstMessageTimestamp) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length > 0 && tabs[0] && tabs[0].id) {
chrome.tabs.sendMessage(tabs[0].id, {
actionType: "updateTime",
firstMessageTimestamp: firstMessageTimestamp,
});
}
});
}
// Function to track GPT-4 requests and increment the request count
function trackGPT4Request(details) {
if (
details.url.includes("https://chat.openai.com/backend-api/conversation")
) {
let requestBody = null;
if (
details.requestBody &&
details.requestBody.raw &&
details.requestBody.raw[0] &&
details.requestBody.raw[0].bytes
) {
let rawBytes = details.requestBody.raw[0].bytes;
let jsonString = new TextDecoder("utf-8").decode(
new Uint8Array(rawBytes)
);
try {
requestBody = JSON.parse(jsonString);
} catch (e) {
console.error("Error parsing JSON: ", e);
}
} else {
console.error("Expected properties not found in details object");
}
let model = requestBody && requestBody.model;
// if model is not gpt 3.5
if (model && !model.includes("text-davinci")) {
chrome.storage.local.get(["request_count"], function (data) {
try {
chrome.storage.local.set(
{
request_count: data.request_count + 1,
first_message_timestamp:
data.request_count === 0
? Date.now()
: data.first_message_timestamp,
},
function () {
updateBadgeCount(data.request_count + 1);
}
);
} catch (error) {
console.error(
"An error occurred inside the if (model && model.includes...) block",
error
);
}
});
}
}
}
// Set up a chrome alarm to periodically
chrome.alarms.create("updateTime", { periodInMinutes: 0.1 });
chrome.alarms.create("resetAlarm", { periodInMinutes: 0.01 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "resetAlarm") {
checkAndReset();
}
if (alarm.name === "updateTime") {
chrome.storage.local.get(["first_message_timestamp"], function (data) {
updateTimeLeft(data.first_message_timestamp);
});
}
});
// Event listener for web requests, calls trackGPT4Request function
chrome.webRequest.onBeforeRequest.addListener(
trackGPT4Request,
{ urls: ["https://chat.openai.com/backend-api/conversation"] },
["requestBody"]
);
// Initial setup
checkAndReset();