summaryrefslogtreecommitdiff
path: root/source/features/download-folder-button.tsx
blob: 010891c400fe3901ac7a162becd707bdfd103227 (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
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
import './download-folder-button.css';

import React from 'dom-chef';
import {DownloadIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import observe from '../helpers/selector-observer.js';

function addLegacy(folderDropdown: HTMLElement): void {
	const downloadUrl = new URL('https://download-directory.github.io/');
	downloadUrl.searchParams.set('url', location.href);

	folderDropdown.before(
		<a
			className="rgh-download-folder-button btn tooltipped tooltipped-nw"
			aria-label="Download directory"
			href={downloadUrl.href}
		>
			<DownloadIcon/>
		</a>,
	);
}

function add({parentElement: deleteDirectoryItem}: HTMLAnchorElement): void {
	const item = deleteDirectoryItem!.cloneNode(true);
	const link = item.firstElementChild as HTMLAnchorElement;
	const downloadUrl = new URL('https://download-directory.github.io/');
	downloadUrl.searchParams.set('url', location.href);
	link.href = downloadUrl.href;
	link.textContent = 'Download directory';

	deleteDirectoryItem!.before(item);
}

function init(signal: AbortSignal): void {
	observe('a[aria-keyshortcuts="d"]', add, {signal});

	// TODO: Drop in late 2023. Old file view #6154
	observe('[aria-label="Add file"] + details', addLegacy, {signal});
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isRepoTree,
	],
	exclude: [
		pageDetect.isRepoHome, // Already has an native download ZIP button
		pageDetect.isEnterprise,
		pageDetect.isRepoFile404,
	],
	init,
});

/*

Test URLs

- Own repo: https://github.com/refined-github/refined-github/tree/main/.github
- Archived repo: https://github.com/fregante/object-fit-images/tree/master/demo

*/