-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
172 lines (146 loc) · 5.44 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
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
162
163
164
165
166
167
168
169
170
171
172
// Copyright (C) 2024 Seth Cottle
// This file is part of Sanitize It.
// Sanitize It is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any later version.
// Sanitize It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Please see the
// GNU General Public License for more details.
console.log('Background script loaded');
chrome.action.onClicked.addListener((tab) => {
console.log('Extension icon clicked');
sanitizeAndUpdateUrl(tab);
});
function sanitizeAndUpdateUrl(tab) {
console.log('sanitizeAndUpdateUrl function called');
let url = new URL(tab.url);
console.log('Original URL:', url.toString());
// Remove everything after '?'
url.search = '';
// Remove ref parameters from the pathname
let newPathname = url.pathname.replace(/\/ref\/.*$/, '');
newPathname = newPathname.replace(/\/ref=.*$/, '');
url.pathname = newPathname;
// Remove hash
url.hash = '';
const sanitizedUrl = url.toString();
console.log('Sanitized URL:', sanitizedUrl);
// Update the current tab with the sanitized URL
chrome.tabs.update(tab.id, { url: sanitizedUrl }, () => {
console.log('Tab updated with sanitized URL');
// Wait for the page to load before injecting the content script
chrome.tabs.onUpdated.addListener(function listener(tabId, info) {
if (tabId === tab.id && info.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
console.log('Page loaded, copying to clipboard');
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: copyToClipboard,
args: [sanitizedUrl]
}).then((results) => {
console.log('Clipboard operation completed, showing notification');
const copySucceeded = results[0].result;
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: showNotification,
args: [
copySucceeded ? 'URL sanitized and copied to clipboard!' : 'URL sanitized, but copying to clipboard failed.',
!copySucceeded // isError flag
]
});
}).catch((error) => {
console.error('Error during clipboard operation:', error);
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: showNotification,
args: ['URL sanitized, but an error occurred while trying to copy.', true] // isError flag
});
});
}
});
});
}
function copyToClipboard(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(text).then(() => {
console.log('URL copied to clipboard using Clipboard API');
return true;
}).catch(err => {
console.error('Failed to copy using Clipboard API:', err);
return fallbackCopyToClipboard(text);
});
} else {
console.log('Clipboard API not available, using fallback method');
return fallbackCopyToClipboard(text);
}
}
function fallbackCopyToClipboard(text) {
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "fixed"; // Avoid scrolling to bottom
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
const successful = document.execCommand('copy');
const msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
document.body.removeChild(textArea);
return successful;
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
document.body.removeChild(textArea);
return false;
}
}
function showNotification(message, isError = false) {
console.log('Showing notification:', message);
// Create container for shadow DOM
const container = document.createElement('div');
container.style.cssText = `
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 2147483647;
`;
// Create shadow DOM
const shadow = container.attachShadow({mode: 'closed'});
// Create notification element
const notification = document.createElement('div');
notification.textContent = message;
// Create style element
const style = document.createElement('style');
style.textContent = `
.notification {
background-color: ${isError ? '#FFC387' : '#327834'};
color: ${isError ? 'black' : 'white'};
padding: 16px;
border-radius: 4px;
font-family: Arial, sans-serif;
font-size: 16px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
text-align: center;
min-width: 200px;
max-width: 80%;
}
`;
// Add style and notification to shadow DOM
shadow.appendChild(style);
shadow.appendChild(notification);
// Add class to notification
notification.className = 'notification';
// Add container to body
document.body.appendChild(container);
console.log('Notification added to the page');
setTimeout(() => {
notification.style.opacity = '0';
notification.style.transition = 'opacity 0.5s';
setTimeout(() => {
document.body.removeChild(container);
console.log('Notification removed from the page');
}, 500);
}, 3000);
}
console.log('Background script setup complete');