Skip to content

Commit 834ad51

Browse files
committed
fix: handle document.head availability in CSS injection
Added check for document.head existence before injecting CSS. Since runAt is 'document_start', document.head may not be available yet. Uses MutationObserver to wait for document.head if needed. Fixes: Uncaught TypeError: Cannot read properties of null (reading 'appendChild')
1 parent 2f6661e commit 834ad51

1 file changed

Lines changed: 35 additions & 19 deletions

File tree

entrypoints/content.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,43 @@ export default defineContentScript({
55

66
main() {
77
// Inject CSS
8-
const style = document.createElement('style');
9-
style.textContent = `
10-
.codewiki-button.btn-sm.btn.BtnGroup-item {
11-
text-decoration: none;
12-
display: inline-flex;
13-
align-items: center;
14-
padding: 3px 12px;
15-
gap: 8px;
16-
}
8+
const injectCSS = () => {
9+
const style = document.createElement('style');
10+
style.textContent = `
11+
.codewiki-button.btn-sm.btn.BtnGroup-item {
12+
text-decoration: none;
13+
display: inline-flex;
14+
align-items: center;
15+
padding: 3px 12px;
16+
gap: 8px;
17+
}
1718
18-
.codewiki-button .octicon {
19-
display: inline-flex;
20-
align-items: center;
21-
}
19+
.codewiki-button .octicon {
20+
display: inline-flex;
21+
align-items: center;
22+
}
2223
23-
.codewiki-button .octicon img {
24-
display: block;
25-
vertical-align: text-bottom;
26-
}
27-
`;
28-
document.head.appendChild(style);
24+
.codewiki-button .octicon img {
25+
display: block;
26+
vertical-align: text-bottom;
27+
}
28+
`;
29+
document.head.appendChild(style);
30+
};
31+
32+
// Inject CSS when document.head is available
33+
if (document.head) {
34+
injectCSS();
35+
} else {
36+
// Wait for document.head to be available
37+
const observer = new MutationObserver(() => {
38+
if (document.head) {
39+
observer.disconnect();
40+
injectCSS();
41+
}
42+
});
43+
observer.observe(document.documentElement, { childList: true });
44+
}
2945

3046
// Function to add Code Wiki button
3147
function addCodeWikiButton(): void {

0 commit comments

Comments
 (0)