-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
124 lines (112 loc) · 3.82 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
// background.js
// Remove the injectCSS function entirely
/*
function injectCSS(tabId) {
return browser.tabs.insertCSS(tabId, {
file: 'lib/bootstrap.min.css'
}).catch(error => {
console.error('Error injecting CSS:', error);
});
}
*/
// Modify the injectScripts function to no longer inject CSS
function injectScripts(tabId, scripts) {
if (scripts.length === 0) {
// Remove the injectCSS call
return Promise.resolve();
}
const script = scripts.shift();
return browser.tabs.executeScript(tabId, {
file: script,
runAt: 'document_idle'
}).then(() => {
return injectScripts(tabId, scripts);
}).catch(error => {
console.error('Error injecting script:', script, error);
});
}
browser.webNavigation.onHistoryStateUpdated.addListener(details => {
console.log('History state updated:', details.url);
const scripts = [
'lib/jquery.min.js',
'lib/popper.min.js',
'lib/bootstrap.min.js',
'lib/vue.min.js',
'content.js'
];
injectScripts(details.tabId, scripts.slice());
}, {
url: [
{ hostEquals: 'find.library.upenn.edu' }
]
});
// Listen for completed navigation to ensure content script is injected
browser.webNavigation.onCompleted.addListener(details => {
console.log('Navigation completed:', details.url);
const scripts = [
'lib/jquery.min.js',
'lib/popper.min.js',
'lib/bootstrap.min.js',
'lib/vue.min.js',
'content.js'
];
injectScripts(details.tabId, scripts.slice());
}, {
url: [
{ hostEquals: 'find.library.upenn.edu' }
]
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
if (tab.url.includes('/catalog/')) {
// Extract MMSID from URL
const match = tab.url.match(/catalog\/(\d+)/);
if (match) {
const mmsid = match[1];
console.log('Extracted MMSID:', mmsid);
const apiUrl = `https://id.bibframe.app/app/lcnaf/${mmsid}`;
console.log('Fetching LCNAF API:', apiUrl);
fetch(apiUrl)
.then(res => res.json())
.then(data => {
console.log('LCNAF API response data:', data);
// Trim response values to avoid extraneous spaces
const qid = (data.qid && data.qid.trim()) || (data.authorQid && data.authorQid.trim()) || '';
console.log('Fetched qid:', qid);
if (qid) {
// Only update the sidebar for the item page if a valid qid is returned
const panelUrl = 'sidebar-item.html?qid=' + encodeURIComponent(qid) + '&t=' + new Date().getTime();
chrome.sidebarAction.setPanel({ panel: panelUrl });
} else {
console.log('No valid qid returned; keeping the previous sidebar.');
}
})
.catch(err => {
console.error('Error fetching LCNAF data:', err);
console.log('Keeping the previous sidebar due to fetch error.');
});
} else {
console.error('No MMSID match found in URL.');
// Do nothing to retain previous sidebar
}
} else if (
tab.url.includes('/search') ||
tab.url.includes('/documents') ||
tab.url.includes('?search_field') ||
tab.url.match(/[?&]q=/) ||
tab.url.includes('?page=')
) {
// Extract the "q" parameter from the active tab URL
const urlObj = new URL(tab.url);
const qParam = urlObj.searchParams.get('q') || '';
// Append a timestamp to force panel reloading (avoid caching)
const panelUrl = 'sidebar-search.html?q=' + encodeURIComponent(qParam) + '&t=' + new Date().getTime();
chrome.sidebarAction.setPanel({ panel: panelUrl });
}
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
chrome.runtime.sendMessage({ type: 'update-sidebar', url: tab.url });
}
});