Skip to content

Commit

Permalink
utils: sendMessageActiveTab return Promise
Browse files Browse the repository at this point in the history
Similar to b5f8fbf, modify the sendMessageActiveTab to return a Promise.
Goodbye callback hell!

Resolves #29
  • Loading branch information
kkuehlz committed Sep 15, 2019
1 parent c4ed8de commit be6cca3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 58 deletions.
14 changes: 6 additions & 8 deletions src/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

'use strict';

import { Request, sendMessageActiveTab } from '../utils/message';
import { Request, Response, sendMessageActiveTab } from '../utils/message';

const proxyTransformer = (proxyRequest: string): string => {
return proxyRequest[5].toLowerCase() + proxyRequest.slice(6);
Expand All @@ -40,16 +40,14 @@ chrome.runtime.onMessage.addListener(
sendResponse
): boolean => {
if (request.request.startsWith('proxy') && request.request.length > 5) {
sendMessageActiveTab(
{
(async (): Promise<void> => {
const r: Response = await sendMessageActiveTab({
request: proxyTransformer(request.request),
requestClass: request.requestClass,
data: request.data
},
(response: any): void => {
sendResponse(response);
}
);
});
sendResponse(r);
})();
return true;
} else {
sendResponse({ success: false });
Expand Down
63 changes: 28 additions & 35 deletions src/popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

'use strict';

import { sendMessageActiveTab } from '../utils/message';
import { sendMessageActiveTab, Response } from '../utils/message';
import * as keystore from '../utils/keystore';

window.addEventListener('DOMContentLoaded', (event: Event) => {
Expand Down Expand Up @@ -52,17 +52,14 @@ window.addEventListener('DOMContentLoaded', (event: Event) => {
) as HTMLElement;

if (debabbleIcon) {
debabbleIcon.addEventListener('click', (event: MouseEvent) => {
sendMessageActiveTab(
{
request: 'activateElementPicker',
requestClass: 'decryptionPicker',
data: null
},
(response: any): void => {
window.close();
}
);
debabbleIcon.addEventListener('click', async (event: MouseEvent) => {
const r: Response = await sendMessageActiveTab({
request: 'activateElementPicker',
requestClass: 'decryptionPicker'
});
if (r.success) {
window.close();
}
});
}

Expand Down Expand Up @@ -98,10 +95,10 @@ window.addEventListener('DOMContentLoaded', (event: Event) => {
async (kevent: Event): Promise<void> => {
const cleanedData: string = plaintext.value.trim();
if (cleanedData === '') {
sendMessageActiveTab(
{ request: 'clearInputBox', requestClass: 'injectInput' },
(response: any): void => {}
);
await sendMessageActiveTab({
request: 'clearInputBox',
requestClass: 'injectInput'
});
displaytext.value = '';
displayLength.innerText = displaytext.value.length.toString();
return;
Expand All @@ -111,33 +108,29 @@ window.addEventListener('DOMContentLoaded', (event: Event) => {
);
displaytext.value = babbledText;
displayLength.innerText = displaytext.value.length.toString();
sendMessageActiveTab(
{
request: 'tunnelCipherText',
requestClass: 'injectInput',
data: babbledText
},
(response: any): void => {}
);
await sendMessageActiveTab({
request: 'tunnelCipherText',
requestClass: 'injectInput',
data: babbledText
});
}
);

const isEnter = (event: KeyboardEvent) => {
return event.ctrlKey && (event.keyCode == 10 || event.keyCode == 13);
};

plaintext.addEventListener('keydown', (kevent: KeyboardEvent) => {
plaintext.addEventListener('keydown', async (kevent: KeyboardEvent) => {
if (isEnter(kevent)) {
sendMessageActiveTab(
{ request: 'submitCipherText', requestClass: 'injectInput' },
(response: any): void => {
if (response.success) {
plaintext.value = '';
displaytext.value = '';
displayLength.innerText = displaytext.value.length.toString();
}
}
);
const r: Response = await sendMessageActiveTab({
request: 'submitCipherText',
requestClass: 'injectInput'
});
if (r.success) {
plaintext.value = '';
displaytext.value = '';
displayLength.innerText = displaytext.value.length.toString();
}
}
});
}
Expand Down
30 changes: 15 additions & 15 deletions src/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ export interface Request {
}

export interface Response {
success: boolean;
data: any; // TODO: be more strict here?
success: boolean; // TODO: change to ENUM of possible errors
data: any;
}

export const sendMessage = (message: Request): Promise<any> => {
return new Promise<any>((resolve: (data: any) => void) => {
chrome.runtime.sendMessage(message, (response: Response): void => {
resolve(response);
});
return new Promise<any>((resolve: (_: any) => void) => {
chrome.runtime.sendMessage(message, resolve);
});
};

export const sendMessageActiveTab = (
message: any,
responseCallback?: (response: any) => void
): void => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const activeTab: chrome.tabs.Tab | null = tabs.length > 0 ? tabs[0] : null;
if (activeTab && activeTab.id) {
chrome.tabs.sendMessage(activeTab.id, message, responseCallback);
}
export const sendMessageActiveTab = (message: Request): Promise<any> => {
return new Promise<any>((resolve: (_: any) => void) => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const activeTab: chrome.tabs.Tab | null =
tabs.length > 0 ? tabs[0] : null;
if (activeTab && activeTab.id) {
chrome.tabs.sendMessage(activeTab.id, message, resolve);
} else {
resolve({ success: false });
}
});
});
};

0 comments on commit be6cca3

Please sign in to comment.