summaryrefslogtreecommitdiff
path: root/source/features/raw-file-link.tsx
blob: 53c029b5528c27e3d587ce557963ef537969668d (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
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
import onPrFileLoad from '../libs/on-pr-file-load';

function createRawUrl(pathname: string): string {
	const url = pathname.split('/');
	url[3] = 'raw'; // Replaces 'blob'
	return url.join('/');
}

function addRawButtons(): void {
	const links = select.all<HTMLAnchorElement>('.js-file-header-dropdown [data-ga-click^="View file"]:not(.rgh-has-raw-file-link)');
	for (const fileLink of links) {
		fileLink.classList.add('rgh-has-raw-file-link');
		fileLink.after(
			<a href={createRawUrl(fileLink.pathname)} className="pl-5 dropdown-item btn-link" role="menuitem">
				View raw
			</a>
		);
	}
}

function init(): void {
	addRawButtons();
	onPrFileLoad(addRawButtons);
}

features.add({
	id: __featureName__,
	description: 'Adds link to view the raw version of files in PRs and commits.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/56484988-b99f2500-6504-11e9-9748-c944e1070cc8.png',
	include: [
		features.isCommit,
		features.isPRFiles
	],
	load: features.onAjaxedPages,
	init
});