diff --git a/README.md b/README.md index 6c7e16e..35df113 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ For any issues, bugs or concerns; please [Open an Issue](https://github.com/cssn * Extract Links from Selected Text on any Site * Extract Links from Clipboard or Any Text * Open Multiple Links in Tabs from Text +* Copy the Text from a Link via Context Menu * Quick Filter Links with a Regular Expression * Store Regular Expressions for Quick Filtering * Automatic Dark/Light Mode based on Browser Setting diff --git a/manifest.json b/manifest.json index f2eafa4..bb37f32 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "description": "Easily extract, parse, or open all links/domains from a site or text with optional filters.", "homepage_url": "https://link-extractor.cssnr.com/", "author": "Shane", - "version": "0.1.3", + "version": "0.1.4", "manifest_version": 3, "commands": { "_execute_action": { diff --git a/src/js/exports.js b/src/js/exports.js index 276aaae..4978f68 100644 --- a/src/js/exports.js +++ b/src/js/exports.js @@ -6,14 +6,15 @@ * @param {Array} patterns */ export function createContextMenus(patterns) { - const ctx = ['page', 'link', 'image', 'selection'] + const ctx = ['page', 'link', 'selection'] const contexts = [ - // ['link', 'link', 'Copy Text to Clipboard'], + [['link'], 'copy', 'Copy Link Text to Clipboard'], [['selection'], 'selection', 'Extract from Selection'], + [['selection', 'link'], 'separator', 'seperator-top'], [ctx, 'filters', 'Extract with Filter'], [ctx, 'links', 'Extract All Links'], [ctx, 'domains', 'Extract All Domains'], - [ctx, 'separator', 'separator-1'], + [ctx, 'separator', 'separator-bottom'], [ctx, 'options', 'Open Options'], ] for (const context of contexts) { diff --git a/src/js/service-worker.js b/src/js/service-worker.js index 365665f..8234ce7 100644 --- a/src/js/service-worker.js +++ b/src/js/service-worker.js @@ -35,7 +35,7 @@ async function onInstalled() { /** * On Context Menu Click Callback * @function onClicked - * @param {Object} ctx + * @param {OnClickData} ctx */ async function onClicked(ctx) { console.log('ctx:', ctx) @@ -57,6 +57,9 @@ async function onClicked(ctx) { const { patterns } = await chrome.storage.sync.get(['patterns']) console.log(`filter: ${patterns[i]}`) await injectTab(patterns[i], null, null) + } else if (ctx.menuItemId === 'copy') { + console.log('injectFunction: copy') + await injectFunction(copyActiveElementText, null) } else { console.error(`Unknown ctx.menuItemId: ${ctx.menuItemId}`) } @@ -75,3 +78,40 @@ async function onCommand(command) { console.error(`Unknown command: ${command}`) } } + +/** + * Copy Text of Active Element of DOM + * @function copyActiveElementText + */ +function copyActiveElementText() { + // console.log('document.activeElement:', document.activeElement) + let text = + document.activeElement.innerText?.trim() || + document.activeElement.title?.trim() || + document.activeElement.firstElementChild?.alt?.trim() || + document.activeElement.ariaLabel?.trim() + console.log(`text: "${text}"`) + if (text.length) { + navigator.clipboard.writeText(text).then() + } else { + console.warn('No Text Found to Copy.') + } +} + +/** + * Inject Function into Current Tab with args + * @function injectFunction + * @param {Function} func + * @param {Array} args + */ +async function injectFunction(func, args) { + const [tab] = await chrome.tabs.query({ + active: true, + currentWindow: true, + }) + await chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: func, + args: args, + }) +}