-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
67 lines (58 loc) · 1.93 KB
/
content.js
File metadata and controls
67 lines (58 loc) · 1.93 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
let siteConfigs = {};
let customShortcuts = {};
const sidebarStates = {};
// Load configurations
Promise.all([
fetch(chrome.runtime.getURL('sites.json')).then(r => r.json()),
chrome.storage.sync.get(['customShortcuts'])
]).then(([configs, storage]) => {
siteConfigs = configs;
customShortcuts = storage.customShortcuts || {};
bindShortcuts();
});
// Listen for shortcut changes
chrome.storage.onChanged.addListener((changes) => {
if (changes.customShortcuts) {
customShortcuts = changes.customShortcuts.newValue || {};
Mousetrap.reset();
bindShortcuts();
}
});
function getShortcut(hostname, position) {
const key = `${hostname}.${position}`;
return customShortcuts[key] || siteConfigs[hostname]?.sidebars[position]?.shortcut;
}
function toggleSidebar(position, config) {
const element = document.querySelector(config.selector);
if (!element) return;
const key = `${window.location.hostname}-${position}`;
sidebarStates[key] = !sidebarStates[key];
element.style.display = sidebarStates[key] ? 'none' : '';
// Find the grid/flex container and force it to single column
let container = element.parentElement;
while (container && container !== document.body) {
const style = getComputedStyle(container);
if (style.display === 'grid') {
container.style.gridTemplateColumns = sidebarStates[key] ? '1fr' : '';
break;
}
if (style.display === 'flex') {
container.style.flexDirection = sidebarStates[key] ? 'column' : '';
break;
}
container = container.parentElement;
}
}
function bindShortcuts() {
const hostname = window.location.hostname;
const siteConfig = siteConfigs[hostname];
if (!siteConfig) return;
for (const [position, config] of Object.entries(siteConfig.sidebars)) {
const shortcut = getShortcut(hostname, position);
Mousetrap.bind(shortcut, (e) => {
e.preventDefault();
toggleSidebar(position, config);
return false;
});
}
}