Skip to content

Commit

Permalink
Copy Link Text (#28)
Browse files Browse the repository at this point in the history
* Copy Link Text

* Parsing Updates

* Cleanup
  • Loading branch information
smashedr authored Nov 11, 2023
1 parent 2c1ca23 commit 1a3955f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
7 changes: 4 additions & 3 deletions src/js/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 41 additions & 1 deletion src/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}`)
}
Expand All @@ -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,
})
}

0 comments on commit 1a3955f

Please sign in to comment.