diff --git a/extension/content.js b/extension/content.js index 0c9cf7a..899688f 100644 --- a/extension/content.js +++ b/extension/content.js @@ -610,14 +610,64 @@ function createTrackerInsertionMarker() { return marker; } +function isInteractiveContainer(element) { + return Boolean( + element + && element.matches + && element.matches('a, button, [role="button"]') + ); +} + +function normalizeTrackerInsertionAnchor(postElement, anchor) { + if (!postElement || !anchor || !anchor.isConnected || !postElement.contains(anchor)) { + return null; + } + + let current = anchor; + while (current && current !== postElement) { + const parent = current.parentElement; + if (!parent || !postElement.contains(parent)) { + break; + } + + if (isInteractiveContainer(current) || isInteractiveContainer(parent)) { + current = parent; + continue; + } + + break; + } + + return current; +} + +function insertMarkerBeforeAnchor(postElement, marker, anchor) { + const target = normalizeTrackerInsertionAnchor(postElement, anchor); + if (!target || !target.parentElement) { + return false; + } + + target.parentElement.insertBefore(marker, target); + return true; +} + +function insertMarkerAfterAnchor(postElement, marker, anchor) { + const target = normalizeTrackerInsertionAnchor(postElement, anchor); + if (!target || !target.parentElement) { + return false; + } + + target.parentElement.insertBefore(marker, target.nextSibling); + return true; +} + function positionTrackerInsertionMarker(postElement, marker, buttonBar, postNum = '?') { if (!postElement || !marker) { return false; } const commentComposerAnchor = findCommentComposerAnchorInPost(postElement); - if (commentComposerAnchor && commentComposerAnchor.parentElement) { - commentComposerAnchor.parentElement.insertBefore(marker, commentComposerAnchor); + if (commentComposerAnchor && insertMarkerBeforeAnchor(postElement, marker, commentComposerAnchor)) { setTrackerInsertionMarkerLocked(marker, true); console.log('[FB Tracker] Post #' + postNum + ' - Marker inserted before comment composer'); return true; @@ -627,21 +677,18 @@ function positionTrackerInsertionMarker(postElement, marker, buttonBar, postNum const actionAnchor = getDirectActionAnchorInPost(postElement, resolvedButtonBar); const commentBoundary = findCommentBoundaryAnchorInPost(postElement, actionAnchor); - if (commentBoundary && commentBoundary.parentElement) { - commentBoundary.parentElement.insertBefore(marker, commentBoundary); + if (commentBoundary && insertMarkerBeforeAnchor(postElement, marker, commentBoundary)) { setTrackerInsertionMarkerLocked(marker, true); console.log('[FB Tracker] Post #' + postNum + ' - Marker inserted before comment boundary'); return true; } - if (actionAnchor && actionAnchor.parentElement) { - actionAnchor.parentElement.insertBefore(marker, actionAnchor.nextSibling); + if (actionAnchor && insertMarkerAfterAnchor(postElement, marker, actionAnchor)) { setTrackerInsertionMarkerLocked(marker, true); return true; } - if (resolvedButtonBar && resolvedButtonBar.parentElement && postElement.contains(resolvedButtonBar.parentElement)) { - resolvedButtonBar.parentElement.insertBefore(marker, resolvedButtonBar.nextSibling); + if (resolvedButtonBar && insertMarkerAfterAnchor(postElement, marker, resolvedButtonBar)) { setTrackerInsertionMarkerLocked(marker, true); return true; } @@ -663,6 +710,10 @@ function isTrackerInsertionMarkerPlacementValid(postElement, marker, buttonBar) return false; } + if (marker.closest('a, button, [role="button"]')) { + return false; + } + const commentBoundary = findCommentBoundaryAnchorInPost(postElement); if (commentBoundary && isNodeAfter(marker, commentBoundary)) { @@ -3680,8 +3731,12 @@ async function createTrackerUI(postElement, buttonBar, postNum = '?', options = align-items: center; gap: 8px; row-gap: 6px; + flex: 0 0 100%; width: 100%; box-sizing: border-box; + position: relative; + z-index: 5; + pointer-events: auto; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; font-size: 13px; `;