/
background.js
87 lines (79 loc) · 2.57 KB
/
background.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: 'atlassian.net' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'bitbucket.org' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'github.com' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'gitlab.com' },
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
])
})
})
chrome.pageAction.onClicked.addListener(tab => {
const url = new URL(tab.url)
const domain = url.hostname.split('.').slice(-2).join('.')
let uriHandle = false
switch (domain) {
case 'bitbucket.org':
uriHandle = handleBitbucket(url)
break
case 'atlassian.net':
uriHandle = handleJira(url)
break
case 'github.com':
// consider using https://github.com/Mottie/github-reserved-names
uriHandle = handleOpenOrClone(url)
break
case 'gitlab.com':
uriHandle = handleOpenOrClone(url)
break
default:
}
if (uriHandle !== false) {
window.open(uriHandle)
// only enable with config
// chrome.tabs.remove(tab.id)
}
})
function handleBitbucket(url) {
const path = url.pathname.split('/').slice(1)
if (path[0] === 'account' || path[0] === 'dashboard' || path.length < 2) return;
if (path.length >= 4 && path[2] === 'pull-requests' && path[3].match(/[0-9]+/) !== null) {
return `${ide()}://atlassian.atlascode/openPullRequest?q=${encodeURIComponent(url.href)}`
} else {
return handleOpenOrClone(url)
}
}
function handleJira(url) {
const issueMatch = url.pathname.match(/[A-Z]{2,}-[0-9]+/)
if (issueMatch) {
return `${ide()}://atlassian.atlascode/showJiraIssue?issueKey=${issueMatch[0]}&site=${url.hostname}`
}
return false
}
function handleOpenOrClone(url) {
const path = url.pathname.split('/').slice(1)
if (path[0] !== undefined && path[1] !== undefined) {
const org = path[0]
const project = path[1]
const cloneUrl = encodeURIComponent(`git@${url.hostname}:${org}/${project}`)
return `${ide()}://timmoverlaan.uri-open-recent/open-or-clone?project=${project}&url=${cloneUrl}`
}
return false
}
function ide() {
// add support for insiders via config
return 'vscode'
}