Skip to content

Commit 23dad67

Browse files
committed
use web-pollyfill to support firefox
1 parent de1ee62 commit 23dad67

File tree

14 files changed

+109
-79
lines changed

14 files changed

+109
-79
lines changed

manifest.json

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

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
"eslint-plugin-n": "^15.7.0",
2525
"eslint-plugin-promise": "^6.1.1",
2626
"typescript": "^5.0.4",
27+
"webextension-polyfill": "^0.10.0",
2728
"webpack": "^5.83.1",
2829
"webpack-cli": "^5.1.1"
2930
},
3031
"dependencies": {
3132
"eventsource-parser": "^1.0.0",
3233
"lodash-es": "^4.17.21"
3334
}
34-
}
35+
}

src/background/background.ts

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

34
// Load JSON & default settings on install
4-
chrome.runtime.onInstalled.addListener(() => {
5+
browser.runtime.onInstalled.addListener(() => {
56
// Load JSON file into storage
6-
const jsonUrl = chrome.runtime.getURL('src/assets/data/leetcode_solutions.json');
7+
const jsonUrl = browser.runtime.getURL('src/assets/data/leetcode_solutions.json');
78
fetch(jsonUrl)
89
.then((response) => response.json())
910
.then((data) => {
10-
chrome.storage.local.set({ leetcodeProblems: data });
11+
browser.storage.local.set({ leetcodeProblems: data });
1112
})
1213
.catch((error) => {
1314
console.error(error);
1415
});
1516

1617
// Default settings
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' });
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' });
2324
});
2425

25-
chrome.runtime.onMessage.addListener(
26+
browser.runtime.onMessage.addListener(
2627
function (request, _, sendResponse) {
2728
if (request.action == 'openSolutionVideo') {
28-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
29+
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => {
2930
let url = tabs[0].url;
3031
if (url) {
3132
// Remove /description/ if it exists
@@ -39,7 +40,7 @@ chrome.runtime.onMessage.addListener(
3940
if (tabs.length > 0 && tabs[0].id) {
4041
const tabId = tabs[0].id;
4142
const updateProperties = { url: newUrl };
42-
chrome.tabs.update(tabId, updateProperties);
43+
browser.tabs.update(tabId, updateProperties);
4344
}
4445
}
4546
});
@@ -48,35 +49,35 @@ chrome.runtime.onMessage.addListener(
4849
},
4950
);
5051

51-
chrome.runtime.onMessage.addListener((request) => {
52+
browser.runtime.onMessage.addListener((request) => {
5253
if (request.action === 'openCompanyPage') {
53-
chrome.storage.local.set({ clickedCompany: request.company });
54-
chrome.tabs.create({
55-
url: chrome.runtime.getURL('src/problems-by-company/company.html'),
54+
browser.storage.local.set({ clickedCompany: request.company });
55+
browser.tabs.create({
56+
url: browser.runtime.getURL('src/problems-by-company/company.html'),
5657
active: true,
5758
}, function (tab) {
5859
// Keep a reference to the listener so it can be removed later
5960
const listener = function (tabId: number, changedProps: any) {
6061
// When the tab is done loading
6162
if (tabId == tab.id && changedProps.status == 'complete') {
62-
chrome.tabs.sendMessage(tabId, request);
63+
browser.tabs.sendMessage(tabId, request);
6364
// Remove the listener once the tab is loaded
64-
chrome.tabs.onUpdated.removeListener(listener);
65+
browser.tabs.onUpdated.removeListener(listener);
6566
}
6667
};
6768
// Attach the listener
68-
chrome.tabs.onUpdated.addListener(listener);
69+
browser.tabs.onUpdated.addListener(listener);
6970
});
7071
}
7172
});
7273

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

79-
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
80+
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
8081
if (request.type === 'GET_CHATGPT_ACCESS_TOKEN') {
8182
getChatGPTAccessToken().then((accessToken) => {
8283
sendResponse({ accessToken: accessToken });
@@ -86,13 +87,13 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
8687
});
8788

8889
// If the user is on a Leetcode problem page, show the solution video or company tags.
89-
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
90+
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
9091
// If descriptions tab is opened or updated, update the description
9192
let urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
9293
if (changeInfo.status === 'complete' && tab.url && tab.url.match(urlPattern)) {
9394
setTimeout(() => {
94-
chrome.tabs.get(tabId, (updatedTab) => {
95-
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: updatedTab.title || 'title' });
95+
browser.tabs.get(tabId, (updatedTab) => {
96+
browser.tabs.sendMessage(tabId, { action: 'updateDescription', title: updatedTab.title || 'title' });
9697
});
9798
}, 1000);
9899
}
@@ -101,8 +102,8 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
101102
urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
102103
if (changeInfo.status === 'complete' && tab.url && tab.url.match(urlPattern)) {
103104
setTimeout(() => {
104-
chrome.tabs.get(tabId, (updatedTab) => {
105-
chrome.tabs.sendMessage(tabId, { action: 'addVideo', title: updatedTab.title || 'title' });
105+
browser.tabs.get(tabId, (updatedTab) => {
106+
browser.tabs.sendMessage(tabId, { action: 'addVideo', title: updatedTab.title || 'title' });
106107
});
107108
}, 1000);
108109
}
@@ -111,7 +112,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
111112
urlPattern = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
112113
if (changeInfo.status === 'complete' && tab.url && tab.url.match(urlPattern)) {
113114
setTimeout(() => {
114-
chrome.storage.local.set({ 'currentLeetCodeProblemTitle': tab.title || 'title' });
115+
browser.storage.local.set({ 'currentLeetCodeProblemTitle': tab.title || 'title' });
115116
}, 1000);
116117
}
117118
});

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

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

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

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

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

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

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

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

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

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

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

@@ -101,7 +103,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
101103
}>;
102104
}
103105

104-
chrome.storage.local.get(['leetcodeProblems'], (result) => {
106+
browser.storage.local.get(['leetcodeProblems'], (result) => {
105107
const problem = result.leetcodeProblems.questions.find((problem: problem) => problem.title === problemTitle);
106108
if (problem.companies && problem.companies.length > 0) {
107109
const topCompanies = problem.companies.slice(0, 5);
@@ -110,7 +112,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
110112
const button = document.createElement('button');
111113
// opens the company page when the button is clicked
112114
button.onclick = () => {
113-
chrome.runtime.sendMessage({
115+
browser.runtime.sendMessage({
114116
action: 'openCompanyPage', company: company.name,
115117
});
116118
};
@@ -157,7 +159,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
157159
return companyTagContainer;
158160
}
159161

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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+
57
const VIDEO_ASPECT_RATIO = 56.25; // 16:9 aspect ratio
68

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

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

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

src/popup/popup.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
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>
810
<script type="module" src="../../dist/popup/popup.js"></script>
911
</head>
1012

0 commit comments

Comments
 (0)