1- // Create context menu on installation
2- chrome . runtime . onInstalled . addListener ( ( ) => {
3- chrome . contextMenus . create ( {
4- id : "askExtension" ,
5- title : "Generate Quiz with Selected Text" ,
6- contexts : [ "selection" ]
7- } ) ;
1+ // Create context menus
2+ chrome . contextMenus . create ( {
3+ id : "askExtension" ,
4+ title : "Generate Quiz with Selected Text" ,
5+ contexts : [ "selection" ]
86 } ) ;
9-
10- // Handle context menu clicks
11- chrome . contextMenus . onClicked . addListener ( async ( info , tab ) => {
7+
8+ // YouTube-specific quiz generator
9+ chrome . contextMenus . create ( {
10+ id : "generateQuizFromYouTube" ,
11+ title : "Generate Quiz from YouTube Video" ,
12+ contexts : [ "page" ] ,
13+ documentUrlPatterns : [ "*://www.youtube.com/watch?v=*" ]
14+ } ) ;
15+
16+ // Handle context menu clicks
17+ chrome . contextMenus . onClicked . addListener ( async ( info , tab ) => {
1218 if ( info . menuItemId === "askExtension" && info . selectionText ) {
1319 try {
1420 // Store the selected text first
@@ -51,10 +57,43 @@ chrome.runtime.onInstalled.addListener(() => {
5157 console . error ( "Error in context menu handler:" , error ) ;
5258 }
5359 }
54- } ) ;
55-
56- // Listen for messages from content script
57- chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
60+
61+ if ( info . menuItemId === "generateQuizFromYouTube" ) {
62+ try {
63+ // Check if script has already been injected
64+ const [ { result } ] = await chrome . scripting . executeScript ( {
65+ target : { tabId : tab . id } ,
66+ func : ( ) => window . youtubeScriptLoaded || false
67+ } ) ;
68+
69+ if ( ! result ) {
70+ await chrome . scripting . executeScript ( {
71+ target : { tabId : tab . id } ,
72+ files : [ "youtubeContentScript.js" ]
73+ } ) ;
74+ } else {
75+ console . log ( "YouTube script already loaded" ) ;
76+ }
77+ // Add badge notification
78+ chrome . action . setBadgeText ( {
79+ text : "!"
80+ } ) ;
81+ chrome . action . setBadgeBackgroundColor ( {
82+ color : "#FF005C"
83+ } ) ;
84+
85+ // Clear the badge after 2 seconds
86+ setTimeout ( ( ) => {
87+ chrome . action . setBadgeText ( { text : "" } ) ;
88+ } , 2000 ) ;
89+ } catch ( error ) {
90+ console . error ( "Error in YouTube quiz generation handler:" , error ) ;
91+ }
92+ }
93+ } ) ;
94+
95+ // Listen for messages from content script
96+ chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
5897 if ( request . type === "TEXT_SELECTED" ) {
5998 chrome . storage . local . set ( {
6099 selectedText : request . text
@@ -89,12 +128,3 @@ chrome.storage.onChanged.addListener((changes, namespace) => {
89128 }
90129} ) ;
91130
92-
93- // Optional: Handle extension install/update
94- chrome . runtime . onInstalled . addListener ( ( details ) => {
95- if ( details . reason === "install" ) {
96- console . log ( "Extension installed" ) ;
97- } else if ( details . reason === "update" ) {
98- console . log ( "Extension updated" ) ;
99- }
100- } ) ;
0 commit comments