Skip to content

Commit

Permalink
fix: avoid console errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Dec 22, 2023
1 parent 7795ad4 commit 9efb5b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ chrome.runtime.onMessage.addListener(({ type, data }, sender, sendResponse) => {
ScreenshotMaker.capture(sender.tab!.windowId).then((data) => {
sendResponse(data);
});
// this indicates, that we send response asynchronously
return true;
case 'TOLGEE_SET_STATE':
setStateIcon(data, sender.tab!.id!);
return false;
sendResponse();
break;
default:
return false;
sendResponse();
}
return false;
});

const setStateIcon = (state: State, tabId: number) => {
Expand Down
12 changes: 4 additions & 8 deletions src/content/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ type Listener = {
callback: (data) => void;
};

type RuntimeCallbackType = (
data,
responseCallback: (response?) => void
) => void;
type RuntimeCallbackType = (data) => Promise<any>;

type RuntimeListener = {
type: string;
Expand All @@ -28,7 +25,6 @@ export class Messages {

readonly startWindowListening = () => {
const receiveMessage = (event: PgEvent) => {
console.log(event.data);
if (event.source !== window) {
return;
}
Expand All @@ -47,11 +43,12 @@ export class Messages {
// noinspection JSDeprecatedSymbols
chrome.runtime.onMessage.addListener(
(request: RuntimeMessage, sender, sendResponse) => {
this.listenersRuntime.forEach((listener) => {
this.listenersRuntime.forEach(async (listener) => {
if (listener.type == request.type) {
listener.callback(request.data, sendResponse);
sendResponse(await listener.callback(request.data));
}
});
return true;
}
);
};
Expand All @@ -69,7 +66,6 @@ export class Messages {
};

readonly sendToPlugin = (type: string, data?: any) => {
console.log({ type, data });
return new Promise((resolve) => {
chrome.runtime.sendMessage({ type, data }, (data) => resolve(data));
});
Expand Down
10 changes: 3 additions & 7 deletions src/content/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ messages.listenWindow('TOLGEE_TAKE_SCREENSHOT', () => {
messages.startRuntimeListening();

// popup will ask if tolgee is present on the page
messages.listenRuntime('DETECT_TOLGEE', (data, sendResponse) => {
sendResponse();
messages.listenRuntime('DETECT_TOLGEE', async () => {
if (configuration) {
messages.sendToPlugin('TOLGEE_CONFIG_LOADED', configuration);
}
});

messages.listenRuntime('GET_CREDENTIALS', (data, sendResponse) =>
sendResponse(getAppliedCredenials())
);
messages.listenRuntime('GET_CREDENTIALS', async () => getAppliedCredenials());

messages.listenRuntime('SET_CREDENTIALS', (data, sendResponse) => {
messages.listenRuntime('SET_CREDENTIALS', async (data) => {
if (data.apiKey) {
sessionStorage.setItem(API_KEY_LOCAL_STORAGE, data.apiKey);
} else {
Expand All @@ -76,6 +73,5 @@ messages.listenRuntime('SET_CREDENTIALS', (data, sendResponse) => {
sessionStorage.removeItem(API_URL_LOCAL_STORAGE);
}
location.reload();
sendResponse(true);
updateState(configuration, messages);
});

0 comments on commit 9efb5b3

Please sign in to comment.