Skip to content

Commit 00805ed

Browse files
committed
Fix mocking the locaiton.
1 parent 96c464f commit 00805ed

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

browser-extension/src/entrypoints/content.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ const enhancedTextareas = new TextareaRegistry()
1010
;(window as any).gitcassoTextareaRegistry = enhancedTextareas
1111

1212
function detectLocation(): StrippedLocation {
13-
return {
13+
if ((window as any).gitcassoMockLocation) {
14+
return (window as any).gitcassoMockLocation
15+
}
16+
const result = {
1417
host: window.location.host,
1518
pathname: window.location.pathname,
1619
}
20+
logger.debug('[gitcasso] detectLocation called, returning:', result)
21+
return result
1722
}
1823

1924
function sendEventToBackground(type: 'ENHANCED' | 'DESTROYED', spot: CommentSpot): void {
@@ -33,6 +38,7 @@ enhancedTextareas.setEventHandlers(
3338

3439
export default defineContentScript({
3540
main() {
41+
logger.debug('Main was called')
3642
const textAreasOnPageLoad = document.querySelectorAll<HTMLTextAreaElement>(`textarea`)
3743
for (const textarea of textAreasOnPageLoad) {
3844
enhanceMaybe(textarea)
@@ -42,7 +48,7 @@ export default defineContentScript({
4248
childList: true,
4349
subtree: true,
4450
})
45-
logger.debug('Extension loaded with', enhancers.getEnhancerCount, 'handlers')
51+
logger.debug('Extension loaded with', enhancers.getEnhancerCount(), 'handlers')
4652
},
4753
matches: ['<all_urls>'],
4854
runAt: 'document_end',
@@ -87,6 +93,7 @@ function handleMutations(mutations: MutationRecord[]): void {
8793
}
8894

8995
function enhanceMaybe(textarea: HTMLTextAreaElement) {
96+
logger.debug('[gitcasso] enhanceMaybe called for textarea:', textarea.id, textarea.className)
9097
if (enhancedTextareas.get(textarea)) {
9198
logger.debug('textarea already registered {}', textarea)
9299
return
@@ -96,7 +103,9 @@ function enhanceMaybe(textarea: HTMLTextAreaElement) {
96103
injectStyles()
97104

98105
try {
99-
const enhancedTextarea = enhancers.tryToEnhance(textarea, detectLocation())
106+
const location = detectLocation()
107+
logger.debug('[gitcasso] Calling tryToEnhance with location:', location)
108+
const enhancedTextarea = enhancers.tryToEnhance(textarea, location)
100109
if (enhancedTextarea) {
101110
logger.debug(
102111
'Identified textarea:',

browser-extension/tests/har-view.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,10 @@ function injectGitcassoScript(key: keyof typeof PAGES, html: string) {
328328
'throw new Error("This script should only be loaded in a browser extension.")',
329329
'console.warn("Webextension-polyfill check bypassed for HAR testing")'
330330
);
331-
332-
// Override detectLocation function with correct domain and pathname
333-
patchedCode = patchedCode.replace(
334-
/function detectLocation(): StrippedLocation {[sS]*?}/,
335-
'function detectLocation() {\\n' +
336-
' return {\\n' +
337-
' host: \\'${urlParts.host}\\',\\n' +
338-
' pathname: \\'${urlParts.pathname}\\'\\n' +
339-
' };\\n' +
340-
'}'
341-
);
331+
window.gitcassoMockLocation = {
332+
host: '${urlParts.host}',
333+
pathname: '${urlParts.pathname}'
334+
};
342335
343336
// Execute the patched script with browser API mocks prepended
344337
const browserMocks = 'window.chrome=window.chrome||{runtime:{getURL:path=>"chrome-extension://gitcasso-test/"+path,onMessage:{addListener:()=>{}},sendMessage:()=>Promise.resolve(),id:"gitcasso-test"}};window.browser=window.chrome;';

0 commit comments

Comments
 (0)