-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notion 2.js
173 lines (151 loc) · 5.71 KB
/
Notion 2.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @name Notion
* @urlAlias notion.so
* @urlRegex *://*.notion.so/*
*/
'use strict';
const PROJECT_NAME_PROPERTY = 'Category'
const TAGS_PROPERTY = 'Tags'
function createWrapper (link) {
const wrapper = document.createElement('div');
wrapper.classList.add('toggl-button-notion-wrapper');
wrapper.appendChild(link);
return wrapper;
}
// Selectors here are madness, it works for as of Dec 4th 2019
// Button renders in popup/dialog view
togglbutton.render(
'.notion-peek-renderer:not(.toggl)',
{ observe: true },
function (elem) {
function getDescription () {
const descriptionElem = elem.querySelector('.notion-scroller .notion-selectable div[contenteditable="true"]');
return descriptionElem ? descriptionElem.textContent.trim() : '';
}
function getProjectName () {
try {
const xpathRes = document.evaluate(`//div[text()="${PROJECT_NAME_PROPERTY}"]`, document, null, XPathResult.ANY_TYPE, null);
const propertyNameElem = xpathRes.iterateNext();
const projectNameElem = propertyNameElem.parentNode.parentNode.parentNode.nextElementSibling;
return projectNameElem && projectNameElem.textContent.trim() != 'Empty' ? projectNameElem.textContent.trim() : '';
} catch (error) {
if (error instanceof TypeError && error.message.includes("propertyNameElem is null")) {
// ignore error
} else {
console.log(error)
}
}
}
function getTags () {
try {
const xpathRes = document.evaluate(`//div[text()="${TAGS_PROPERTY}"]`, document, null, XPathResult.ANY_TYPE, null);
let currElem = xpathRes.iterateNext();
while (currElem) {
if (currElem.offsetParent.classList.contains('notion-scroller')) {
break;
}
currElem = xpathRes.iterateNext();
}
const propertyNameElem = currElem;
const tagsElem = propertyNameElem.parentNode.parentNode.parentNode.nextElementSibling.firstChild.firstChild.firstChild.firstChild;
const tags = Array.from(tagsElem.children)
.map(child => child.textContent.trim())
.filter(textContent => textContent !== 'Empty');
return tags;
} catch (error) {
if (error instanceof TypeError && error.message.includes("propertyNameElem is null")) {
// ignore error
} else {
console.log(error)
}
}
}
const link = togglbutton.createTimerLink({
className: 'notion',
description: getDescription,
projectName: getProjectName,
tags: getTags
});
const wrapper = createWrapper(link);
const root = elem.querySelector('.notion-topbar-share-menu');
if (root) {
root.parentElement.prepend(wrapper);
} else {
elem.querySelector('div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3)').prepend(wrapper)
}
}
);
togglbutton.render(
'.notion-topbar-action-buttons:not(.toggl)',
{ observe: true },
function (elem) {
if (!elem) return;
elem.style.position = 'relative';
function getDescription () {
const controls = document.querySelector('.notion-page-controls');
const topBar = document.querySelector('.notion-topbar');
let title = '';
if (controls) {
if (controls.nextElementSibling) {
title = controls.nextElementSibling;
} else {
const parent = controls.parentElement;
if (!parent) return '';
title = parent ? parent.nextElementSibling : '';
}
} else if (topBar) {
const breadcrumbs = topBar.querySelector('div > .notranslate')
if (breadcrumbs) {
title = breadcrumbs.childNodes[breadcrumbs.childNodes.length - 1].querySelector('.notranslate:last-child')
}
}
return title ? title.textContent.trim() : '';
}
function getProjectName () {
try {
const xpathRes = document.evaluate(`//div[text()="${PROJECT_NAME_PROPERTY}"]`, document, null, XPathResult.ANY_TYPE, null);
const propertyNameElem = xpathRes.iterateNext();
const projectNameElem = propertyNameElem.parentNode.parentNode.parentNode.nextElementSibling;
return projectNameElem && projectNameElem.textContent.trim() != 'Empty' ? projectNameElem.textContent.trim() : '';
} catch (error) {
if (error instanceof TypeError && error.message.includes("propertyNameElem is null")) {
// ignore error
} else {
console.log(error)
}
}
}
function getTags () {
try {
const xpathRes = document.evaluate(`//div[text()="${TAGS_PROPERTY}"]`, document, null, XPathResult.ANY_TYPE, null);
let currElem = xpathRes.iterateNext();
while (currElem) {
if (currElem.offsetParent.classList.contains('notion-scroller')) {
break;
}
currElem = xpathRes.iterateNext();
}
const propertyNameElem = currElem;
const tagsElem = propertyNameElem.parentNode.parentNode.parentNode.nextElementSibling.firstChild.firstChild.firstChild.firstChild;
const tags = Array.from(tagsElem.children)
.map(child => child.textContent.trim())
.filter(textContent => textContent !== 'Empty');
return tags;
} catch (error) {
if (error instanceof TypeError && error.message.includes("propertyNameElem is null")) {
// ignore error
} else {
console.log(error)
}
}
}
const link = togglbutton.createTimerLink({
className: 'notion',
description: getDescription,
projectName: getProjectName,
tags: getTags
});
const wrapper = createWrapper(link);
elem.prepend(wrapper);
}
);