diff --git a/background.js b/background.js index 8db9c92..71d8aa2 100644 --- a/background.js +++ b/background.js @@ -11,15 +11,25 @@ chrome.runtime.onInstalled.addListener(() => { setupContextMenu() }) // handle context menu click chrome.contextMenus.onClicked.addListener((info, tab) => { console.log(info, tab); + + let extractedUrl = info.linkUrl || info.pageUrl; + if (info.menuItemId === "addnote") { console.log(`Adding the note "${info.selectionText}"`); - // we don't need a response, don't bother waiting for one - chrome.runtime.sendMessage({content: info.selectionText}); + // Open sidepanel to ensure connection is met for event listener + chrome.sidePanel.open({ tabId: tab.id }) + + // Ensure content scripts have time to activate + setTimeout(() => { + chrome.runtime.sendMessage({ + content: info.selectionText, + url: extractedUrl + }); + }, 200); } }) - // open panel onclick chrome.sidePanel .setPanelBehavior({ openPanelOnActionClick: true }) diff --git a/js/notes.js b/js/notes.js index d903752..b6eaad0 100644 --- a/js/notes.js +++ b/js/notes.js @@ -43,8 +43,8 @@ function deleteAllNotes() { * @param {object} insertAfter - the note that precedes the new note you're trying to add */ -function addNote(text, insertAfter) { - const title = titleInput.value || ""; +function addNote(text, titleText, insertAfter) { + const title = titleText || ""; const content = text === "" ? infoInput.value : text; infoInput.value = ""; // empty out the textbox titleInput.value = ""; diff --git a/js/sidepanel.js b/js/sidepanel.js index 8183f4b..0d46558 100644 --- a/js/sidepanel.js +++ b/js/sidepanel.js @@ -10,7 +10,8 @@ document.addEventListener("visibilitychange", _ => { saveNotesOrder(); saveFolde // context menu --> add new note chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { const content = request.content; + const title = request.url || "Untitled"; // make that new message if it's non-empty - if (content) addNote(content); + if (content) addNote(content, title); });