Skip to content

Commit e60840d

Browse files
committed
rm browser-polyfill and replace w chrome b/c i couldnt get polyfill working on chrome
1 parent 73b2ee1 commit e60840d

File tree

12 files changed

+78
-98
lines changed

12 files changed

+78
-98
lines changed

manifest.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"content_scripts": [
2727
{
2828
"js": [
29-
"node_modules/webextension-polyfill/dist/browser-polyfill.js",
3029
"dist/content-script/get-gpt-access-token.js",
3130
"dist/content-script/get-user-code.js",
3231
"dist/content-script/update-solutions-tab.js",

src/background/background.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
import { getChatGPTAccessToken } from './chatgpt/chatgpt.js';
2-
var { browser } = require("webextension-polyfill-ts");
32

43
// Load JSON & default settings on install
5-
browser.runtime.onInstalled.addListener(() => {
4+
chrome.runtime.onInstalled.addListener(() => {
65
// Load JSON file into storage
7-
const jsonUrl = browser.runtime.getURL('src/assets/data/leetcode_solutions.json');
6+
const jsonUrl = chrome.runtime.getURL('src/assets/data/leetcode_solutions.json');
87
fetch(jsonUrl)
98
.then((response) => response.json())
109
.then((data) => {
11-
browser.storage.local.set({ leetcodeProblems: data });
10+
chrome.storage.local.set({ leetcodeProblems: data });
1211
})
1312
.catch((error) => {
1413
console.error(error);
1514
});
1615

1716
// Default settings
18-
browser.storage.local.set({ language: 'python' });
19-
browser.storage.local.set({ fontSize: 14 });
20-
browser.storage.local.set({ showCompanyTags: true });
21-
browser.storage.local.set({ showExamples: true });
22-
browser.storage.local.set({ showDifficulty: true });
23-
browser.storage.local.set({ clickedCompany: 'Amazon' });
17+
chrome.storage.local.set({ language: 'python' });
18+
chrome.storage.local.set({ fontSize: 14 });
19+
chrome.storage.local.set({ showCompanyTags: true });
20+
chrome.storage.local.set({ showExamples: true });
21+
chrome.storage.local.set({ showDifficulty: true });
22+
chrome.storage.local.set({ clickedCompany: 'Amazon' });
2423
});
2524

26-
browser.runtime.onMessage.addListener(
25+
chrome.runtime.onMessage.addListener(
2726
function (request, _, sendResponse) {
2827
if (request.action == 'openSolutionVideo') {
29-
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
28+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
3029
let url = tabs[0].url;
3130
if (url) {
3231
// Remove /description/ if it exists
@@ -40,7 +39,7 @@ browser.runtime.onMessage.addListener(
4039
if (tabs.length > 0 && tabs[0].id) {
4140
const tabId = tabs[0].id;
4241
const updateProperties = { url: newUrl };
43-
browser.tabs.update(tabId, updateProperties);
42+
chrome.tabs.update(tabId, updateProperties);
4443
}
4544
}
4645
});
@@ -49,35 +48,35 @@ browser.runtime.onMessage.addListener(
4948
},
5049
);
5150

52-
browser.runtime.onMessage.addListener((request) => {
51+
chrome.runtime.onMessage.addListener((request) => {
5352
if (request.action === 'openCompanyPage') {
54-
browser.storage.local.set({ clickedCompany: request.company });
55-
browser.tabs.create({
56-
url: browser.runtime.getURL('src/problems-by-company/company.html'),
53+
chrome.storage.local.set({ clickedCompany: request.company });
54+
chrome.tabs.create({
55+
url: chrome.runtime.getURL('src/problems-by-company/company.html'),
5756
active: true,
5857
}, function (tab) {
5958
// Keep a reference to the listener so it can be removed later
6059
const listener = function (tabId: number, changedProps: any) {
6160
// When the tab is done loading
6261
if (tabId == tab.id && changedProps.status == 'complete') {
63-
browser.tabs.sendMessage(tabId, request);
62+
chrome.tabs.sendMessage(tabId, request);
6463
// Remove the listener once the tab is loaded
65-
browser.tabs.onUpdated.removeListener(listener);
64+
chrome.tabs.onUpdated.removeListener(listener);
6665
}
6766
};
6867
// Attach the listener
69-
browser.tabs.onUpdated.addListener(listener);
68+
chrome.tabs.onUpdated.addListener(listener);
7069
});
7170
}
7271
});
7372

74-
browser.runtime.onMessage.addListener((request: any) => {
73+
chrome.runtime.onMessage.addListener((request: any) => {
7574
if (request.type === 'OPEN_LOGIN_PAGE') {
76-
browser.tabs.create({ url: 'https://chat.openai.com' });
75+
chrome.tabs.create({ url: 'https://chat.openai.com' });
7776
}
7877
});
7978

80-
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
79+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
8180
if (request.type === 'GET_CHATGPT_ACCESS_TOKEN') {
8281
getChatGPTAccessToken().then((accessToken) => {
8382
sendResponse({ accessToken: accessToken });
@@ -87,13 +86,13 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
8786
});
8887

8988
// If the user is on a Leetcode problem page, show the solution video or company tags.
90-
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
89+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
9190
// If descriptions tab is opened or updated, update the description
9291
let urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
9392
if (changeInfo.status === 'complete' && tab.url && tab.url.match(urlPattern)) {
9493
setTimeout(() => {
95-
browser.tabs.get(tabId, (updatedTab) => {
96-
browser.tabs.sendMessage(tabId, { action: 'updateDescription', title: updatedTab.title || 'title' });
94+
chrome.tabs.get(tabId, (updatedTab) => {
95+
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: updatedTab.title || 'title' });
9796
});
9897
}, 1000);
9998
}
@@ -102,8 +101,8 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
102101
urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
103102
if (changeInfo.status === 'complete' && tab.url && tab.url.match(urlPattern)) {
104103
setTimeout(() => {
105-
browser.tabs.get(tabId, (updatedTab) => {
106-
browser.tabs.sendMessage(tabId, { action: 'addVideo', title: updatedTab.title || 'title' });
104+
chrome.tabs.get(tabId, (updatedTab) => {
105+
chrome.tabs.sendMessage(tabId, { action: 'addVideo', title: updatedTab.title || 'title' });
107106
});
108107
}, 1000);
109108
}
@@ -112,7 +111,7 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
112111
urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
113112
if (changeInfo.status === 'complete' && tab.url && tab.url.match(urlPattern)) {
114113
setTimeout(() => {
115-
browser.storage.local.set({ 'currentLeetCodeProblemTitle': tab.title || 'title' });
114+
chrome.storage.local.set({ 'currentLeetCodeProblemTitle': tab.title || 'title' });
116115
}, 1000);
117116
}
118117
});

src/content-script/get-gpt-access-token.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
var browser = require("webextension-polyfill");
2-
31
// Request the access token from the background script
4-
browser.runtime.sendMessage({ type: 'GET_CHATGPT_ACCESS_TOKEN' }, (response) => {
2+
chrome.runtime.sendMessage({ type: 'GET_CHATGPT_ACCESS_TOKEN' }, (response) => {
53
const accessToken = response.accessToken;
64
if (accessToken) {
7-
browser.storage.local.set({ accessToken });
5+
chrome.storage.local.set({ accessToken });
86
} else {
97
console.error('Error: Unable to get ChatGPT access token.');
108
}

src/content-script/get-user-code.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
var { browser } = require("webextension-polyfill-ts");
2-
31
// Reads the code from the user's code editor and sends it to the background script
42

53
// On get user code request, read & send the code as a response
6-
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
4+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
75
if (request.type === 'getCode') {
86
sendResponse({ data: getCode() });
97
}

src/content-script/update-description-tab.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
This includes hiding the company tags, examples, and difficulty of the problem.
55
*/
66

7-
var { browser } = require('webextension-polyfill-ts');
8-
97
// shows the examples if the user has enabled it in the settings
108
function showExamples() {
11-
browser.storage.local.get(['showExamples'], (result) => {
9+
chrome.storage.local.get(['showExamples'], (result) => {
1210
const showExamples = result.showExamples;
1311
const descriptionContainer = document.querySelector('div._1l1MA') as Element;
1412
if (!descriptionContainer) {
@@ -31,7 +29,7 @@ function showExamples() {
3129

3230
// show the leetcode difficulty if the user has enabled it in the settings
3331
function showDifficulty() {
34-
browser.storage.local.get(['showDifficulty'], (result) => {
32+
chrome.storage.local.get(['showDifficulty'], (result) => {
3533
const showDifficulty = result.showDifficulty;
3634

3735
// Finding the difficulty element and then toggling the display.
@@ -48,7 +46,7 @@ function showDifficulty() {
4846

4947
// show the company tags if the user has enabled it in the settings
5048
function showCompanyTags(problemTitle: string) {
51-
browser.storage.local.get(['showCompanyTags'], (result) => {
49+
chrome.storage.local.get(['showCompanyTags'], (result) => {
5250
const showCompanyTags = result.showCompanyTags;
5351
let companyTagContainer = document.getElementById('companyTagContainer');
5452

@@ -103,7 +101,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
103101
}>;
104102
}
105103

106-
browser.storage.local.get(['leetcodeProblems'], (result) => {
104+
chrome.storage.local.get(['leetcodeProblems'], (result) => {
107105
const problem = result.leetcodeProblems.questions.find((problem: problem) => problem.title === problemTitle);
108106
if (problem.companies && problem.companies.length > 0) {
109107
const topCompanies = problem.companies.slice(0, 5);
@@ -112,7 +110,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
112110
const button = document.createElement('button');
113111
// opens the company page when the button is clicked
114112
button.onclick = () => {
115-
browser.runtime.sendMessage({
113+
chrome.runtime.sendMessage({
116114
action: 'openCompanyPage', company: company.name,
117115
});
118116
};
@@ -159,7 +157,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
159157
return companyTagContainer;
160158
}
161159

162-
browser.runtime.onMessage.addListener((request) => {
160+
chrome.runtime.onMessage.addListener((request) => {
163161
if (request.action === 'updateDescription') {
164162
showExamples();
165163
showCompanyTags(request.title.split('-')[0].trim());

src/content-script/update-solutions-tab.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Adds the top 5 youtube solution videos into the solutions tab of a Leetcode problem page.
33
*/
44

5-
var { browser } = require('webextension-polyfill-ts');
6-
75
const VIDEO_ASPECT_RATIO = 56.25; // 16:9 aspect ratio
86

97
function createStyledElement(tagName: string, styles: { [key: string]: string }) {
@@ -101,7 +99,7 @@ function addVideo(title: string) {
10199
const existingContainer = solutionsTab.parentElement?.querySelector('div.video-container');
102100
if (existingContainer) return;
103101

104-
browser.storage.local.get(['leetcodeProblems'], (result) => {
102+
chrome.storage.local.get(['leetcodeProblems'], (result) => {
105103
const problem = result.leetcodeProblems.questions.find((problem: { title: string }) => problem.title === title);
106104
if (problem?.videos?.length) {
107105
let currentVideoIndex = 0;
@@ -167,7 +165,7 @@ function updateVideo(container: HTMLDivElement, videoUrl: string, channelName: s
167165
}
168166
}
169167

170-
browser.runtime.onMessage.addListener((request) => {
168+
chrome.runtime.onMessage.addListener((request) => {
171169
if (request.action === 'addVideo') {
172170
const title = request.title.split('-')[0].trim();
173171
addVideo(title);

src/popup/popup.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<title>Leetcode Explained</title>
66
<link rel="stylesheet" href="./popup.css">
77
<link rel="stylesheet" href="prism.css">
8-
<script type="application/javascript"
9-
src="../../node_modules/webextension-polyfill/dist/browser-polyfill.js"></script>
108
<script type="module" src="../../dist/popup/popup.js"></script>
119
</head>
1210

src/popup/popup.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Creates the GPT buttons, sets the prompts, and displays the responses.
44
The user can also copy the code to their clipboard, clear the code, and open the settings page.
55
*/
66

7-
var { browser } = require('webextension-polyfill-ts');
8-
97
import {
108
getChatGPTAccessToken,
119
ChatGPTProvider,
@@ -59,8 +57,8 @@ function clearResponse(): void {
5957
if (fixCodeResponse) fixCodeResponse.textContent = '';
6058
if (fixCodeContainer) fixCodeContainer.classList.add('hidden');
6159
if (analyzeCodeResponse) analyzeCodeResponse.classList.add('hidden');
62-
browser.storage.local.set({ 'fixCodeResponse': '' });
63-
browser.storage.local.set({ 'analyzeCodeResponse': '' });
60+
chrome.storage.local.set({ 'fixCodeResponse': '' });
61+
chrome.storage.local.set({ 'analyzeCodeResponse': '' });
6462
}
6563

6664
function setInfoMessage(message: string, duration: number) {
@@ -88,12 +86,12 @@ function initActionButton(buttonId: string, action: string, chatGPTProvider: Cha
8886

8987
async function getCodeFromActiveTab(): Promise<string | null> {
9088
return new Promise<string | null>((resolve) => {
91-
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
92-
browser.tabs.sendMessage(
89+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
90+
chrome.tabs.sendMessage(
9391
tabs[0].id as number,
9492
{ type: 'getCode' },
9593
(response) => {
96-
if (browser.runtime.lastError) {
94+
if (chrome.runtime.lastError) {
9795
resolve(null);
9896
} else {
9997
resolve(response.data);
@@ -164,9 +162,9 @@ function processCode(
164162
}
165163
}
166164
if (event.type === 'done') {
167-
analyzeCodeResponse && browser.storage.local.set({ 'analyzeCodeResponse': analyzeCodeResponse.textContent });
168-
fixCodeResponse && browser.storage.local.set({ 'fixCodeResponse': fixCodeResponse.textContent });
169-
browser.storage.local.set({ 'lastAction': action });
165+
analyzeCodeResponse && chrome.storage.local.set({ 'analyzeCodeResponse': analyzeCodeResponse.textContent });
166+
fixCodeResponse && chrome.storage.local.set({ 'fixCodeResponse': fixCodeResponse.textContent });
167+
chrome.storage.local.set({ 'lastAction': action });
170168
infoMessage && (infoMessage.textContent = problemTitle);
171169
disableAllButtons(false);
172170
(window as any).Prism.highlightAll();
@@ -192,7 +190,7 @@ async function main(): Promise<void> {
192190
const fontSizeElement = document.documentElement; // Or any specific element you want to change the font size of
193191

194192
// Load font size from storage
195-
browser.storage.local.get('fontSize', function (data) {
193+
chrome.storage.local.get('fontSize', function (data) {
196194
if (data.fontSize) {
197195
fontSizeElement.style.setProperty('--dynamic-font-size', `${data.fontSize}px`);
198196
if (parseInt(data.fontSize) >= 18) {
@@ -209,21 +207,21 @@ async function main(): Promise<void> {
209207
}
210208
});
211209

212-
browser.storage.local.get('analyzeCodeResponse', function (data) {
210+
chrome.storage.local.get('analyzeCodeResponse', function (data) {
213211
if (data.analyzeCodeResponse) {
214212
analyzeCodeResponse && (analyzeCodeResponse.textContent = data.analyzeCodeResponse);
215213
(window as any).Prism.highlightAll();
216214
}
217215
});
218216

219-
browser.storage.local.get('fixCodeResponse', function (data) {
217+
chrome.storage.local.get('fixCodeResponse', function (data) {
220218
if (data.fixCodeResponse) {
221219
fixCodeResponse && (fixCodeResponse.textContent = data.fixCodeResponse);
222220
(window as any).Prism.highlightAll();
223221
}
224222
});
225223

226-
browser.storage.local.get('lastAction', function (data) {
224+
chrome.storage.local.get('lastAction', function (data) {
227225
if (data.lastAction) {
228226
if (data.lastAction === 'analyze') {
229227
analyzeCodeResponse && analyzeCodeResponse.classList.remove('hidden');
@@ -237,15 +235,15 @@ async function main(): Promise<void> {
237235
});
238236

239237
// get language from storage and set the classname of the code block to it
240-
browser.storage.local.get('language', function (data) {
238+
chrome.storage.local.get('language', function (data) {
241239
fixCodeResponse && fixCodeResponse.classList.add('language-' + data.language);
242240
});
243241

244242
// get name of current tab and set info message to it if its a leetcode problem
245-
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
243+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
246244
const tab = tabs[0];
247245
if (tab.url && tab.url.includes('leetcode.com/problems')) {
248-
browser.storage.local.set({ 'currentLeetCodeProblemTitle': tab.title });
246+
chrome.storage.local.set({ 'currentLeetCodeProblemTitle': tab.title });
249247
if (tab.title && infoMessage) {
250248
infoMessage.textContent = tab.title.split('-')[0];
251249
}
@@ -308,7 +306,7 @@ function handleError(error: Error): void {
308306

309307
function displayLoginMessage(): void {
310308
elements['loginBtn'] && elements['loginBtn'].classList.remove('hidden');
311-
infoMessage && (infoMessage.textContent = 'Log into ChatGPT in your browser to get started');
309+
infoMessage && (infoMessage.textContent = 'Log into ChatGPT in your chrome to get started');
312310
}
313311

314312
function displayErrorMessage(error: string): void {
@@ -317,10 +315,10 @@ function displayErrorMessage(error: string): void {
317315

318316
/* Event listeners */
319317
elements['loginBtn'] && (elements['loginBtn'].onclick = () => {
320-
browser.runtime.sendMessage({ type: 'OPEN_LOGIN_PAGE' });
318+
chrome.runtime.sendMessage({ type: 'OPEN_LOGIN_PAGE' });
321319
});
322320

323-
browser.runtime.onMessage.addListener((message) => {
321+
chrome.runtime.onMessage.addListener((message) => {
324322
if (message.action === 'setTabInfo') {
325323
const urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
326324
if (message.url.match(urlPattern)) {
@@ -332,7 +330,7 @@ browser.runtime.onMessage.addListener((message) => {
332330
/* Utility functions */
333331
function getFromStorage(key: string) {
334332
return new Promise((resolve) => {
335-
browser.storage.local.get(key, (data) => resolve(data[key]));
333+
chrome.storage.local.get(key, (data) => resolve(data[key]));
336334
});
337335
}
338336

0 commit comments

Comments
 (0)