diff --git a/browser-extension/src/entrypoints/content.ts b/browser-extension/src/entrypoints/content.ts index b49267f..c21c33a 100644 --- a/browser-extension/src/entrypoints/content.ts +++ b/browser-extension/src/entrypoints/content.ts @@ -1,5 +1,5 @@ import { CONFIG } from '../lib/config' -import type { CommentEvent, CommentSpot } from '../lib/enhancer' +import type { CommentEvent, CommentSpot, StrippedLocation } from '../lib/enhancer' import { logger } from '../lib/logger' import { EnhancerRegistry, TextareaRegistry } from '../lib/registries' @@ -9,6 +9,18 @@ const enhancedTextareas = new TextareaRegistry() // Expose for debugging in har:view ;(window as any).gitcassoTextareaRegistry = enhancedTextareas +function detectLocation(): StrippedLocation { + if ((window as any).gitcassoMockLocation) { + return (window as any).gitcassoMockLocation + } + const result = { + host: window.location.host, + pathname: window.location.pathname, + } + logger.debug('[gitcasso] detectLocation called, returning:', result) + return result +} + function sendEventToBackground(type: 'ENHANCED' | 'DESTROYED', spot: CommentSpot): void { const message: CommentEvent = { spot, @@ -26,6 +38,7 @@ enhancedTextareas.setEventHandlers( export default defineContentScript({ main() { + logger.debug('Main was called') const textAreasOnPageLoad = document.querySelectorAll(`textarea`) for (const textarea of textAreasOnPageLoad) { enhanceMaybe(textarea) @@ -35,7 +48,7 @@ export default defineContentScript({ childList: true, subtree: true, }) - logger.debug('Extension loaded with', enhancers.getEnhancerCount, 'handlers') + logger.debug('Extension loaded with', enhancers.getEnhancerCount(), 'handlers') }, matches: [''], runAt: 'document_end', @@ -80,6 +93,7 @@ function handleMutations(mutations: MutationRecord[]): void { } function enhanceMaybe(textarea: HTMLTextAreaElement) { + logger.debug('[gitcasso] enhanceMaybe called for textarea:', textarea.id, textarea.className) if (enhancedTextareas.get(textarea)) { logger.debug('textarea already registered {}', textarea) return @@ -89,7 +103,9 @@ function enhanceMaybe(textarea: HTMLTextAreaElement) { injectStyles() try { - const enhancedTextarea = enhancers.tryToEnhance(textarea) + const location = detectLocation() + logger.debug('[gitcasso] Calling tryToEnhance with location:', location) + const enhancedTextarea = enhancers.tryToEnhance(textarea, location) if (enhancedTextarea) { logger.debug( 'Identified textarea:', diff --git a/browser-extension/src/lib/enhancer.ts b/browser-extension/src/lib/enhancer.ts index 5c9e95b..d835c4c 100644 --- a/browser-extension/src/lib/enhancer.ts +++ b/browser-extension/src/lib/enhancer.ts @@ -19,6 +19,15 @@ export interface CommentEvent { draft?: string } +/** + * Minimal location information that enhancers need for routing decisions. + * Avoids dependency on global window/location objects for better testability. + */ +export interface StrippedLocation { + host: string + pathname: string +} + /** Wraps the textareas of a given platform with Gitcasso's enhancements. */ export interface CommentEnhancer { /** Guarantees to only return a type within this list. */ @@ -27,7 +36,7 @@ export interface CommentEnhancer { * Whenever a new `textarea` is added to any webpage, this method is called. * If we return non-null, then we become the handler for that text area. */ - tryToEnhance(textarea: HTMLTextAreaElement): Spot | null + tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): Spot | null /** This gets called the first time that `tryToEnhance` returns non-null. */ prepareForFirstEnhancement(): void /** diff --git a/browser-extension/src/lib/enhancers/CommentEnhancerMissing.tsx b/browser-extension/src/lib/enhancers/CommentEnhancerMissing.tsx index a470feb..f59a6e6 100644 --- a/browser-extension/src/lib/enhancers/CommentEnhancerMissing.tsx +++ b/browser-extension/src/lib/enhancers/CommentEnhancerMissing.tsx @@ -1,6 +1,6 @@ import type { OverTypeInstance } from 'overtype' import type { ReactNode } from 'react' -import type { CommentEnhancer, CommentSpot } from '../enhancer' +import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../enhancer' /** Used when an entry is in the table which we don't recognize. */ export class CommentEnhancerMissing implements CommentEnhancer { @@ -40,7 +40,7 @@ export class CommentEnhancerMissing implements CommentEnhancer { forSpotTypes(): string[] { throw new Error('Method not implemented.') } - tryToEnhance(_textarea: HTMLTextAreaElement): CommentSpot | null { + tryToEnhance(_textarea: HTMLTextAreaElement, _location: StrippedLocation): CommentSpot | null { throw new Error('Method not implemented.') } prepareForFirstEnhancement(): void { diff --git a/browser-extension/src/lib/enhancers/github/githubIssueAddComment.tsx b/browser-extension/src/lib/enhancers/github/githubIssueAddComment.tsx index e3b195d..233508d 100644 --- a/browser-extension/src/lib/enhancers/github/githubIssueAddComment.tsx +++ b/browser-extension/src/lib/enhancers/github/githubIssueAddComment.tsx @@ -1,7 +1,7 @@ import { IssueOpenedIcon } from '@primer/octicons-react' import OverType, { type OverTypeInstance } from 'overtype' import type React from 'react' -import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer' +import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer' import { logger } from '@/lib/logger' import { modifyDOM } from '../modifyDOM' import { commonGithubOptions } from './ghOptions' @@ -20,19 +20,22 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer } - tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea | null { + tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): EnhancedTextarea | null { for (const enhancer of this.enhancers) { try { - const spot = enhancer.tryToEnhance(textarea) + const spot = enhancer.tryToEnhance(textarea, location) if (spot) { // Prepare enhancer on first use if (!this.preparedEnhancers.has(enhancer)) { diff --git a/browser-extension/tests/har-view.ts b/browser-extension/tests/har-view.ts index 80cbba9..6586eba 100644 --- a/browser-extension/tests/har-view.ts +++ b/browser-extension/tests/har-view.ts @@ -315,30 +315,23 @@ function injectGitcassoScript(key: keyof typeof PAGES, html: string) { const contentScriptTag = `