-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
98 lines (83 loc) · 2.28 KB
/
background.js
File metadata and controls
98 lines (83 loc) · 2.28 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
/**
* PasteAnyway Background Script
*
* Handles context menu creation and script injection.
*/
// === CONSTANTS ===
const BADGE_ERROR_COLOR = '#F44336';
const BADGE_CLEAR_DELAY = 3000;
/**
* Browser-specific restricted URL patterns.
* Extensions cannot run on these pages for security reasons.
*/
const RESTRICTED_PATTERNS = [
// Chrome
'chrome://',
'chrome-extension://',
'chrome.google.com/webstore',
// Firefox
'about:',
'moz-extension://',
'addons.mozilla.org',
// Edge
'edge://',
'microsoftedge.microsoft.com/addons'
];
// === HELPER FUNCTIONS ===
/**
* Checks if a URL is restricted (browser internal pages or extension stores).
*/
const isRestrictedUrl = (url = '') => {
if (!url) return false;
return RESTRICTED_PATTERNS.some(pattern => url.includes(pattern));
};
/**
* Shows an error badge on the extension icon.
*/
const showErrorBadge = () => {
chrome.action.setBadgeText({ text: '!' });
chrome.action.setBadgeBackgroundColor({ color: BADGE_ERROR_COLOR });
setTimeout(() => {
chrome.action.setBadgeText({ text: '' });
}, BADGE_CLEAR_DELAY);
};
/**
* Injects the content script and triggers paste from clipboard.
*/
const injectAndPaste = async (tabId) => {
// First inject the content script
await chrome.scripting.executeScript({
target: { tabId },
files: ['lib/content-script.js']
});
// Then call the pasteFromClipboard function
await chrome.scripting.executeScript({
target: { tabId },
func: () => window.__pasteAnyway.pasteFromClipboard()
});
};
// === EVENT HANDLERS ===
// Create context menu on install
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'paste-anyway',
title: 'Paste Anyway',
contexts: ['editable']
});
console.log('[PasteAnyway] Context menu created');
});
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId !== 'paste-anyway') return;
console.log('[PasteAnyway] Context menu clicked');
// Check for restricted pages
if (isRestrictedUrl(tab.url)) {
showErrorBadge();
console.log('[PasteAnyway] Cannot run on restricted page:', tab.url);
return;
}
// Inject and execute
injectAndPaste(tab.id).catch(error => {
console.error('[PasteAnyway] Error:', error);
});
});