-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
226 lines (199 loc) · 6.93 KB
/
content.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Copyright (c) 2025 Mike McCracken
* MIT License - see LICENSE file in root directory
*/
// Get debug mode from storage
async function getDebugMode() {
return (await chrome.storage.sync.get(STORAGE_KEYS.DEBUG_MODE))[STORAGE_KEYS.DEBUG_MODE] || false;
}
// Get the user's selected browser from storage
async function getSelectedBrowser() {
const result = await chrome.storage.sync.get(STORAGE_KEYS.SELECTED_BROWSER);
if (await getDebugMode()) {
console.log('Click Interceptor - Retrieved browser from storage:', result);
}
return result[STORAGE_KEYS.SELECTED_BROWSER];
}
// Save the user's browser selection
async function saveSelectedBrowser(browser) {
if (await getDebugMode()) {
console.log('Click Interceptor - Saving browser selection:', browser);
}
await chrome.storage.sync.set({ [STORAGE_KEYS.SELECTED_BROWSER]: browser });
}
// Get the Finicky config from storage
async function getFinickyConfig() {
const result = await chrome.storage.sync.get(STORAGE_KEYS.CONFIG);
if (await getDebugMode()) {
console.log('Click Interceptor - Retrieved config from storage:', result);
}
return result[STORAGE_KEYS.CONFIG] || { handlers: [] };
}
// Get redirect to default setting from storage
async function getRedirectToDefault() {
const result = await chrome.storage.sync.get(STORAGE_KEYS.REDIRECT_TO_DEFAULT);
return result[STORAGE_KEYS.REDIRECT_TO_DEFAULT] || false;
}
// Convert http(s) URL to finicky(s) URL
function toFinickyUrl(url) {
return url.replace(/^https?:\/\//, match =>
match === 'https://' ? 'finickys://' : 'finicky://');
}
// Initialize and set up click handling
async function initialize() {
document.addEventListener('click', async (e) => {
const link = e.target.closest('a');
if (!link) return;
// Skip if we've already processed this click
if (link.dataset.finickyProcessed === 'true') {
link.dataset.finickyProcessed = ''; // Reset for future clicks
return;
}
const url = link.href;
if (!url) return;
const parsedUrl = new URL(url);
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') return;
// Prevent default immediately to ensure the link doesn't navigate
e.preventDefault();
const currentBrowser = await getSelectedBrowser();
if (!currentBrowser) {
// Create modal container
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 2147483647;
`;
// Create dialog box
const dialog = document.createElement('div');
dialog.style.cssText = `
background: #ffffff;
color: #000000;
border-radius: 8px;
padding: 20px;
max-width: 400px;
width: calc(100% - 40px);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
position: relative;
margin: 20px;
`;
dialog.innerHTML = `
<h2 style="margin: 0 0 16px 0; color: #000000; font-size: 18px;">Finicky Link Router</h2>
<p style="margin: 0 0 16px 0; color: #000000; font-size: 14px;">
This extension is not configured. Please configure it to start routing links to the correct browser.
</p>
<div style="display: flex; justify-content: flex-end; gap: 8px;">
<button id="configureBtn" style="
background: #0366d6;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
">Configure Now</button>
<button id="closeBtn" style="
background: #f1f2f3;
color: #000000;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
">Close</button>
</div>
`;
modal.appendChild(dialog);
document.body.appendChild(modal);
// Handle button clicks
return new Promise((resolve) => {
document.getElementById('configureBtn').addEventListener('click', () => {
chrome.runtime.sendMessage({ action: 'openOptionsPage' });
modal.remove();
resolve(true); // Proceed with original navigation
});
document.getElementById('closeBtn').addEventListener('click', () => {
modal.remove();
resolve(true); // Proceed with original navigation
});
// Close on background click
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.remove();
resolve(true); // Proceed with original navigation
}
});
}).then(() => {
window.location.href = url;
});
}
const config = await getFinickyConfig();
const targetBrowser = await getTargetBrowser(config, url);
const intercept = !!targetBrowser && targetBrowser !== currentBrowser;
const destination = intercept ? toFinickyUrl(url) : url;
if (await getDebugMode()) {
console.log('Link routing:', {
url,
destination,
currentBrowser,
targetBrowser,
willIntercept: intercept
});
alert(`Link routing:
URL: ${url}
Destination: ${destination}
Will intercept: ${intercept ? 'Yes' : 'No'}
Target browser: ${targetBrowser}
Current browser: ${currentBrowser}`);
}
// If we should use the default browser behavior, trigger the link naturally
if (!intercept) {
link.dataset.finickyProcessed = 'true';
// Create a new click event preserving the original event's properties
const clickEvent = new MouseEvent('click', {
bubbles: e.bubbles,
cancelable: e.cancelable,
view: e.view,
ctrlKey: e.ctrlKey,
altKey: e.altKey,
shiftKey: e.shiftKey,
metaKey: e.metaKey,
button: e.button,
buttons: e.buttons
});
link.dispatchEvent(clickEvent);
return;
}
// For intercepted links, redirect to Finicky
window.location.href = destination;
});
}
// Determine target browser based on Finicky rules
async function getTargetBrowser(config, url) {
if (!config || !config.handlers) return config.defaultBrowser;
for (const handler of config.handlers) {
if (handler.match && handler.browser) {
const pattern = handler.match;
if (typeof pattern === 'string' && url.includes(pattern)) {
return handler.browser;
} else if (pattern.type === 'regex') {
const regex = new RegExp(pattern.pattern, pattern.flags);
if (regex.test(url)) {
return handler.browser;
}
}
}
}
// Check redirect to default setting before returning default browser
const redirectToDefault = await getRedirectToDefault();
return redirectToDefault ? config.defaultBrowser : null;
}
// Start the extension
initialize();