-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
113 lines (104 loc) · 3.54 KB
/
Copy pathbackground.js
File metadata and controls
113 lines (104 loc) · 3.54 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
// background.js — tracks PDF tabs across sessions (in-memory only, no localStorage)
// In-memory store: tabId -> { url, title, tabId }
const pdfTabs = new Map();
function isPDF(url, title) {
if (!url) return false;
// Direct .pdf URL
if (/\.pdf(\?.*)?$/i.test(url)) return true;
// Google Docs PDF viewer
if (url.includes('docs.google.com/viewer') && url.includes('.pdf')) return true;
// Chrome built-in PDF viewer
if (url.startsWith('chrome-extension://') && url.includes('pdf')) return true;
// PDF.js viewer
if (url.includes('pdfjs') || url.includes('pdf.js')) return true;
// Firefox/browser inline viewer (blob or data URLs with pdf mime hint)
if ((url.startsWith('blob:') || url.startsWith('data:application/pdf'))) return true;
return false;
}
function updateTab(tab) {
if (!tab || !tab.id || tab.id < 0) return;
if (isPDF(tab.url, tab.title)) {
pdfTabs.set(tab.id, {
tabId: tab.id,
url: tab.url,
title: tab.title || tab.url || 'Untitled PDF',
favIconUrl: tab.favIconUrl || ''
});
} else {
pdfTabs.delete(tab.id);
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete') {
updateTab(tab);
}
});
chrome.tabs.onRemoved.addListener((tabId) => {
pdfTabs.delete(tabId);
});
chrome.tabs.onCreated.addListener((tab) => {
updateTab(tab);
});
// Refresh all current tabs on extension load
chrome.tabs.query({}, (tabs) => {
if (chrome.runtime.lastError) {
console.warn('chrome.tabs.query failed on init:', chrome.runtime.lastError);
return;
}
if (!tabs || !tabs.forEach) return;
tabs.forEach(updateTab);
});
// Message handler for popup
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === 'GET_PDF_TABS') {
// Also do a fresh query to catch anything we missed
chrome.tabs.query({}, (tabs) => {
if (chrome.runtime.lastError) {
console.warn('chrome.tabs.query failed in GET_PDF_TABS:', chrome.runtime.lastError);
// Respond with whatever we currently know
sendResponse({ pdfs: Array.from(pdfTabs.values()) });
return;
}
if (!tabs || !tabs.forEach) {
sendResponse({ pdfs: Array.from(pdfTabs.values()) });
return;
}
// Update our map with latest state
tabs.forEach(tab => {
if (isPDF(tab.url, tab.title)) {
pdfTabs.set(tab.id, {
tabId: tab.id,
url: tab.url,
title: tab.title || tab.url || 'Untitled PDF',
favIconUrl: tab.favIconUrl || ''
});
}
});
// Remove stale entries
const currentIds = new Set(tabs.map(t => t.id));
for (const id of pdfTabs.keys()) {
if (!currentIds.has(id)) pdfTabs.delete(id);
}
sendResponse({ pdfs: Array.from(pdfTabs.values()) });
});
return true; // async
}
if (msg.type === 'FOCUS_TAB') {
chrome.tabs.update(msg.tabId, { active: true }, () => {
if (chrome.runtime.lastError) {
console.warn('chrome.tabs.update failed in FOCUS_TAB:', chrome.runtime.lastError);
}
chrome.tabs.get(msg.tabId, (tab) => {
if (chrome.runtime.lastError) {
console.warn('chrome.tabs.get failed in FOCUS_TAB:', chrome.runtime.lastError);
return;
}
if (tab) chrome.windows.update(tab.windowId, { focused: true }, () => {
if (chrome.runtime.lastError) console.warn('chrome.windows.update failed in FOCUS_TAB:', chrome.runtime.lastError);
});
});
});
sendResponse({ ok: true });
return true;
}
});