-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontent.ts
More file actions
96 lines (80 loc) · 2.66 KB
/
content.ts
File metadata and controls
96 lines (80 loc) · 2.66 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Function to add Code Wiki button
function addCodeWikiButton(): void {
// If button already exists, do nothing
if (document.querySelector('.codewiki-button')) {
return;
}
// Get repository main navigation element
const navActions = document.querySelector<HTMLUListElement>('ul.pagehead-actions');
if (!navActions) {
return;
}
// Get repository information from current URL
const pathMatch = window.location.pathname.match(/^\/([^/]+)\/([^/]+)/);
if (!pathMatch) {
return;
}
const [, owner, repo] = pathMatch;
// Create Code Wiki button container
const container = document.createElement('li');
container.className = 'codewiki-container';
// Create BtnGroup container
const btnGroup = document.createElement('div');
btnGroup.setAttribute('data-view-component', 'true');
btnGroup.className = 'BtnGroup';
// Create button
const button = document.createElement('a');
button.className = 'btn-sm btn BtnGroup-item codewiki-button';
button.href = `https://codewiki.google/github.com/${owner}/${repo}`;
button.target = '_blank';
button.rel = 'noopener noreferrer';
button.setAttribute('data-view-component', 'true');
// Add icon
const icon = document.createElement('span');
icon.className = 'octicon';
icon.innerHTML = `
<img src="${chrome.runtime.getURL('images/icon-64.png')}" width="16" height="16" alt="Code Wiki">
`;
// Add text
const text = document.createTextNode('Code Wiki');
button.appendChild(icon);
button.appendChild(text);
btnGroup.appendChild(button);
container.appendChild(btnGroup);
// Add to navigation
navActions.insertBefore(container, navActions.firstChild);
}
// Execute immediately and on DOMContentLoaded
addCodeWikiButton();
document.addEventListener('DOMContentLoaded', () => {
addCodeWikiButton();
// Handle GitHub SPA navigation
let lastUrl = location.href;
let isProcessing = false;
const observer = new MutationObserver((mutations: MutationRecord[]) => {
if (isProcessing) return;
isProcessing = true;
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
setTimeout(() => {
addCodeWikiButton();
isProcessing = false;
}, 500);
return;
}
// Monitor navigation element addition (only if button doesn't exist)
const navActions = document.querySelector<HTMLUListElement>('ul.pagehead-actions');
const codeWikiButton = document.querySelector('.codewiki-button');
if (navActions && !codeWikiButton) {
addCodeWikiButton();
}
isProcessing = false;
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});
});