-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
69 lines (50 loc) · 1.6 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
// background.js console.log only appears in its console, not in the current web page.
/* global */
//chrome.storage.local.clear();
const AuthURL = "https://chat.openai.com/api/auth/session";
let authReceived = false;
console.log ("Background script loaded");
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
if (changeInfo.status == "complete")
if (tab.url != null && tab.url.includes("https://chat.openai.com/"))
{
chrome.tabs.sendMessage(tabId,
{
type: "at_chat_openai",
url: tab.url
});
}
});
// Get the auth token to be able to list and scan the conversations by its Id
chrome.webRequest.onBeforeSendHeaders.addListener ( function(details)
{
// we need it only once, isn't it?
if (authReceived) return;
for (var i = 0; i < details.requestHeaders.length; ++i)
{
if (details.requestHeaders[i].name === 'Authorization')
{
chrome.storage.local.set( { authToken : details.requestHeaders[i].value })
authReceived=true;
chrome.tabs.sendMessage (details.tabId,{ type: "authReceived" });
console.log ("Auth token received ",details.url)
// will remove the listener, this will work once per browser session, isn't it?
chrome.webRequest.onBeforeSendHeaders.removeListener(arguments.callee);
break;
}
}
},
{urls: ["https://chat.openai.com/*"]},
['requestHeaders']
);
chrome.commands.onCommand.addListener(function(command)
{
if (command == "toogle_root_collapse")
{
chrome.tabs.query ({active: true, currentWindow: true}, function (tabs)
{
chrome.tabs.sendMessage (tabs[0].id,{ type: "command", command : command });
});
}
});