|
| 1 | +/** |
| 2 | + * DeepSeek-specific heuristics for editor and send button discovery. |
| 3 | + * Registered with the site heuristics registry so the Brain can pick it up when DeepSeek is active. |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +(function () { |
| 9 | + const isVisible = (el) => { |
| 10 | + if (!el) return false; |
| 11 | + const rect = el.getBoundingClientRect(); |
| 12 | + const style = window.getComputedStyle(el); |
| 13 | + return rect.width > 10 && |
| 14 | + rect.height > 10 && |
| 15 | + style.display !== 'none' && |
| 16 | + style.visibility !== 'hidden' && |
| 17 | + style.opacity !== '0'; |
| 18 | + }; |
| 19 | + |
| 20 | + const unique = (list) => { |
| 21 | + const seen = new Set(); |
| 22 | + return list.filter((el) => { |
| 23 | + if (!el) return false; |
| 24 | + if (seen.has(el)) return false; |
| 25 | + seen.add(el); |
| 26 | + return true; |
| 27 | + }); |
| 28 | + }; |
| 29 | + |
| 30 | + const detectEditor = async () => { |
| 31 | + // Note: this selector list mirrors the defaults in utils.js so heuristics can still |
| 32 | + // guess an editor when the Guard's configured selectors fail (e.g., class churn). |
| 33 | + const selectors = [ |
| 34 | + 'textarea[placeholder="Message DeepSeek"]', |
| 35 | + 'textarea[aria-label*="Message"]', |
| 36 | + 'textarea[placeholder*="Message"]', |
| 37 | + '[class*="chat-input"] textarea', |
| 38 | + '[class*="chat-input"] [contenteditable="true"]', |
| 39 | + 'textarea', |
| 40 | + 'div[contenteditable="true"]', |
| 41 | + 'textarea._27c9245', |
| 42 | + 'textarea.ds-scroll-area' |
| 43 | + ]; |
| 44 | + |
| 45 | + const candidates = unique( |
| 46 | + selectors.flatMap(sel => Array.from(document.querySelectorAll(sel))) |
| 47 | + ).filter(isVisible); |
| 48 | + |
| 49 | + if (candidates.length === 0) return null; |
| 50 | + |
| 51 | + const scored = candidates.map(el => { |
| 52 | + let score = 0; |
| 53 | + const placeholder = (el.getAttribute('placeholder') || '').toLowerCase(); |
| 54 | + const aria = (el.getAttribute('aria-label') || '').toLowerCase(); |
| 55 | + const cls = el.className || ''; |
| 56 | + |
| 57 | + if (placeholder === 'message deepseek') score += 12; |
| 58 | + else if (placeholder.includes('message') || aria.includes('message')) score += 8; |
| 59 | + if (cls.includes('ds-scroll-area') || cls.includes('_27c9245')) score += 2; |
| 60 | + if (el.tagName === 'TEXTAREA') score += 3; |
| 61 | + |
| 62 | + const footer = el.closest('[class*="editor"], [class*="footer"], form'); |
| 63 | + if (footer) score += 2; |
| 64 | + |
| 65 | + const rect = el.getBoundingClientRect(); |
| 66 | + score += rect.top; // prefer lower on the page |
| 67 | + |
| 68 | + return { el, score }; |
| 69 | + }); |
| 70 | + |
| 71 | + scored.sort((a, b) => b.score - a.score); |
| 72 | + return scored[0]?.el || null; |
| 73 | + }; |
| 74 | + |
| 75 | + const detectSendButton = async () => { |
| 76 | + const editor = await detectEditor().catch(() => null); |
| 77 | + const editorRect = editor ? editor.getBoundingClientRect() : null; |
| 78 | + |
| 79 | + const candidates = unique([ |
| 80 | + ...document.querySelectorAll('.ds-icon-button'), |
| 81 | + ...document.querySelectorAll('button'), |
| 82 | + ...document.querySelectorAll('[role="button"]'), |
| 83 | + ...document.querySelectorAll('[data-testid]') |
| 84 | + ]).filter(el => { |
| 85 | + if (!isVisible(el)) return false; |
| 86 | + const testId = (el.getAttribute('data-testid') || '').toLowerCase(); |
| 87 | + if (testId.startsWith('custom-send-button')) return false; // ignore our own buttons |
| 88 | + const ocpContainer = el.closest('[id*="custom-buttons-container"], [data-ocp-profile-selector]'); |
| 89 | + if (ocpContainer) return false; // avoid OCP UI |
| 90 | + return true; |
| 91 | + }); |
| 92 | + |
| 93 | + if (candidates.length === 0) return null; |
| 94 | + |
| 95 | + const scored = candidates.map(el => { |
| 96 | + let score = 0; |
| 97 | + const aria = (el.getAttribute('aria-label') || '').toLowerCase(); |
| 98 | + const title = (el.getAttribute('title') || '').toLowerCase(); |
| 99 | + const testId = (el.getAttribute('data-testid') || '').toLowerCase(); |
| 100 | + const text = (el.innerText || '').toLowerCase(); |
| 101 | + const cls = (el.className || '').toLowerCase(); |
| 102 | + |
| 103 | + const matchesSend = (str) => str.includes('send'); |
| 104 | + |
| 105 | + if (matchesSend(aria) || matchesSend(title) || matchesSend(testId) || matchesSend(text)) score += 10; |
| 106 | + if (cls.includes('ds-icon-button')) score += 5; |
| 107 | + if (el.querySelector('svg')) score += 4; |
| 108 | + |
| 109 | + const container = el.closest('[class*="button"], [class*="footer"], form'); |
| 110 | + if (container) score += 3; |
| 111 | + |
| 112 | + if (editorRect) { |
| 113 | + const rect = el.getBoundingClientRect(); |
| 114 | + const verticalProximity = Math.max(0, 200 - Math.abs(rect.top - editorRect.top)); |
| 115 | + const horizontalProximity = Math.max(0, 300 - Math.abs(rect.left - editorRect.right)); |
| 116 | + score += verticalProximity * 0.01; |
| 117 | + score += horizontalProximity * 0.01; |
| 118 | + if (rect.top >= editorRect.top) score += 2; // below or aligned with editor |
| 119 | + } |
| 120 | + |
| 121 | + if (el.disabled || el.getAttribute('aria-disabled') === 'true') { |
| 122 | + score -= 5; // avoid disabled matches that blocked recovery earlier |
| 123 | + } |
| 124 | + |
| 125 | + return { el, score }; |
| 126 | + }); |
| 127 | + |
| 128 | + scored.sort((a, b) => b.score - a.score); |
| 129 | + const best = scored[0]; |
| 130 | + if (best && best.score > 0) { |
| 131 | + return best.el; |
| 132 | + } |
| 133 | + return null; |
| 134 | + }; |
| 135 | + |
| 136 | + if (window.OneClickPromptsSiteHeuristics?.register) { |
| 137 | + window.OneClickPromptsSiteHeuristics.register('DeepSeek', { |
| 138 | + detectEditor, |
| 139 | + detectSendButton |
| 140 | + }); |
| 141 | + } |
| 142 | +})(); |
0 commit comments