-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
64 lines (53 loc) · 1.75 KB
/
content_script.js
File metadata and controls
64 lines (53 loc) · 1.75 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
54
55
56
57
58
59
60
61
62
63
64
function insertGenerateButton() {
const titleInput = document.querySelector('#pull_request_title')
const titleHeader = document.querySelector('#pull_request_title_header')
if (
!titleInput ||
!titleHeader ||
document.querySelector('#ai-generate-title-btn')
)
return
const button = document.createElement('button')
button.id = 'ai-generate-title-btn'
button.type = 'button'
button.textContent = '✨ Generate Title'
button.style.marginLeft = '10px'
button.style.padding = '4px 8px'
button.style.border = '1px solid #d0d7de'
button.style.borderRadius = '6px'
button.style.cursor = 'pointer'
button.style.fontSize = '12px'
const loadingText = document.createElement('span')
loadingText.id = 'ai-loading-text'
loadingText.textContent = ' ⏳'
loadingText.style.display = 'none'
button.appendChild(loadingText)
button.onclick = async (event) => {
event.preventDefault()
event.stopPropagation()
loadingText.style.display = 'inline'
const commitMessages = Array.from(
document.querySelectorAll('.js-navigation-open'),
).map((el) => el.textContent.trim())
chrome.runtime.sendMessage(
{ type: 'generateTitle', data: commitMessages },
(response) => {
loadingText.style.display = 'none'
console.log(response)
if (response && response.title) {
titleInput.value = response.title
} else {
alert('AI failed to generate title. Please try again.')
}
},
)
}
titleHeader.appendChild(button)
}
// Observe DOM changes to handle SPA navigation on GitHub
const observer = new MutationObserver(() => {
insertGenerateButton()
})
observer.observe(document.body, { childList: true, subtree: true })
// Initial try
insertGenerateButton()