Skip to content

Commit f66e7a9

Browse files
author
ntwigg
committed
identifyContextOf
1 parent 038710c commit f66e7a9

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

browser-extension/src/datamodel/handler-registry.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ export class HandlerRegistry {
2424
return null;
2525
}
2626

27+
identifyTextarea(textarea: HTMLTextAreaElement): TextareaInfo<any> | null {
28+
for (const handler of this.handlers) {
29+
try {
30+
const result = handler.identifyContextOf(textarea);
31+
if (result) {
32+
return result;
33+
}
34+
} catch (error) {
35+
console.warn('Handler failed to identify textarea:', error);
36+
}
37+
}
38+
return null;
39+
}
40+
2741
identifyAll(): TextareaInfo<any>[] {
2842
const allTextareas: TextareaInfo<any>[] = [];
2943

browser-extension/src/datamodel/textarea-handler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface TextareaInfo<T extends CommentContext = CommentContext> {
1212
export interface TextareaHandler<T extends CommentContext = CommentContext> {
1313
// Handler metadata
1414
forCommentTypes(): string[];
15+
// whenever a new `textarea` is added to any webpage, this method is called to try to find a handler for it
16+
identifyContextOf(textarea: HTMLTextAreaElement): TextareaInfo | null;
1517

1618
// Content script functionality
1719
identify(): TextareaInfo<T>[];
@@ -37,6 +39,7 @@ export abstract class BaseTextareaHandler<T extends CommentContext = CommentCont
3739
}
3840

3941
abstract forCommentTypes(): string[];
42+
abstract identifyContextOf(textarea: HTMLTextAreaElement): TextareaInfo<T> | null;
4043
abstract identify(): TextareaInfo<T>[];
4144
abstract extractContext(textarea: HTMLTextAreaElement): T | null;
4245
abstract determineType(textarea: HTMLTextAreaElement): string | null;

browser-extension/src/entrypoints/content.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ function initializeMaybe(textarea: HTMLTextAreaElement) {
4848
injectStyles()
4949
textarea.classList.add(CONFIG.ADDED_OVERTYPE_CLASS)
5050

51-
// Use registry to identify and handle this textarea
52-
const textareaInfos = registry.identifyAll().filter(info => info.element === textarea)
53-
for (const info of textareaInfos) {
54-
logger.debug('Identified textarea:', info.type, info.context.unique_key)
51+
// Use registry to identify and handle this specific textarea
52+
const textareaInfo = registry.identifyTextarea(textarea)
53+
if (textareaInfo) {
54+
logger.debug('Identified textarea:', textareaInfo.type, textareaInfo.context.unique_key)
5555
// TODO: Set up textarea monitoring and draft saving
56+
} else {
57+
logger.debug('No handler found for textarea')
5658
}
5759
} else {
5860
logger.debug('already activated textarea {}', textarea)

browser-extension/src/handlers/github-handler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ export class GitHubHandler extends BaseTextareaHandler<GitHubContext> {
3333
];
3434
}
3535

36+
identifyContextOf(textarea: HTMLTextAreaElement): TextareaInfo<GitHubContext> | null {
37+
// Only handle GitHub domains
38+
if (!window.location.hostname.includes('github')) {
39+
return null;
40+
}
41+
42+
const type = this.determineType(textarea);
43+
const context = this.extractContext(textarea);
44+
45+
if (type && context) {
46+
return { element: textarea, type, context };
47+
}
48+
49+
return null;
50+
}
51+
3652
identify(): TextareaInfo<GitHubContext>[] {
3753
const textareas = document.querySelectorAll<HTMLTextAreaElement>('textarea');
3854
const results: TextareaInfo<GitHubContext>[] = [];

browser-extension/src/handlers/reddit-handler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ export class RedditHandler extends BaseTextareaHandler<RedditContext> {
2424
];
2525
}
2626

27+
identifyContextOf(textarea: HTMLTextAreaElement): TextareaInfo<RedditContext> | null {
28+
// Only handle Reddit domains
29+
if (!window.location.hostname.includes('reddit')) {
30+
return null;
31+
}
32+
33+
const type = this.determineType(textarea);
34+
const context = this.extractContext(textarea);
35+
36+
if (type && context) {
37+
return { element: textarea, type, context };
38+
}
39+
40+
return null;
41+
}
42+
2743
identify(): TextareaInfo<RedditContext>[] {
2844
const textareas = document.querySelectorAll<HTMLTextAreaElement>('textarea');
2945
const results: TextareaInfo<RedditContext>[] = [];

0 commit comments

Comments
 (0)