Skip to content

Commit 4f45508

Browse files
committed
fix(notion): Fix button not re-rendering on page change
1 parent 3d4e798 commit 4f45508

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

src/content/notion.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ togglbutton.render(
1919
'.notion-peek-renderer:not(.toggl)',
2020
{ observe: true },
2121
function (elem) {
22+
if (!elem) return;
2223
function getDescription () {
2324
const descriptionElem = elem.querySelector('.notion-peek-renderer .notion-scroller h1[contenteditable]');
2425
return descriptionElem ? descriptionElem.textContent.trim() : '';
@@ -36,54 +37,63 @@ togglbutton.render(
3637
if (root) {
3738
root.parentElement.prepend(wrapper);
3839
} else {
39-
elem.querySelector('div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3)').prepend(wrapper)
40+
const selector = elem.querySelector('div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3)')
41+
if (!selector) return;
42+
selector.prepend(wrapper)
4043
}
4144
}
4245
);
4346

44-
togglbutton.render(
45-
'.notion-topbar-action-buttons:not(.toggl)',
46-
{ observe: true },
47-
function (elem) {
48-
if (!elem) return;
47+
setTimeout(() => {
48+
togglbutton.render(
49+
'.notion-topbar-action-buttons',
50+
{ observe: true, debounceInterval: 1000 },
51+
function (elem) {
52+
if (!elem) return;
53+
const elements = document.querySelectorAll('.notion-topbar-action-buttons .toggl-button-notion-wrapper')
54+
if(elements.length > 0) {
55+
elements.forEach(element => element.remove())
56+
}
4957

50-
elem.style.position = 'relative';
58+
elem.style.position = 'relative';
5159

52-
function getDescription () {
53-
const controls = document.querySelector('.notion-page-controls');
54-
const topBar = document.querySelector('.notion-topbar');
55-
let title = '';
60+
function getDescription () {
61+
const controls = document.querySelector('.notion-page-controls');
62+
const topBar = document.querySelector('.notion-topbar');
63+
let title = '';
5664

57-
if (controls) {
58-
if (controls.nextElementSibling) {
59-
title = controls.nextElementSibling;
60-
} else {
61-
const parent = controls.parentElement;
65+
if (controls) {
66+
if (controls.nextElementSibling) {
67+
title = controls.nextElementSibling;
68+
} else {
69+
const parent = controls.parentElement;
6270

63-
if (!parent) return '';
71+
if (!parent) return '';
6472

65-
title = parent ? parent.nextElementSibling : '';
66-
}
67-
} else if (topBar) {
68-
const breadcrumbs = topBar.querySelector('div > .notranslate')
69-
if (breadcrumbs) {
70-
title = breadcrumbs.childNodes[breadcrumbs.childNodes.length - 1].querySelector('.notranslate:last-child')
73+
title = parent ? parent.nextElementSibling : '';
74+
}
75+
} else if (topBar) {
76+
const breadcrumbs = topBar.querySelector('div > .notranslate')
77+
if (breadcrumbs) {
78+
title = breadcrumbs.childNodes[breadcrumbs.childNodes.length - 1].querySelector('.notranslate:last-child')
79+
}
7180
}
72-
}
7381

74-
return title ? title.textContent.trim() : '';
75-
}
82+
return title ? title.textContent.trim() : '';
83+
}
7684

77-
const link = togglbutton.createTimerLink({
78-
className: 'notion',
79-
description: getDescription,
80-
});
85+
const link = togglbutton.createTimerLink({
86+
className: 'notion',
87+
description: getDescription,
88+
});
8189

82-
const wrapper = createWrapper(link);
90+
const wrapper = createWrapper(link);
8391

84-
elem.prepend(wrapper);
85-
}
86-
);
92+
elem.prepend(wrapper);
93+
},
94+
'.notion-topbar .shadow-cursor-breadcrumb *,title'
95+
);
96+
}, 2000)
8797

8898
/**
8999
* @name Notion Calendar
@@ -94,6 +104,7 @@ togglbutton.render(
94104
'div[data-context-panel-root]:not(.toggl)',
95105
{ observe: true },
96106
function (elem) {
107+
if (!elem) return;
97108
function getDescription () {
98109
const descriptionElem = elem.querySelector('div[contenteditable="true"]');
99110
return descriptionElem ? descriptionElem.textContent.trim() : '';

0 commit comments

Comments
 (0)