Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions entrypoints/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { extractPageImageURL } from '../components/utils';
let lastURL: string | undefined;

export default defineBackground(() => {
console.log('Hello background!', { id: browser.runtime.id });
// Action API is conditionally available based on manifest v2 vs v3
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const actionAPI = browser.action || browser.browserAction;

// Move message listener setup to top level so it persists through service worker wake/sleep cycles
browser.runtime.onMessage.addListener((request: ScraperMessage, _sender, sendResponse) => {
Expand Down Expand Up @@ -42,23 +44,29 @@ export default defineBackground(() => {
console.error('Error updating page action:', e);
});

browser.declarativeContent.onPageChanged.removeRules(undefined, () => {
browser.declarativeContent.onPageChanged.addRules([
{
conditions: [
...sites.map(
(site) =>
new browser.declarativeContent.PageStateMatcher({
pageUrl: site.readerDomain,
}),
),
],
actions: [new browser.declarativeContent.ShowAction()],
},
]);
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
const url = new URL(tab.url);
const enabledSites = new Set(
sites.map((site) =>
typeof site.readerDomain === 'string'
? site.readerDomain
: site.readerDomain.hostEquals || site.readerDomain.hostContains,
),
);
const shouldShow =
enabledSites.has(url.hostname) ||
Array.from(enabledSites).some((domain) => domain && url.hostname.includes(domain));

if (shouldShow) {
actionAPI.enable(tabId).catch((e) => console.error('Error enabling action', e));
} else {
actionAPI.disable(tabId).catch((e) => console.error('Error disabling action', e));
}
}
});

browser.action.setBadgeBackgroundColor({ color: '#f45752' }).catch((e) => {
actionAPI.setBadgeBackgroundColor({ color: '#f45752' }).catch((e) => {
console.error('Error setting badge background color:', e);
});
});
Expand Down Expand Up @@ -110,7 +118,7 @@ export default defineBackground(() => {
updatePageAction().catch((e) => {
console.error('Error updating page action on tab update:', e);
});
browser.action.setBadgeBackgroundColor({ color: '#f45752' }).catch((e) => {
actionAPI.setBadgeBackgroundColor({ color: '#f45752' }).catch((e) => {
console.error('Error setting badge background color on tab update:', e);
});
}
Expand Down
12 changes: 11 additions & 1 deletion wxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ export default defineConfig({
__APP_VERSION__: JSON.stringify(version),
},
}),
manifest: () => {
manifest: ({ browser }) => {
const permissions: string[] = ['storage', 'scripting', 'tabs', 'webRequest', 'webRequestBlocking'];
switch (browser) {
case 'chrome':
permissions.push('declarativeContent');
break;
Comment on lines +18 to +20
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cover other chrome-like browsers? Arc, Zen, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik yes because they use the same engine

case 'firefox':
permissions.push('declarativeNetRequest');
break;
}
return {
permissions,
host_permissions: sites.map((site) => site.urlScope),
};
},
Expand Down