i am ting to mimic side functionality in a website maybe block the text selection , i have added text selection copy to clipboard and a button ask ai but it is not working now your work is to identify how can we make this c# script robust and mimic side google chrome extension functionality "private readonly string enableCopyScript = @"
(function() {
    // --- Helper: Detect input/editor elements ---
    function isInputOrEditor(el) {
        if (!el) return false;
        if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.isContentEditable) return true;
        if (el.closest && el.closest('.ql-editor, .CodeMirror, [role=\"textbox\"], [contenteditable=\"true\"]')) return true;
        return false;
    }

    // --- Remove CSS rules blocking selection/pointer events ---
    try {
        document.querySelectorAll('[style]').forEach(function(el) {
            if (isInputOrEditor(el)) return;
            el.style.userSelect = 'text';
            el.style.webkitUserSelect = 'text';
            el.style.msUserSelect = 'text';
            el.style.MozUserSelect = 'text';
            el.style.pointerEvents = 'auto';
        });
        var blockClasses = [
            'no-select', 'noselect', 'unselectable', 'disable-select', 'overlay', 'blocker', 'modal-backdrop'
        ];
        blockClasses.forEach(function(cls) {
            document.querySelectorAll('.' + cls).forEach(function(el) {
                if (isInputOrEditor(el)) return;
                el.classList.remove(cls);
            });
        });
        for (var i = 0; i < document.styleSheets.length; ++i) {
            var sheet = document.styleSheets[i];
            var rules;
            try { rules = sheet.cssRules; } catch (e) { continue; }
            if (!rules) continue;
            for (var j = rules.length - 1; j >= 0; --j) {
                var rule = rules[j];
                if (rule.type === 1) {
                    if (
                        /user-select\s*:\s*none/i.test(rule.cssText) ||
                        /pointer-events\s*:\s*none/i.test(rule.cssText)
                    ) {
                        if (!/(input|textarea|\.ql-editor|\.CodeMirror|\[contenteditable\])/i.test(rule.selectorText)) {
                            try { sheet.deleteRule(j); } catch (e) {}
                        }
                    }
                }
            }
        }
        var style = document.createElement('style');
        style.innerHTML =
            '*:not(input):not(textarea):not([contenteditable=\"true\"]):not(.ql-editor):not(.CodeMirror) {' +
            'user-select: text !important;' +
            '-webkit-user-select: text !important;' +
            '-ms-user-select: text !important;' +
            '-moz-user-select: text !important;' +
            'pointer-events: auto !important;' +
            '}';
        document.head.appendChild(style);
    } catch (e) {}

    // --- Sider-like Ask AI floating button logic ---
    let aiBtn = null;
    let aiBtnMouseDown = false;
    let aiPanel = null;
    let lastSelection = '';
    let aiPanelOpen = false;
    let lastPollText = '';

    function removeAI() {
        if (aiBtn) {
            aiBtn.remove();
            aiBtn = null;
        }
        if (aiPanel) {
            aiPanel.remove();
            aiPanel = null;
            aiPanelOpen = false;
        }
    }

    function showAIButton(rect, text) {
        removeAI();
        aiBtn = document.createElement('button');
        aiBtn.textContent = 'Ask AI';
        aiBtn.setAttribute('type', 'button');
        aiBtn.style.position = 'fixed';
        aiBtn.style.zIndex = '2147483647';
        aiBtn.style.left = (rect.right + 8) + 'px';
        aiBtn.style.top = (rect.top - 4) + 'px';
        aiBtn.style.padding = '6px 14px';
        aiBtn.style.background = '#fff';
        aiBtn.style.color = '#222';
        aiBtn.style.border = '1px solid #bbb';
        aiBtn.style.borderRadius = '6px';
        aiBtn.style.boxShadow = '0 2px 8px rgba(0,0,0,0.12)';
        aiBtn.style.fontSize = '15px';
        aiBtn.style.cursor = 'pointer';
        aiBtn.style.userSelect = 'none';
        aiBtn.style.pointerEvents = 'auto';
        aiBtn.style.transition = 'opacity 0.15s';
        aiBtn.style.opacity = '0.98';
        aiBtn.style.maxWidth = '200px';
        aiBtn.style.overflow = 'hidden';
        aiBtn.style.whiteSpace = 'nowrap';
        aiBtn.style.textOverflow = 'ellipsis';
        aiBtn.style.outline = 'none';
        aiBtn.tabIndex = 0;

        aiBtn.addEventListener('mousedown', function(e) {
            aiBtnMouseDown = true;
        });
        aiBtn.addEventListener('mouseup', function(e) {
            aiBtnMouseDown = false;
        });
        aiBtn.addEventListener('mouseleave', function(e) {
            aiBtnMouseDown = false;
        });

        aiBtn.onclick = function(e) {
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation(); // Prevent React/SPA/global blockers
            aiBtnMouseDown = false;
            setTimeout(() => {
                showAIPanel(text, aiBtn);
            }, 50);
        };

        document.body.appendChild(aiBtn);
    }

    function showAIPanel(text, anchorBtn) {
        if (aiPanelOpen) return;
        aiPanelOpen = true;
        aiPanel = document.createElement('div');
        aiPanel.style.position = 'fixed';
        // --- Improved bounds check for panel positioning ---
        let btnRect = anchorBtn.getBoundingClientRect();
        let panelLeft = Math.min(btnRect.right + 8, window.innerWidth - 420);
        let panelTop = Math.max(4, btnRect.top - 4);
        aiPanel.style.left = panelLeft + 'px';
        aiPanel.style.top = panelTop + 'px';
        aiPanel.style.minWidth = '320px';
        aiPanel.style.maxWidth = '420px';
        aiPanel.style.background = '#fff';
        aiPanel.style.color = '#222';
        aiPanel.style.border = '1px solid #bbb';
        aiPanel.style.borderRadius = '8px';
        aiPanel.style.boxShadow = '0 4px 24px rgba(0,0,0,0.18)';
        aiPanel.style.padding = '18px 18px 18px 18px';
        aiPanel.style.fontSize = '15px';
        aiPanel.style.lineHeight = '1.6';
        aiPanel.style.maxHeight = '60vh';
        aiPanel.style.overflowY = 'auto';
        aiPanel.style.userSelect = 'text';
        aiPanel.style.pointerEvents = 'auto';
        aiPanel.style.transition = 'opacity 0.18s';
        aiPanel.style.opacity = '0.99';
        aiPanel.innerHTML = '<div style=\"font-weight:bold;margin-bottom:8px;\">AI Answer</div><div id=\"aiPanelContent\">Thinking...</div>';
        document.body.appendChild(aiPanel);
        // Simulate AI response (replace with your Gemini endpoint logic)
        setTimeout(function() {
            var content = aiPanel.querySelector('#aiPanelContent');
            if (content) content.textContent = 'This is a simulated AI answer for: \"' + text.slice(0, 120) + (text.length > 120 ? '...' : '') + '\"';
        }, 1200);
    }

    document.addEventListener('selectionchange', function() {
        if (aiBtnMouseDown) return;
        let sel = window.getSelection();
        if (!sel || sel.isCollapsed) {
            removeAI();
            lastSelection = '';
            return;
        }
        let text = sel.toString().trim();
        if (!text || text.length < 2) {
            removeAI();
            lastSelection = '';
            return;
        }
        let range = sel.getRangeAt(0);
        let rect = range.getBoundingClientRect();
        if (!rect || rect.width === 0 || rect.height === 0) {
            removeAI();
            lastSelection = '';
            return;
        }
        let anchorNode = sel.anchorNode && sel.anchorNode.nodeType === 3 ? sel.anchorNode.parentElement : sel.anchorNode;
        if (isInputOrEditor(anchorNode)) {
            removeAI();
            lastSelection = '';
            return;
        }
        if (text === lastSelection && aiBtn) return;
        lastSelection = text;
        showAIButton(rect, text);
    });

    // --- Debounced polling for selection (Sider-style robustness) ---
    setInterval(function() {
        let sel = window.getSelection();
        if (!sel || sel.isCollapsed) return;
        let text = sel.toString().trim();
        if (!text || text.length < 2 || text === lastPollText) return;
        let range = sel.getRangeAt(0);
        let rect = range.getBoundingClientRect();
        if (rect && rect.width > 0 && rect.height > 0) {
            lastPollText = text;
            showAIButton(rect, text);
        }
    }, 300);

    // Remove AI UI on scroll or click outside
    document.addEventListener('scroll', removeAI, true);
    document.addEventListener('mousedown', function(e) {
        if (aiBtn && e.target !== aiBtn && (!aiPanel || e.target !== aiPanel)) {
            removeAI();
        }
    }, true);
    window.addEventListener('blur', removeAI);

    // --- Escape key closes panel ---
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') removeAI();
    });

    // --- Optional: force pointer-events for debug (uncomment if needed) ---
    // document.querySelectorAll('*').forEach(el => el.style.pointerEvents = 'auto');

})();
";"