summaryrefslogtreecommitdiff
path: root/source/features/selection-in-new-tab.tsx
blob: 58511ea1bfd7d89aef48d00192e24e3adcce427b (plain) (blame)
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
import select from 'select-dom';
import onetime from 'onetime';

import features from '.';
import {isEditable} from '../helpers/dom-utils';

function openInNewTab({key, target}: KeyboardEvent): void {
	const selected = select<HTMLAnchorElement>('.navigation-focus .js-navigation-open[href]');
	if (selected && key === 'O' && !isEditable(target)) {
		void browser.runtime.sendMessage({
			openUrls: [selected.href]
		});

		// Get the list element that contains the unread class and mark it as read.
		selected.closest('.unread')?.classList.replace('unread', 'read');
	}
}

function init(): void {
	document.addEventListener('keypress', openInNewTab);
}

void features.add(__filebasename, {
	shortcuts: {
		'shift o': 'Open selection in new tab'
	},
	awaitDomReady: false,
	init: onetime(init)
});