-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
179 lines (167 loc) · 4.28 KB
/
script.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
174
175
176
177
178
179
function emptyTrash() {
function standardizeUUID(uuid) {
if (uuid?.length === 32 && !uuid.includes("-")) {
uuid = uuid.replace(
/([\d\w]{8})([\d\w]{4})([\d\w]{4})([\d\w]{4})([\d\w]{12})/,
"$1-$2-$3-$4-$5"
);
}
return uuid;
}
function getCurrentPageId() {
const regex = /(?:\/|-)([a-f0-9]{32})/i;
const match = window.location.href.match(regex);
if (match && match[1]) {
return standardizeUUID(match[1]);
} else {
return null;
}
}
async function getCurrentSpaceInfo() {
id = getCurrentPageId();
if (!id) {
alert("Error: Not on a Notion page?");
return;
}
// Get the workspace this page belongs to
resp = await fetchNotion(
"https://www.notion.so/api/v3/getRecordValues",
null,
{
requests: [
{
id: id,
table: "block",
},
],
}
);
json = await resp.json();
return {
space_id: json?.results?.[0]?.value?.space_id,
user_id: json?.results?.[0]?.value?.permissions?.[0]?.user_id,
};
}
async function getBlockIds(spaceId) {
body = {
type: "BlocksInSpace",
query: "",
filters: {
isDeletedOnly: true,
excludeTemplates: false,
navigableBlockContentOnly: true,
requireEditPermissions: false,
includePublicPagesWithoutExplicitAccess: false,
ancestors: [],
createdBy: [],
editedBy: [],
lastEditedTime: {},
createdTime: {},
inTeams: [],
},
sort: {
field: "lastEdited",
direction: "desc",
},
limit: 1000,
spaceId: spaceId,
source: "trash",
};
resp = await fetchNotion("https://www.notion.so/api/v3/search", null, body);
json = await resp.json();
blockIds = json.results.map((el) => {
return el.id;
});
return blockIds;
}
(async () => {
const spaceInfo = await getCurrentSpaceInfo();
blockIds = await getBlockIds(spaceInfo.space_id);
if (blockIds.length == 0) {
alert("Trash is empty!");
return;
}
const dg = confirm(
"Are you sure you want to delete " +
blockIds.length +
" blocks from workspace " +
spaceInfo.space_id +
"?"
);
if (!dg) {
return;
}
resp = await fetchNotion(
"https://www.notion.so/api/v3/deleteBlocks",
spaceInfo,
{
blocks: blockIds.map((id) => {
return {
id: id,
spaceId: spaceInfo.space_id,
};
}),
permanentlyDelete: true,
}
);
if (resp.status == 200) {
alert("Trash emptied!");
} else {
alert("Error: " + resp.status + ", see console for details");
console.log(resp);
}
})();
}
async function fetchNotion(url, spaceInfo, body) {
req = {
credentials: "include",
headers: {
accept: "*/*",
"cache-control": "no-cache",
"content-type": "application/json",
pragma: "no-cache",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
},
body: JSON.stringify(body),
method: "POST",
mode: "cors",
};
if (spaceInfo) {
req.headers["x-notion-active-user-header"] = spaceInfo.user_id;
req.headers["x-notion-space-id"] = spaceInfo.space_id;
}
const response = await fetch(url, req);
return response;
}
function injectEmptyTrashButton(trashMenu) {
const button = document.createElement("button");
button.innerText = "Empty Trash";
button.onclick = emptyTrash;
if (trashMenu.children.length > 0) {
const secondChild = trashMenu.children[1] || null;
trashMenu.insertBefore(button, secondChild);
} else {
trashMenu.appendChild(button);
}
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === "childList") {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const trashMenu = node.classList.contains("notion-sidebar-trash-menu")
? node
: node.querySelector(".notion-sidebar-trash-menu");
if (trashMenu) {
injectEmptyTrashButton(trashMenu);
}
}
});
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true,
});