Skip to content

Commit d457307

Browse files
authored
Merge pull request #30 from jacobtender/jrt-sidepanel
I'm seeing Marian on the side
2 parents 7acfc01 + 648f315 commit d457307

19 files changed

+277
-44
lines changed

src/background.js

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { isAllowedUrl } from "./shared/allowed-patterns";
22

33
function updateIcon(tabId, isAllowed) {
4-
// console.log(`Updating icon for tab ${tabId}: ${isAllowed ? 'allowed' : 'not allowed'}`);
54
chrome.action.setIcon({
65
tabId,
76
path: isAllowed
8-
? {
9-
128: "icons/icon.png"
10-
}
11-
: {
12-
128: "icons/icon-disabled.png",
13-
}
7+
? { 128: "icons/icon.png" }
8+
: { 128: "icons/icon-disabled.png" }
149
});
1510
}
1611

@@ -25,3 +20,62 @@ chrome.tabs.onActivated.addListener(({ tabId }) => {
2520
updateIcon(tabId, isAllowedUrl(tab.url));
2621
});
2722
});
23+
24+
function openSidebar(tab) {
25+
if (typeof chrome.sidePanel !== "undefined" && chrome.sidePanel.open) {
26+
// Chrome Side Panel API
27+
chrome.sidePanel.open({ windowId: tab.windowId });
28+
} else if (chrome.sidebarAction && chrome.sidebarAction.open) {
29+
// Firefox Sidebar API
30+
chrome.sidebarAction.open();
31+
} else {
32+
console.warn("No native sidebar API available.");
33+
}
34+
}
35+
36+
function sendWhenReady(msg, retries = 10, delay = 150) {
37+
function attempt(remaining) {
38+
chrome.runtime.sendMessage({ type: "ping" }, (response) => {
39+
if (response === "pong") {
40+
chrome.runtime.sendMessage(msg);
41+
} else if (remaining > 0) {
42+
setTimeout(() => attempt(remaining - 1), delay);
43+
}
44+
});
45+
}
46+
attempt(retries);
47+
}
48+
49+
function showUnsupportedNotification(tab) {
50+
try {
51+
chrome.notifications.create(
52+
// use a unique id so repeated clicks update the same notification
53+
"marian-unsupported",
54+
{
55+
type: "basic",
56+
iconUrl: "icons/icon-shush.png",
57+
title: "Shhhh...",
58+
message: "You cannot do that here!"
59+
},
60+
(id) => {
61+
setTimeout(() => chrome.notifications.clear(id || "marian-unsupported"), 3000);
62+
}
63+
);
64+
} catch (e) {
65+
console.warn("Notification failed:", e);
66+
}
67+
}
68+
69+
chrome.action.onClicked.addListener((tab) => {
70+
if (!tab?.url) return;
71+
72+
if (!isAllowedUrl(tab.url)) {
73+
showUnsupportedNotification(tab);
74+
return;
75+
}
76+
77+
openSidebar(tab);
78+
setTimeout(() => {
79+
sendWhenReady({ type: "REFRESH_SIDEBAR", url: tab.url });
80+
}, 300); // give the sidebar a moment to load
81+
});

src/extractors/amazon.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ async function getAmazonDetails() {
4040
bookDetails['Reading Format'] = 'Physical Book';
4141
}
4242

43-
// logMarian("bookDetails", audibleDetails);
44-
// logMarian("bookDetails", audibleDetails);
43+
// logMarian("bookDetails", bookDetails);
44+
// logMarian("audibleDetails", audibleDetails);
4545

4646
return {
4747
...bookDetails,

src/extractors/goodreads.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ async function getGoodreadsDetails() {
1212
bookDetails["imgScore"] = imgEl?.src ? await getImageScore(imgEl.src) : 0;
1313
bookDetails["Title"] = document.querySelector('[data-testid="bookTitle"]')?.innerText.trim();
1414

15-
const button = document.querySelector('.ContributorLinksList button[aria-label="Show all contributors"]');
16-
if (button) {
17-
button.click();
15+
const contributorsButton = document.querySelector('.ContributorLinksList button[aria-label="Show all contributors"]');
16+
if (contributorsButton) {
17+
contributorsButton.click();
18+
await delay(1500); // wait for contributors to load
19+
}
20+
21+
const detailsButton = document.querySelector('.BookDetails button[aria-label="Book details and editions"]');
22+
if (detailsButton) {
23+
detailsButton.click();
1824
await delay(1500); // wait for contributors to load
1925
}
2026

src/icons/.DS_Store

6 KB
Binary file not shown.

src/icons/icon-sad-full.png

3.53 MB
Loading

src/icons/icon-sad.png

20.5 KB
Loading

src/icons/icon-shush-full.png

659 KB
Loading

src/icons/icon-shush.png

19.7 KB
Loading
Lines changed: 1 addition & 0 deletions
Loading

src/icons/third-party/github.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)