-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbackground.js
More file actions
161 lines (149 loc) · 4.61 KB
/
background.js
File metadata and controls
161 lines (149 loc) · 4.61 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
let originTabId = null;
let originFrameId = null;
/**
* Handles the clipboard helper window creation
* @param {Object} request - The request object from the content script
* @param {Object} sender - The sender information
* @param {Function} sendResponse - The callback function
*/
function handleOpenClipboardHelper(request, sender, sendResponse) {
originTabId = sender.tab.id;
originFrameId = request.frameId;
chrome.windows.create(
{
url: chrome.runtime.getURL("clipboard-helpers/clipboard-helper.html"),
type: "popup",
width: 500,
height: 400,
},
function (window) {
if (chrome.runtime.lastError) {
console.error(
"Error creating clipboard helper window:",
chrome.runtime.lastError.message
);
sendResponse({
success: false,
error: chrome.runtime.lastError.message,
});
} else {
console.log("Clipboard helper window created with id:", window.id);
sendResponse({ success: true });
}
}
);
}
/**
* Handles clipboard data transfer to content script
* @param {Object} request - The request object containing file data
* @param {Function} sendResponse - The callback function
*/
function handleFileDataTransfer(request, sendResponse) {
chrome.tabs.sendMessage(
originTabId,
{
fileDataUrl: request.fileDataUrl,
mimeType: request.mimeType,
frameId: originFrameId,
},
(response) => {
if (chrome.runtime.lastError) {
console.error(
`Error in sending message to content script: ${chrome.runtime.lastError.message}`
);
} else {
console.log("Content script response:", response);
}
closeHelperTabAndFocusOrigin();
}
);
}
/**
* Closes the helper tab and focuses back on the origin tab
*/
function closeHelperTabAndFocusOrigin() {
chrome.tabs.query(
{ url: chrome.runtime.getURL("clipboard-helpers/clipboard-helper.html") },
function (tabs) {
if (tabs.length > 0) {
chrome.tabs.remove(tabs[0].id, () => {
if (chrome.runtime.lastError) {
console.error(
"Failed to close the helper tab:",
chrome.runtime.lastError.message
);
}
});
}
if (originTabId) {
chrome.tabs.update(originTabId, { active: true }, () => {
if (chrome.runtime.lastError) {
console.error(
"Failed to activate the origin tab:",
chrome.runtime.lastError.message
);
}
});
}
}
);
}
/**
* Handles site preference saving
* @param {Object} request - The request object with site and enabled properties
* @param {Function} sendResponse - The callback function
*/
function handleSaveSitePreference(request, sendResponse) {
const { site, enabled } = request;
chrome.storage.sync.get({ sitePreferences: {} }, (data) => {
const sitePreferences = data.sitePreferences;
sitePreferences[site] = enabled;
chrome.storage.sync.set({ sitePreferences }, () => {
if (chrome.runtime.lastError) {
console.error(
"Error saving site preference:",
chrome.runtime.lastError.message
);
sendResponse({
success: false,
error: chrome.runtime.lastError.message,
});
} else {
sendResponse({ success: true });
}
});
});
}
/**
* Handles site preference retrieval
* @param {Object} request - The request object with site property
* @param {Function} sendResponse - The callback function
*/
function handleGetSitePreference(request, sendResponse) {
const { site } = request;
chrome.storage.sync.get({ sitePreferences: {} }, (data) => {
const sitePreferences = data.sitePreferences;
sendResponse({ enabled: sitePreferences[site] });
});
}
// Main message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Background script received a message:", request);
// Handle different message types
if (request.action === "openClipboardHelper") {
handleOpenClipboardHelper(request, sender, sendResponse);
return true; // Keep the message channel open for async response
} else if (request.fileDataUrl) {
handleFileDataTransfer(request, sendResponse);
return true;
} else if (request.closeTab) {
closeHelperTabAndFocusOrigin();
return true;
} else if (request.action === "saveSitePreference") {
handleSaveSitePreference(request, sendResponse);
return true;
} else if (request.action === "getSitePreference") {
handleGetSitePreference(request, sendResponse);
return true;
}
});