-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
83 lines (76 loc) · 2.92 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
chrome.runtime.onInstalled.addListener(() => {
createContextMenu();
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "updateContextMenu") {
createContextMenu();
}
sendResponse({status: "ContextMenu updated"});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId.startsWith('template_')) {
const templateId = info.menuItemId.split('_')[1];
chrome.storage.local.get(['templates'], function(result) {
const template = result.templates ? result.templates[templateId] : null;
if (template && !tab.url.startsWith('chrome://')) {
chrome.scripting.executeScript({
target: {tabId: tab.id, allFrames: true},
function: insertTemplate,
args: [template]
});
}
});
} else if (info.menuItemId === 'manageTemplates') {
chrome.tabs.create({url: chrome.runtime.getURL("manage_templates.html")});
}
});
function createContextMenu() {
chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
id: 'manageClipboard',
title: 'Managed Clipboard',
contexts: ['all']
});
chrome.contextMenus.create({
id: 'manageTemplates',
title: '[ Manage Templates ]',
parentId: 'manageClipboard',
contexts: ['all']
});
updateTemplatesContextMenu();
});
}
function updateTemplatesContextMenu() {
chrome.storage.local.get(['templates'], function(result) {
const templates = result.templates || [];
templates.forEach((template, index) => {
chrome.contextMenus.create({
id: `template_${index}`,
title: template.name,
parentId: 'manageClipboard',
contexts: ['all']
});
});
});
}
function insertTemplate(template) {
var activeElement = document.activeElement;
console.log('Active Element:', activeElement); // Debugging
if (activeElement.tagName === "TEXTAREA" || activeElement.tagName === "INPUT" || activeElement.isContentEditable) {
if (activeElement.isContentEditable) {
// Handle contenteditable elements
document.execCommand('insertHTML', false, template.content);
} else {
// If it's a textarea or input, insert as plain text
activeElement.value += template.content;
}
} else {
var range = document.createRange();
var sel = document.getSelection();
range.setStart(activeElement, activeElement.childNodes.length);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
activeElement.appendChild(document.createTextNode(template.content));
}
}