Skip to content

Commit

Permalink
Use WebExtension API for cross-browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
wong2 committed Dec 5, 2022
1 parent a755854 commit a966d23
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"eventsource-parser": "^0.0.5",
"expiry-map": "^2.0.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"webextension-polyfill": "^0.10.0"
}
}
5 changes: 3 additions & 2 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v4 as uuidv4 } from "uuid";
import ExpiryMap from "expiry-map";
import { v4 as uuidv4 } from "uuid";
import Browser from "webextension-polyfill";
import { fetchSSE } from "./fetch-sse.mjs";

const KEY_ACCESS_TOKEN = "accessToken";
Expand Down Expand Up @@ -57,7 +58,7 @@ async function getAnswer(question, callback) {
});
}

chrome.runtime.onConnect.addListener((port) => {
Browser.runtime.onConnect.addListener((port) => {
port.onMessage.addListener(async (msg) => {
console.debug("received msg", msg);
try {
Expand Down
9 changes: 2 additions & 7 deletions src/content-script/index.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
function getMainContainerWidth() {
const resultContainer = document.getElementById("center_col");
if (resultContainer) {
return resultContainer.offsetWidth + "px";
}
}
import Browser from "webextension-polyfill";

async function run(question) {
const container = document.createElement("div");
Expand All @@ -18,7 +13,7 @@ async function run(question) {
document.getElementById("rcnt").appendChild(container);
}

const port = chrome.runtime.connect();
const port = Browser.runtime.connect();
port.onMessage.addListener(function (msg) {
if (msg.answer) {
container.innerHTML = `<p><span class="prefix">ChatGPT:</span><pre>${msg.answer}</pre></p>`;
Expand Down

0 comments on commit a966d23

Please sign in to comment.