-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
164 lines (141 loc) · 4.57 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { postToServer } from './lib.js';
const state = {};
let last_url = '';
const extractProblem = (url) => {
return /(?:problems\/)([^/]+)/.exec(url)[1];
};
const cacheTitle = async (tabid, tab) => {
const url = tab.url;
const resp1 = await chrome.tabs.sendMessage(tabid, {
type: 'queryTitle',
});
if (resp1 && resp1.formattedTitle) {
saveTabState(extractProblem(url), resp1);
}
};
// testings
const getState = (tab) => {
return state[extractProblem(tab.url)];
};
const uploadSolutionCallback = (tab, useCommentAsTitle) => {
chrome.action.setBadgeText({ tabId: tab.id, text: '...' });
chrome.action.setBadgeBackgroundColor({
tabId: tab.id,
color: 'black',
});
(async () => {
if (!getState(tab)) await cacheTitle(tab.id, tab);
const { url, code } = await chrome.tabs.sendMessage(tab.id, {
type: 'scrapeLeetCode',
});
let suffix = '';
if (useCommentAsTitle) {
// extract as what we should append
let comment = code.split('\n')[0];
console.log(comment);
// basic checks to make sure it's formatted how we would expect
comment = comment.trim();
if (comment.charAt(0) != '#' && comment.charAt(0) != '/') {
console.log(
'uh oh we failed to get comment!',
comment,
comment.charAt(0)
);
}
comment = comment.substring(1).trim().replace(/\s+/g, '-');
console.log('trimmed', comment);
//
suffix = comment;
}
const { difficulty, formattedTitle } = getState(tab);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const password = (
await (
await fetch(chrome.runtime.getURL('config/secret.json'))
).json()
)['password'];
chrome.action.setBadgeBackgroundColor({
tabId: tab.id,
color: '#90EE90',
});
const resp = await postToServer({
controller,
difficulty,
formattedTitle,
suffix,
url,
code,
password,
fetchMethod: fetch,
});
const d = await resp.text();
if (resp.status == 200) {
// const badgeText = d.substring(0, d.indexOf('-')).replace(/^0+/, '');
const badgeText = ':)';
chrome.action.setBadgeBackgroundColor({
tabId: tab.id,
color: 'green',
});
chrome.action.setBadgeText({
tabId: tab.id,
text: badgeText,
});
// so we can pop it open later
last_url = d;
} else if (resp.status == 501) {
chrome.action.setBadgeBackgroundColor({
tabId: tab.id,
color: 'orange',
});
} else {
console.log(resp);
chrome.action.setBadgeBackgroundColor({
tabId: tab.id,
color: 'red',
});
}
chrome.storage.local.set({
jasbot_success: resp.status == 200,
jasbot_last: formattedTitle,
});
// chrome.action.openPopup();
})();
return true;
};
const openLastUrl = (tab) => {
if (last_url && last_url.length) {
chrome.tabs.create({ url: last_url });
} else {
console.log('broke', last_url);
chrome.action.setBadgeBackgroundColor({
tabId: tab.id,
color: 'red',
});
chrome.action.setBadgeText({
tabId: tab.id,
text: 'brok:(',
});
}
};
chrome.commands.onCommand.addListener((command, tab) => {
console.log('command triggered', command);
if (command == 'add_alternate') uploadSolutionCallback(tab, true);
else if (command == 'add_regular') uploadSolutionCallback(tab, false);
else if (command == 'pop_open_tab') openLastUrl(tab);
});
chrome.action.onClicked.addListener((tab) => openLastUrl(tab));
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
tab.url.includes('leetcode.com') &&
changeInfo.status === 'complete' &&
tab.status == 'complete'
) {
cacheTitle(tabId, tab);
}
return true;
});
const saveTabState = (tabIdentifier, tabstate) => {
if (!(tabIdentifier in state)) state[tabIdentifier] = {};
state[tabIdentifier] = { ...state[tabIdentifier], ...tabstate };
};