-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
43 lines (38 loc) · 1.44 KB
/
content.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
let currentURL
function waitTargetElement(){
return new Promise(resolve => {
let interval = setInterval(() => {
if(document.querySelector(".AppHeader-actions")){
resolve()
clearInterval(interval)
}
}, 100)
})
}
function getUserName(){
return document.querySelector("meta[name='user-login']").content
}
function writeQuickActionElement(name, href, icon, userName){
let hrefReplaced = href.replace("$NAME", userName)
document.querySelector(".AppHeader-actions").insertAdjacentHTML("beforeend",`
<a title="${name}" href="${hrefReplaced}" data-view-component="true" class="Button Button--iconOnly Button--secondary Button--medium AppHeader-button color-fg-muted Button--custom">
${icon}
</a>
`)
}
setInterval(() => {
if(currentURL != location.href && !document.querySelector(".Button--custom")){
currentURL = location.href
main()
}
}, 100)
async function main(){
await waitTargetElement()
let actions = await fetch(chrome.runtime.getURL("./actions.json")).then(e => e.json())
let currentItems = await chrome.storage.local.get("currentItems").then(e => e?.currentItems) || ["issues", "pulls"]
let userName = getUserName()
currentItems.forEach(item => {
let itemDict = actions.find(e => item == e.id)
writeQuickActionElement(itemDict.name, itemDict.href, itemDict.icon, userName)
})
}