-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (43 loc) · 1.33 KB
/
index.js
File metadata and controls
53 lines (43 loc) · 1.33 KB
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
'use strict'
const config = {
title: ':sushi: I want to buy you a sushi',
body:
`I think your software is worth it, thank you! I will buy you a :sushi: if we meet.`,
}
const pageChangeObserver = new window.MutationObserver(() => {
update()
})
const pjaxContainer = document.querySelector('#js-repo-pjax-container, .context-loader-container, [data-pjax-container]')
if (pjaxContainer) {
pageChangeObserver.observe(pjaxContainer, {
childList: true,
})
update()
}
function update() {
const pageheadActions = document.querySelector('ul.pagehead-actions')
if (pageheadActions) {
pageheadActions.insertBefore(
tag('li', {},
tag('a', {
class: 'btn btn-sm',
href: buildIssueLink(config),
}, '🍣')),
pageheadActions.firstChild)
}
}
function tag(tagName, attrs, child) {
const element = document.createElement(tagName)
for (let attrName of Object.keys(attrs)) {
element.setAttribute(attrName, attrs[attrName])
}
if (typeof child === 'string') {
child = document.createTextNode(child)
}
element.appendChild(child)
return element
}
function buildIssueLink(config) {
const repo = location.pathname.split('/').slice(0, 3).join('/')
return `${location.origin}${repo}/issues/new?title=${encodeURIComponent(config.title)}&body=${encodeURIComponent(config.body)}`
}