|
1 | | -import OverType from "../overtype/overtype"; |
| 1 | +import { CONFIG } from '../lib/config' |
| 2 | +import { logger } from '../lib/logger' |
| 3 | +import { EnhancerRegistry, TextareaRegistry } from '../lib/registries' |
| 4 | + |
| 5 | +const enhancers = new EnhancerRegistry() |
| 6 | +const enhancedTextareas = new TextareaRegistry() |
2 | 7 |
|
3 | 8 | export default defineContentScript({ |
4 | 9 | main() { |
5 | | - if (window.location.hostname !== "github.com") { |
6 | | - return; |
| 10 | + const textAreasOnPageLoad = document.querySelectorAll<HTMLTextAreaElement>(`textarea`) |
| 11 | + for (const textarea of textAreasOnPageLoad) { |
| 12 | + enhanceMaybe(textarea) |
7 | 13 | } |
| 14 | + const observer = new MutationObserver(handleMutations) |
| 15 | + observer.observe(document.body, { |
| 16 | + childList: true, |
| 17 | + subtree: true, |
| 18 | + }) |
| 19 | + logger.debug('Extension loaded with', enhancers.getEnhancerCount, 'handlers') |
| 20 | + }, |
| 21 | + matches: ['<all_urls>'], |
| 22 | + runAt: 'document_end', |
| 23 | +}) |
8 | 24 |
|
9 | | - const ghCommentBox = document.getElementById( |
10 | | - "new_comment_field" |
11 | | - ) as HTMLTextAreaElement | null; |
12 | | - if (ghCommentBox) { |
13 | | - const overtypeContainer = modifyDOM(ghCommentBox); |
14 | | - new OverType(overtypeContainer, { |
15 | | - placeholder: "Add your comment here...", |
16 | | - autoResize: true, |
17 | | - minHeight: "102px", |
18 | | - padding: "var(--base-size-8)", |
19 | | - }); |
| 25 | +function handleMutations(mutations: MutationRecord[]): void { |
| 26 | + for (const mutation of mutations) { |
| 27 | + for (const node of mutation.addedNodes) { |
| 28 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 29 | + const element = node as Element |
| 30 | + if (element.tagName === 'TEXTAREA') { |
| 31 | + enhanceMaybe(element as HTMLTextAreaElement) |
| 32 | + } else { |
| 33 | + // Also check for textareas within added subtrees |
| 34 | + const textareas = element.querySelectorAll?.('textarea') |
| 35 | + if (textareas) { |
| 36 | + for (const textarea of textareas) { |
| 37 | + enhanceMaybe(textarea) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
20 | 42 | } |
21 | | - }, |
22 | | - matches: ["<all_urls>"], |
23 | | - runAt: "document_end", |
24 | | -}); |
25 | | - |
26 | | -function modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement { |
27 | | - overtypeInput.classList.add("overtype-input"); |
28 | | - const overtypePreview = document.createElement("div"); |
29 | | - overtypePreview.classList.add("overtype-preview"); |
30 | | - overtypeInput.insertAdjacentElement("afterend", overtypePreview); |
31 | | - const overtypeWrapper = overtypeInput.parentElement!.closest("div")!; |
32 | | - overtypeWrapper.classList.add("overtype-wrapper"); |
33 | | - overtypeInput.placeholder = "Add your comment here..."; |
34 | | - const overtypeContainer = overtypeWrapper.parentElement!.closest("div")!; |
35 | | - overtypeContainer.classList.add("overtype-container"); |
36 | | - return overtypeContainer.parentElement!.closest("div")!; |
| 43 | + |
| 44 | + for (const node of mutation.removedNodes) { |
| 45 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 46 | + const element = node as Element |
| 47 | + if (element.tagName === 'TEXTAREA') { |
| 48 | + enhancedTextareas.unregisterDueToModification(element as HTMLTextAreaElement) |
| 49 | + } else { |
| 50 | + // Also check for textareas within removed subtrees |
| 51 | + const textareas = element.querySelectorAll?.('textarea') |
| 52 | + if (textareas) { |
| 53 | + for (const textarea of textareas) { |
| 54 | + enhancedTextareas.unregisterDueToModification(textarea) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function enhanceMaybe(textarea: HTMLTextAreaElement) { |
| 64 | + if (enhancedTextareas.get(textarea)) { |
| 65 | + logger.debug('textarea already registered {}', textarea) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + logger.debug('activating textarea {}', textarea) |
| 70 | + injectStyles() |
| 71 | + |
| 72 | + const enhancedTextarea = enhancers.tryToEnhance(textarea) |
| 73 | + if (enhancedTextarea) { |
| 74 | + logger.debug( |
| 75 | + 'Identified textarea:', |
| 76 | + enhancedTextarea.spot.type, |
| 77 | + enhancedTextarea.spot.unique_key, |
| 78 | + ) |
| 79 | + enhancedTextareas.register(enhancedTextarea) |
| 80 | + } else { |
| 81 | + logger.debug('No handler found for textarea') |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const STYLES = ` |
| 86 | +.${CONFIG.ADDED_OVERTYPE_CLASS} { |
| 87 | + background: cyan !important; |
| 88 | +} |
| 89 | +` |
| 90 | + |
| 91 | +function injectStyles(): void { |
| 92 | + if (!document.getElementById('gitcasso-styles')) { |
| 93 | + const style = document.createElement('style') |
| 94 | + style.textContent = STYLES |
| 95 | + style.id = 'gitcasso-styles' |
| 96 | + document.head.appendChild(style) |
| 97 | + } |
37 | 98 | } |
0 commit comments