Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
96 lines (86 loc) · 2.65 KB
/
Copy pathbackground.js
File metadata and controls
96 lines (86 loc) · 2.65 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
// Check if URL is restricted (can't inject content scripts)
function isRestrictedUrl(url) {
return !url ||
url.startsWith('chrome://') ||
url.startsWith('chrome-extension://') ||
url.startsWith('https://chrome.google.com/webstore') ||
url.startsWith('edge://') ||
url.startsWith('about:');
}
// Listen for keyboard shortcut
chrome.commands.onCommand.addListener(async (command) => {
if (command === 'open-tab-portal') {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab?.id) return;
// Check for restricted URLs
if (isRestrictedUrl(tab.url)) {
// Open as popup window instead
chrome.windows.create({
url: 'popup.html',
type: 'popup',
width: 600,
height: 500,
focused: true
});
return;
}
// Try to toggle existing modal first
try {
const response = await chrome.tabs.sendMessage(tab.id, { action: 'toggle' });
if (response?.success) return; // Modal toggled successfully
} catch (e) {
// Content script not injected yet, inject it
}
// Inject content script and CSS
try {
await chrome.scripting.insertCSS({
target: { tabId: tab.id },
files: ['content.css']
});
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
} catch (e) {
console.error('Failed to inject Tab Portal:', e);
}
}
});
// Handle messages from content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'getTabs') {
getAllTabs().then(sendResponse);
return true; // Keep channel open for async response
}
if (message.action === 'switchToTab') {
chrome.tabs.update(message.tabId, { active: true });
chrome.windows.update(message.windowId, { focused: true });
sendResponse({ success: true });
}
if (message.action === 'deleteTab') {
chrome.tabs.remove(message.tabId).then(() => {
sendResponse({ success: true });
}).catch(() => {
sendResponse({ success: false });
});
return true;
}
});
async function getAllTabs() {
const tabs = await chrome.tabs.query({});
const windows = await chrome.windows.getAll();
const windowNames = new Map();
windows.forEach((win, index) => {
windowNames.set(win.id, `Window ${index + 1}`);
});
return tabs.map(tab => ({
id: tab.id,
windowId: tab.windowId,
title: tab.title || 'Untitled',
url: tab.url || '',
favIconUrl: tab.favIconUrl,
active: tab.active,
lastAccessed: tab.lastAccessed || 0,
windowName: windowNames.get(tab.windowId) || 'Window'
}));
}