Skip to content

Commit a700bbd

Browse files
authored
Allow for the buttons to be displayed even without a refresh (#39)
* allow for the buttons to be displayed event without a refresh * fix snapshots
1 parent ba6c00b commit a700bbd

File tree

7 files changed

+45
-4
lines changed

7 files changed

+45
-4
lines changed

extension/background/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Request } from '../types/request'
22
import { Response } from '../types/response'
33
import { CodeTour, EnhancedCodeTourStep } from '../types/code-tour'
4+
import { Actions, BackgroundMessage } from '../types/background-messages'
45
import MessageSender = chrome.runtime.MessageSender
56

67
const codeTourMap: Record<string, CodeTourGenerator> = {}
@@ -92,3 +93,12 @@ chrome.runtime.onMessage.addListener(function (
9293
}
9394
return false
9495
})
96+
97+
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
98+
if (changeInfo.url) {
99+
const message: BackgroundMessage = {
100+
action: Actions.UrlChanged,
101+
}
102+
chrome.tabs.sendMessage(tabId, message)
103+
}
104+
})

extension/github/__snapshots__/add-code-tour.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`buildSection renders properly the section when the next and previous URLs are not defined 1`] = `
44
<div
5-
class="dl-doctolib-code-tour-comment"
5+
class="code-tour-comment"
66
style="
77
padding: 14px;
88
margin: 14px;
@@ -52,7 +52,7 @@ exports[`buildSection renders properly the section when the next and previous UR
5252

5353
exports[`buildSection renders properly the whole section 1`] = `
5454
<div
55-
class="dl-doctolib-code-tour-comment"
55+
class="code-tour-comment"
5656
style="
5757
padding: 14px;
5858
margin: 14px;

extension/github/add-code-tour.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function buildSection(currentStep: EnhancedCodeTourStep, step: number): H
8686
const nextButton = buttonTo('Next', currentStep.nextUrl)
8787

8888
const section = document.createElement('div')
89-
section.setAttribute('class', 'dl-doctolib-code-tour-comment')
89+
section.setAttribute('class', 'code-tour-comment')
9090

9191
const tourInfo = buildTitleRow(currentStep, step)
9292

@@ -112,6 +112,8 @@ export function buildSection(currentStep: EnhancedCodeTourStep, step: number): H
112112
}
113113

114114
export async function addCodeTour(): Promise<void> {
115+
// Prevent displaying the code tour twice
116+
if (document.querySelector('.code-tour-comment')) return
115117
const sheet = document.createElement('style')
116118
sheet.innerHTML = `
117119
pre {

extension/github/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { onMainPage } from './on-main-page'
22
import { onCodeTourList } from './on-code-tour-list'
33
import { addCodeTour } from './add-code-tour'
4+
import { Actions, BackgroundMessage } from '../types/background-messages'
45

56
enum PageType {
67
NoWhere,
@@ -38,4 +39,12 @@ function main(): void {
3839
return undefined
3940
}
4041

41-
setTimeout(main, 1000)
42+
document.addEventListener('DOMContentLoaded', () => {
43+
main()
44+
})
45+
46+
chrome.runtime.onMessage.addListener(function (request: BackgroundMessage) {
47+
if (request.action === Actions.UrlChanged) {
48+
setTimeout(main, 1000)
49+
}
50+
})

extension/github/on-code-tour-list.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export function onCodeTourList(): void {
2424
const currentRepository = currentRepositoryMatches[1]
2525
const currentBranch = currentBranchMatches[1]
2626

27+
// Prevent displaying the buttons twice
28+
if (document.querySelector('.code-tour-start')) return
29+
2730
const insertPreparation = Array.from(
2831
document.querySelectorAll('div[role=row] > div[role="rowheader"] > span > a').values(),
2932
).map(
@@ -52,6 +55,7 @@ export function onCodeTourList(): void {
5255

5356
const newChild = document.createElement('a')
5457
newChild.classList.add('btn')
58+
newChild.classList.add('code-tour-start')
5559
newChild.innerHTML = `Learn ${prettyName}`
5660
newChild.onclick = async () => {
5761
const result = await forwardRequest({ action: 'START', codeTour: tourContent })
@@ -71,6 +75,8 @@ export function onCodeTourList(): void {
7175
)
7276

7377
void Promise.all(insertPreparation).then((operations) => {
78+
// Prevent displaying the buttons twice
79+
if (document.querySelector('.code-tour-start')) return
7480
operations.forEach((operation) => {
7581
if (!operation) return
7682
const { newChild, row } = operation

extension/github/on-main-page.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ export function onMainPage(): void {
22
const node = document.querySelector('a[href$="/.tours"]')
33
if (!node) return
44

5+
if (document.querySelector('.code-tour-access')) return
6+
57
const codeTourFolderUrl = node.getAttribute('href')
68
if (!codeTourFolderUrl) return
79

810
const codeTourButton = document.createElement('a')
911
codeTourButton.classList.add('btn')
12+
codeTourButton.classList.add('code-tour-access')
1013
codeTourButton.classList.add('ml-2')
1114
codeTourButton.classList.add('d-none')
1215
codeTourButton.classList.add('d-md-block')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export enum Actions {
2+
UrlChanged = 'URL_CHANGED',
3+
}
4+
5+
interface Message<T extends Actions> {
6+
action: T
7+
}
8+
9+
type UrlChangedMessage = Message<Actions.UrlChanged>
10+
11+
export type BackgroundMessage = UrlChangedMessage

0 commit comments

Comments
 (0)