diff --git a/src/content/components/CopyTranscriptButton.tsx b/src/content/components/CopyTranscriptButton.tsx index 42c7a728..e1768d25 100644 --- a/src/content/components/CopyTranscriptButton.tsx +++ b/src/content/components/CopyTranscriptButton.tsx @@ -1,26 +1,10 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import { getTranscript } from '../utils/transcript'; export const CopyTranscriptButton: React.FC = () => { const [status, setStatus] = useState< 'idle' | 'copying' | 'success' | 'error' >('idle'); - const [isLecturePage, setIsLecturePage] = useState(false); - - useEffect(() => { - const checkIfLecturePage = () => { - const url = window.location.href; - const learnMatch = url.match(/\/learn\/.*?\/lecture\//); - setIsLecturePage(!!learnMatch); - }; - - checkIfLecturePage(); - }, []); - - // Don't render the button if we're not on a lecture page - if (!isLecturePage) { - return null; - } const handleCopy = async () => { try { @@ -34,7 +18,6 @@ export const CopyTranscriptButton: React.FC = () => { await navigator.clipboard.writeText(transcript); setStatus('success'); - // Reset status after 2 seconds setTimeout(() => { setStatus('idle'); }, 2000); @@ -42,38 +25,32 @@ export const CopyTranscriptButton: React.FC = () => { console.error('Failed to copy transcript:', error); setStatus('error'); - // Reset status after 2 seconds setTimeout(() => { setStatus('idle'); }, 2000); } }; - const getButtonText = () => { - switch (status) { - case 'copying': - return 'Copying...'; - case 'success': - return 'Copied ✅'; - case 'error': - return 'Failed to copy'; - default: - return 'Copy Transcript'; - } - }; - return ( ); }; diff --git a/src/content/index.tsx b/src/content/index.tsx index b84dc97b..dc32ee8a 100644 --- a/src/content/index.tsx +++ b/src/content/index.tsx @@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react'; import { createRoot } from 'react-dom/client'; import { CopyTranscriptButton } from './components/CopyTranscriptButton'; import { findElement } from './utils/domUtils'; +import { shouldInitialize } from './utils/urlUtils'; import './styles.css'; const TABLIST_SELECTORS = [ @@ -41,6 +42,14 @@ const App = () => { const init = async () => { console.log('[Coursera Copilot] Initializing extension...'); + // Skip initialization for unsupported pages + if (!shouldInitialize(window.location.href)) { + console.log( + '[Coursera Copilot] Skipping initialization for unsupported page' + ); + return; + } + // Check if button already exists const existingButton = document.getElementById('coursera-copilot-button'); if (existingButton) { @@ -55,9 +64,11 @@ const init = async () => { if (tablist) { console.log('[Coursera Copilot] Found tablist, injecting button...'); - // Create a container but keep it minimal const container = document.createElement('div'); - container.className = 'inline-block'; // Keep it inline with other elements + // Add Coursera's classes and make it behave like their tab items + container.className = 'contents cds-button-container css-zgpty rounded-md'; + container.setAttribute('role', 'tab'); + container.setAttribute('id', 'transcript-copy-button-container'); tablist.appendChild(container); const root = createRoot(container); diff --git a/src/content/utils/urlUtils.ts b/src/content/utils/urlUtils.ts new file mode 100644 index 00000000..1669d83a --- /dev/null +++ b/src/content/utils/urlUtils.ts @@ -0,0 +1,7 @@ +export const isLecturePage = (url: string): boolean => { + return url.includes('/learn/') && url.includes('/lecture/'); +}; + +export const shouldInitialize = (url: string): boolean => { + return isLecturePage(url); +};