const inputTags = [ "input" , "textarea" , "select" ] ; const labelTags = [ "label" , "span" ] ; const attributes = [ "id" , "name" , "label-aria" , "placeholder" ] ; const invalidElement = chrome . i18n . getMessage ( "copyCustomFieldNameInvalidElement" ) ; const noUniqueIdentifier = chrome . i18n . getMessage ( "copyCustomFieldNameNotUnique" ) ; let clickedEl = null ; function getClickedElementIdentifier ( ) { if ( clickedEl == null ) { return invalidElement ; } const clickedTag = clickedEl . nodeName . toLowerCase ( ) ; let inputEl = null ; if ( labelTags . includes ( clickedTag ) ) { let inputId = null ; if ( clickedTag === "label" ) { inputId = clickedEl . getAttribute ( "for" ) ; } else { inputId = clickedEl . closest ( "label" ) ?. getAttribute ( "for" ) ; } inputEl = document . getElementById ( inputId ) ; } else { inputEl = clickedEl ; } if ( inputEl == null || ! inputTags . includes ( inputEl . nodeName . toLowerCase ( ) ) ) { return invalidElement ; } for ( const attr of attributes ) { const attributeValue = inputEl . getAttribute ( attr ) ; const selector = "[" + attr + '="' + attributeValue + '"]' ; if ( ! isNullOrEmpty ( attributeValue ) && document . querySelectorAll ( selector ) ?. length === 1 ) { return attributeValue ; } } return noUniqueIdentifier ; } function isNullOrEmpty ( s ) { return s == null || s === "" ; } document . addEventListener ( "contextmenu" , ( event ) => { clickedEl = event . target as HTMLElement ; } ) ; chrome . runtime . onMessage . addListener ( ( event ) => { if ( event . command === "getClickedElement" ) { const identifier = getClickedElementIdentifier ( ) ; chrome . runtime . sendMessage ( { command : "getClickedElementResponse" , sender : "contextMenuHandler" , identifier : identifier , } ) ; } } ) ;