-
Notifications
You must be signed in to change notification settings - Fork 25
/
background.js
83 lines (76 loc) · 2.55 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
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true }
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions)
return tab
}
// used when Github uses push state.
chrome.webNavigation.onHistoryStateUpdated.addListener(async () => {
const tab = await getCurrentTab()
try {
chrome.scripting.executeScript({
files: ['jquery/jquery-3.6.0.min.js', 'handlebars.runtime.min-v4.7.7.js', 'template.js', 'app.js'],
target: { tabId: tab.id },
})
chrome.scripting.insertCSS({ files: ['./css/style.css'], target: { tabId: tab.id } })
} catch (ex) {
console.error(ex)
}
})
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'installed') {
chrome.tabs.create({ url: 'https://github.com/gatewayapps/kamino' }, () => {
console.log('Kamino Github page launched')
})
}
})
chrome.runtime.onMessage.addListener((request) => {
if (request.action && request.action === 'goToOptions') {
chrome.tabs.create({ url: `chrome-extension://${chrome.runtime.id}/options.html`, selected: true })
} else {
chrome.storage.sync.get(
{
goToList: false,
createTab: true,
filters: '',
},
(item) => {
if (item.goToList) {
chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => {
const filterList = typeof item.filters === 'string' ? [] : item.filters
var f = filterList.filter((i) => {
return i.organization === request.organization && i.currentRepo === request.oldRepo
})
var filter = {
filter: '',
}
if (f && f.length > 0) {
filter = f[0]
}
setTimeout(() => {
if (item.createTab) {
chrome.tabs.create({
url: `https://github.com/${request.repo}/issues/${request.issueNumber}`,
selected: false,
})
}
chrome.tabs.update(tabs[0].id, {
url: `https://github.com/${request.organization}/${request.oldRepo}${filter.filter}`,
selected: true,
})
}, 1000)
})
} else {
if (item.createTab) {
setTimeout(() => {
chrome.tabs.create({
url: `https://github.com/${request.repo}/issues/${request.issueNumber}`,
selected: true,
})
}, 1000)
}
}
}
)
}
})