2021-09-01 23:51:43 +02:00
|
|
|
const inputTags = ["input", "textarea", "select"];
|
2021-10-07 01:52:33 +02:00
|
|
|
const labelTags = ["label", "span"];
|
2021-09-01 23:51:43 +02:00
|
|
|
const attributes = ["id", "name", "label-aria", "placeholder"];
|
2021-10-07 01:52:33 +02:00
|
|
|
const invalidElement = chrome.i18n.getMessage("copyCustomFieldNameInvalidElement");
|
|
|
|
const noUniqueIdentifier = chrome.i18n.getMessage("copyCustomFieldNameNotUnique");
|
|
|
|
|
2021-09-01 23:51:43 +02:00
|
|
|
let clickedEl: HTMLElement = null;
|
|
|
|
|
|
|
|
// Find the best attribute to be used as the Name for an element in a custom field.
|
|
|
|
function getClickedElementIdentifier() {
|
|
|
|
if (clickedEl == null) {
|
2021-10-07 01:52:33 +02:00
|
|
|
return invalidElement;
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:15:55 +02:00
|
|
|
const clickedTag = clickedEl.nodeName.toLowerCase();
|
|
|
|
let inputEl = null;
|
2021-10-07 01:52:33 +02:00
|
|
|
|
2021-10-07 23:15:55 +02:00
|
|
|
// Try to identify the input element (which may not be the clicked element)
|
|
|
|
if (labelTags.includes(clickedTag)) {
|
|
|
|
let inputId = null;
|
|
|
|
if (clickedTag === "label") {
|
|
|
|
inputId = clickedEl.getAttribute("for");
|
|
|
|
} else {
|
|
|
|
inputId = clickedEl.closest("label")?.getAttribute("for");
|
2021-09-01 23:51:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 23:15:55 +02:00
|
|
|
inputEl = document.getElementById(inputId);
|
|
|
|
} else {
|
|
|
|
inputEl = clickedEl;
|
2021-09-01 23:51:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 23:15:55 +02:00
|
|
|
if (inputEl == null || !inputTags.includes(inputEl.nodeName.toLowerCase())) {
|
2021-10-07 01:52:33 +02:00
|
|
|
return invalidElement;
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 23:51:43 +02:00
|
|
|
for (const attr of attributes) {
|
2021-10-07 01:52:33 +02:00
|
|
|
const attributeValue = inputEl.getAttribute(attr);
|
2021-09-01 23:51:43 +02:00
|
|
|
const selector = "[" + attr + '="' + attributeValue + '"]';
|
|
|
|
if (!isNullOrEmpty(attributeValue) && document.querySelectorAll(selector)?.length === 1) {
|
|
|
|
return attributeValue;
|
|
|
|
}
|
2021-12-21 15:43:35 +01:00
|
|
|
}
|
2021-10-07 01:52:33 +02:00
|
|
|
return noUniqueIdentifier;
|
2021-09-01 23:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNullOrEmpty(s: string) {
|
|
|
|
return s == null || s === "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only have access to the element that's been clicked when the context menu is first opened.
|
|
|
|
// Remember it for use later.
|
|
|
|
document.addEventListener("contextmenu", (event) => {
|
|
|
|
clickedEl = event.target as HTMLElement;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Runs when the 'Copy Custom Field Name' context menu item is actually clicked.
|
2023-01-07 01:31:32 +01:00
|
|
|
chrome.runtime.onMessage.addListener((event, _sender, sendResponse) => {
|
2021-09-01 23:51:43 +02:00
|
|
|
if (event.command === "getClickedElement") {
|
|
|
|
const identifier = getClickedElementIdentifier();
|
2023-01-07 01:31:32 +01:00
|
|
|
if (sendResponse) {
|
|
|
|
sendResponse(identifier);
|
|
|
|
}
|
2024-02-02 21:13:37 +01:00
|
|
|
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2021-09-01 23:51:43 +02:00
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
command: "getClickedElementResponse",
|
|
|
|
sender: "contextMenuHandler",
|
|
|
|
identifier: identifier,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|