-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
86 lines (78 loc) · 2.71 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Create the context menu when the extension is installed or updated.
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "copyWithURL",
title: "Copy with URL",
contexts: ["selection"]
});
});
// Listen for clicks on the context menu.
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "copyWithURL" && tab.id) {
chrome.scripting.executeScript({
target: {
tabId: tab.id
},
function: copyWithURL
}, () => {
if (chrome.runtime.lastError) {
console.error(`Script injection failed: ${chrome.runtime.lastError.message}`);
}
});
}
});
// Listen for the Alt+C key command.
chrome.commands.onCommand.addListener((command) => {
if (command === "copy_with_url") {
chrome.tabs.query({
active: true,
currentWindow: true
}, (tabs) => {
if (tabs.length > 0 && tabs[0].id) {
chrome.scripting.executeScript({
target: {
tabId: tabs[0].id
},
function: copyWithURL
}, () => {
if (chrome.runtime.lastError) {
console.error(`Script injection failed: ${chrome.runtime.lastError.message}`);
}
});
}
});
}
});
// Function that copies selected text with the URL.
function copyWithURL() {
chrome.storage.sync.get(['prefix', 'format'], ({
prefix = '',
format = 'text'
}) => {
const selectedText = window.getSelection().toString().trim();
if (!selectedText) {
alert('Please select some text before copying.');
return;
}
const pageUrl = window.location.href;
const pageTitle = document.title;
// Prepare the content for each format.
const text = `${prefix}${pageTitle}\n${pageUrl}\n\n${selectedText}`;
const html = `<p><a href="${pageUrl}">${pageTitle}</a><br><br>${selectedText}</p>`;
const markdown = `[${pageTitle}](${pageUrl})\n\n${selectedText}`;
// Copy based on the user's selected format.
let contentToCopy;
if (format === 'html') {
contentToCopy = html;
} else if (format === 'markdown') {
contentToCopy = markdown;
} else {
contentToCopy = text;
}
navigator.clipboard.writeText(contentToCopy).then(() => {
console.log(`URL copied with selection in ${format} format!`);
}).catch(err => {
console.error('Failed to copy text: ', err);
});
});
}