import './open-all-notifications.css'; import React from 'dom-chef'; import select from 'select-dom'; import delegate from 'delegate-it'; import * as pageDetect from 'github-url-detection'; import LinkExternalIcon from 'octicon/link-external.svg'; import features from '.'; const confirmationRequiredCount = 10; function getUnreadNotifications(container: ParentNode = document): HTMLAnchorElement[] { return select.all('.notification-unread', container); } function openNotifications({delegateTarget}: delegate.Event): void { const container = delegateTarget.closest('.js-notifications-group') ?? document; // Ask for confirmation const unreadNotifications = getUnreadNotifications(container); if ( unreadNotifications.length >= confirmationRequiredCount && !confirm(`This will open ${unreadNotifications.length} new tabs. Continue?`) ) { return; } void browser.runtime.sendMessage({ openUrls: unreadNotifications.map(element => element.querySelector('a')!.href) }); // Mark all as read for (const notification of unreadNotifications) { notification.classList.replace('notification-unread', 'notification-read'); } // Remove all now-useless buttons for (const button of select.all('.rgh-open-notifications-button', container)) { button.remove(); } } function addOpenReposButton(): void { for (const repository of select.all('.js-notifications-group')) { if (getUnreadNotifications(repository).length === 0) { continue; } select('.js-grouped-notifications-mark-all-read-button', repository)!.before( ); } } function addOpenAllButton(): void { // Selector works on: // https://github.com/notifications (Grouped by date) // https://github.com/notifications (Grouped by repo) // https://github.com/notifications?query=reason%3Acomment (which is an unsaved filter) select('.js-check-all-container .js-bulk-action-toasts ~ div .Box-header')!.append( ); } function update(): void { if (getUnreadNotifications().length > 0) { addOpenAllButton(); addOpenReposButton(); } } function init(): void { delegate(document, '.rgh-open-notifications-button', 'click', openNotifications); update(); } void features.add(__filebasename, { include: [ pageDetect.isNotifications ], init });