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

View file

@ -223,8 +223,21 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
case "copyToClipboard":
navigator.clipboard.writeText(request.text);
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;
}
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 {
found: boolean;

View file

@ -180,6 +180,36 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.refreshSegmentsButton.addEventListener("click", refreshSegments);
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
const disableSkipping = Config.config.disableSkipping;
if (disableSkipping != undefined && disableSkipping) {