-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
110 lines (100 loc) · 3.85 KB
/
content_script.js
File metadata and controls
110 lines (100 loc) · 3.85 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function getPageInfo() {
const url = window.location.href;
const title = document.title;
const selectedText = window.getSelection().toString();
return { url, title, selectedText };
}
function updatePopup(newContent, isInitial, useAlternateTemplate) {
let popup = document.getElementById('info-popup');
if (!popup) {
popup = document.createElement('textarea');
popup.id = 'info-popup';
popup.style.position = 'fixed';
popup.style.bottom = '10px';
popup.style.right = '10px';
popup.style.width = '300px';
popup.style.height= '200px';
popup.style.backgroundColor = 'white';
popup.style.border = '1px solid #ddd';
popup.style.boxShadow = '0px 0px 10px rgba(0,0,0,0.5)';
popup.style.padding = '15px';
popup.style.zIndex = '10000';
popup.style.fontSize = '14px';
popup.style.overflowY = 'auto';
popup.style.whiteSpace = 'pre-wrap';
// Append the popup to the body
document.body.appendChild(popup);
}
if (isInitial) {
popup.value = newContent;
let titleStart = 2
if(!useAlternateTemplate){
titleStart = 7
}
const titleEnd = newContent.indexOf('\n', titleStart);
//Pre-select the title
popup.setSelectionRange(titleStart, titleEnd);
} else if (newContent) {
popup.value += (popup.value ? '\n' : '') + `${newContent}`;
}
popup.style.display = 'block';
popup.focus();
}
function removePopup() {
let popup = document.getElementById('info-popup');
if (popup) {
//popup.style.display = 'none';
// Or, if you prefer to remove it completely:
popup.parentNode.removeChild(popup);
}
}
function formatSelectedText(text) {
return text.split('\n')
.map(line => {
// Remove leading dash if present
const trimmedLine = line.trimStart();
return ` - ${trimmedLine.startsWith('-') ? trimmedLine.substring(1).trimStart() : trimmedLine}`;
})
.join('\n');
}
function getCurrentDate() {
const date = new Date();
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // Months are zero-indexed
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.command === "toggleExtension" || request.command === "toggleAlternateExtension") {
let popup = document.getElementById('info-popup');
const useAlternateTemplate = request.command === "toggleAlternateExtension";
if (popup) {
// Popup exists, format and add selected text
const selectedText = window.getSelection().toString();
if (selectedText) {
const formattedText = formatSelectedText(selectedText);
updatePopup(formattedText, false, useAlternateTemplate);
}
} else {
// Popup doesn't exist, create with initial content
const pageInfo = getPageInfo();
let initialContent;
if (useAlternateTemplate) {
initialContent = `- ${pageInfo.title}\n created:: [[${getCurrentDate()}]]\n ref:: [web-source](${pageInfo.url})`;
} else {
initialContent = `- TODO ${pageInfo.title}\n created:: [[${getCurrentDate()}]]\n related-to:: \n deadline:: \n ref:: [web-source](${pageInfo.url})`;
}
if (pageInfo.selectedText) {
const formattedText = formatSelectedText(pageInfo.selectedText);
initialContent += `\n${formattedText}`;
}
updatePopup(initialContent, true, useAlternateTemplate);
}
} else if (request.command === "copyToClipboard") {
let popup = document.getElementById('info-popup');
if (popup) {
sendResponse(popup.value);
removePopup(); // Remove the popup after copying
}
}
});