@@ -32,6 +32,7 @@ const createHarmonyUrl = ({ questions, instrument_name }) => {
3232
3333// Create context menu item
3434chrome . runtime . onInstalled . addListener ( ( ) => {
35+ // Create different menu items for PDFs and regular pages
3536 chrome . contextMenus . create ( {
3637 id : "sendToHarmony" ,
3738 title : "Send to Harmony" ,
@@ -41,22 +42,9 @@ chrome.runtime.onInstalled.addListener(() => {
4142 chrome . storage . local . set ( { history : [ ] } ) ;
4243} ) ;
4344
44- // Function to find or create Harmony tab
45- async function findOrCreateHarmonyTab ( url ) {
46- // First, try to find an existing tab with our target name in the URL
47- const tabs = await chrome . tabs . query ( { } ) ;
48- const harmonyTab = tabs . find (
49- ( tab ) => tab . url && tab . url . includes ( harmonyURL )
50- ) ;
51-
52- if ( harmonyTab ) {
53- // Update existing tab
54- await chrome . tabs . update ( harmonyTab . id , { url : url , active : true } ) ;
55- await chrome . windows . update ( harmonyTab . windowId , { focused : true } ) ;
56- } else {
57- // Create new tab
58- await chrome . tabs . create ( { url : url } ) ;
59- }
45+ // Function to open Harmony URL in new tab
46+ function openHarmonyTab ( url ) {
47+ chrome . tabs . create ( { url : url } ) ;
6048}
6149
6250// Listen for messages from popup
@@ -65,52 +53,40 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
6553 findOrCreateHarmonyTab ( request . url ) ;
6654 return true ;
6755 }
68- if ( request . action === "returnCopied " ) {
69- findOrCreateHarmonyTab ( request . url ) ;
56+ if ( request . action === "processPdfText " ) {
57+ processSelection ( request . text , request . tab ) ;
7058 return true ;
7159 }
7260} ) ;
7361
74- chrome . contextMenus . onClicked . addListener ( function ( info , tab ) {
62+ chrome . contextMenus . onClicked . addListener ( async function ( info , tab ) {
7563 if ( info . menuItemId === "sendToHarmony" ) {
76- if ( tab ?. id > - 1 ) {
77- chrome . scripting
78- . executeScript ( {
79- target : { tabId : tab . id } ,
80- function : ( ) => {
81- const selection = document . getSelection ( ) ;
82- return selection ? selection . toString ( ) : "" ;
83- } ,
84- } )
85- . then ( ( resultArray ) => {
86- const result = resultArray [ 0 ] ;
87- const selectedText =
88- result && result && result . result ? result . result : "" ; // Handle various result possibilities
89- processSelection ( selectedText , tab ) ;
90- } )
91- . catch ( ( error ) => {
92- console . error ( "Error getting selected text:" , error ) ;
93- } ) ;
94- } else {
95- // If tab.id is null (eg in a PDF), trigger the copy command and then read from clipboard
96- chrome . tabs
97- . sendMessage ( {
98- action : "copySelection" ,
99- } )
100- . then ( ( response ) => {
101- if ( response ?. success ) {
102- try {
103- navigator . clipboard
104- . readText ( )
105- . then ( ( selectedText ) => processSelection ( selectedText , tab ) ) ;
106- } catch ( err ) {
107- console . error ( "Failed to read clipboard contents: " , err ) ;
108- }
109- }
110- } )
111- . catch ( ( error ) => {
112- console . error ( "Error executing copy command:" , error ) ;
113- } ) ;
64+ if ( tab ?. id === - 1 || tab ?. url ?. toLowerCase ( ) . includes ( "pdf" ) ) {
65+ // For PDF tabs, show popup
66+ chrome . action . openPopup ( ) ;
67+ return ;
68+ }
69+
70+ // For non-PDF tabs, use scripting API
71+ try {
72+ const resultArray = await chrome . scripting . executeScript ( {
73+ target : { tabId : tab . id } ,
74+ function : ( ) => {
75+ const selection = document . getSelection ( ) ;
76+ return selection ? selection . toString ( ) : "" ;
77+ } ,
78+ } ) ;
79+ const selectedText = resultArray [ 0 ] ?. result || "" ;
80+ if ( selectedText ) {
81+ processSelection ( selectedText , tab ) ;
82+ }
83+ } catch ( error ) {
84+ console . error ( "Error getting selected text:" , error ) ;
85+ chrome . action . setBadgeText ( { text : "!" } ) ;
86+ chrome . action . setBadgeBackgroundColor ( { color : "#F44336" } ) ;
87+ setTimeout ( ( ) => {
88+ chrome . action . setBadgeText ( { text : "" } ) ;
89+ } , 2000 ) ;
11490 }
11591 }
11692} ) ;
0 commit comments