-
Notifications
You must be signed in to change notification settings - Fork 8
/
script.js
36 lines (32 loc) · 913 Bytes
/
script.js
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
import fetchFirstCommit from 'repo-first-commit';
/**
* Loads and redirects user to first commit
* @param info
* @param tab
*/
const loadFirstCommit = (info, tab) => {
// Remove scheme and host from the URL
const pageUrl = info.pageUrl.replace(/.+:\/\/(www\.)?github.com\/?/, '');
let [owner, repo, pre_sha, sha] = pageUrl.split('/');
// Only these pages have a valid sha
if (!['commits', 'find', 'blob', 'commit', 'tree'].includes(pre_sha)) {
sha = null;
}
// Fetch the first commit and redirect
fetchFirstCommit({ owner, repo, sha })
.then(commit => {
chrome.tabs.update(tab.id, {
url: commit.html_url
});
})
.catch(err => {
alert(err.message);
});
};
// Register the context menu item
chrome.contextMenus.create({
title: 'Initial Commit',
documentUrlPatterns: ['*://github.com/*/*'],
contexts: ['all'],
onclick: loadFirstCommit
});