Forward key presses from popup

This commit is contained in:
Ajay 2022-06-06 17:09:34 -04:00
parent d9970bf110
commit 32a3cb2cfe
4 changed files with 63 additions and 6 deletions

View file

@ -84,7 +84,7 @@ chrome.runtime.onMessage.addListener(function (request, _, callback) {
case "unregisterContentScript": case "unregisterContentScript":
unregisterFirefoxContentScript(request.id) unregisterFirefoxContentScript(request.id)
return false; return false;
case "tabs": case "tabs": {
chrome.tabs.query({ chrome.tabs.query({
active: true, active: true,
currentWindow: true currentWindow: true
@ -92,11 +92,13 @@ chrome.runtime.onMessage.addListener(function (request, _, callback) {
chrome.tabs.sendMessage( chrome.tabs.sendMessage(
tabs[0].id, tabs[0].id,
request.data, request.data,
(response) => callback(response) (response) => {
callback(response);
}
); );
} });
);
return true; return true;
}
} }
}); });

View file

@ -223,8 +223,21 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
case "copyToClipboard": case "copyToClipboard":
navigator.clipboard.writeText(request.text); navigator.clipboard.writeText(request.text);
break; break;
case "keydown":
document.dispatchEvent(new KeyboardEvent('keydown', {
key: request.key,
keyCode: request.keyCode,
code: request.code,
which: request.which,
shiftKey: request.shiftKey,
ctrlKey: request.ctrlKey,
altKey: request.altKey,
metaKey: request.metaKey
}));
break;
} }
sendResponse({});
} }
/** /**

View file

@ -47,7 +47,19 @@ interface CopyToClipboardMessage {
text: string; text: string;
} }
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage | SubmitVoteMessage | HideSegmentMessage | CopyToClipboardMessage); interface KeyDownMessage {
message: "keydown";
key: string;
keyCode: number;
code: string;
which: number;
shiftKey: boolean;
ctrlKey: boolean;
altKey: boolean;
metaKey: boolean;
}
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage | SubmitVoteMessage | HideSegmentMessage | CopyToClipboardMessage | KeyDownMessage);
export interface IsInfoFoundMessageResponse { export interface IsInfoFoundMessageResponse {
found: boolean; found: boolean;

View file

@ -180,6 +180,36 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.refreshSegmentsButton.addEventListener("click", refreshSegments); PageElements.refreshSegmentsButton.addEventListener("click", refreshSegments);
PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => copyToClipboard(await utils.getHash(Config.config.userID))); PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => copyToClipboard(await utils.getHash(Config.config.userID)));
// Forward click events
if (window !== window.top) {
document.addEventListener("keydown", (e) => {
const target = e.target as HTMLElement;
if (target.tagName === "INPUT"
|| target.tagName === "TEXTAREA"
|| e.key === "ArrowUp"
|| e.key === "ArrowDown") {
return;
}
if (e.key === " ") {
// No scrolling
e.preventDefault();
}
sendTabMessage({
message: "keydown",
key: e.key,
keyCode: e.keyCode,
code: e.code,
which: e.which,
shiftKey: e.shiftKey,
ctrlKey: e.ctrlKey,
altKey: e.altKey,
metaKey: e.metaKey
});
});
}
//show proper disable skipping button //show proper disable skipping button
const disableSkipping = Config.config.disableSkipping; const disableSkipping = Config.config.disableSkipping;
if (disableSkipping != undefined && disableSkipping) { if (disableSkipping != undefined && disableSkipping) {