-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
183 lines (172 loc) · 5.18 KB
/
content-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
180
181
182
183
const KEYWORD = "DocBaseTreeViewParentMarker:";
const TREE_CACHE_KEY = "docbase-treeview-tree-cache";
function strip(html) {
const doc = new DOMParser().parseFromString(html, "text/html");
return doc.body.textContent || "";
}
async function getAllMarkedPages(page = 1) {
const perPage = 1000;
const encoded = encodeURIComponent(`"${KEYWORD}"`);
const res = await (
await fetch(
`/search?page=${page}&per_page=${perPage}&q=desc%3Ascore%20${encoded}`,
{
headers: {
Accept: "application/json",
"X-Requested-With": "XMLHttpRequest",
},
}
)
).json();
const entries = [];
for (const page of res[1]) {
const match = strip(page.highlights.other).match(
new RegExp(`${KEYWORD}\\s+(Root|#\{(\\d+)\})`)
);
if (!match) continue;
entries.push({
id: page.id,
title: page.title,
parentId: match[2] ? parseInt(match[2]) : 0,
children: [],
});
}
if (res[0].total_entries > page * perPage) {
entries.push(...(await getAllMarkedPages(page + 1)));
}
return entries;
}
function convertToTree(entries, parentId = 0) {
return entries
.filter((entry) => entry.parentId === parentId)
.map((entry) => ({
...entry,
children: convertToTree(entries, entry.id).sort((a, b)=>a.title.localeCompare(b.title)),
}));
}
async function createChild(parentPageId) {
const token = document.cookie.match(/CSRF-TOKEN=(.+)(?:;|$)/)[1];
const res = await fetch("/posts", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
"X-Csrf-Token": token,
},
body: JSON.stringify({
post: {
title: `${parentPageId}の子ページ`,
body: `${KEYWORD}\n#{${parentPageId}}`,
tag_list: "",
limited_access: null,
groups: [],
draft: true,
disable_notification: false,
},
}),
redirect: "manual",
});
return (await res.headers.get("Location")) + "/edit";
}
async function renderChildren(parentNode, children) {
const ul = document.createElement("ul");
parentNode.appendChild(ul);
for (const child of children) {
const li = document.createElement("li");
const span = document.createElement("span");
const a = document.createElement("a");
a.textContent = child.title;
a.href = `/posts/${child.id}`;
a.setAttribute("data-page-id", child.id);
span.appendChild(a);
li.appendChild(span);
ul.appendChild(li);
if (child.children.length > 0) {
span.classList.add("docbase-treeview-folder");
renderChildren(li, child.children);
} else {
span.classList.add("docbase-treeview-file");
}
}
}
async function openParent(elm) {
const ul = elm.closest("ul");
if (ul && ul.previousSibling) {
ul.previousSibling.classList.add("open");
openParent(ul.previousSibling);
}
}
async function openChild(elm) {
const folder = elm.parentNode;
if (folder.classList.contains("docbase-treeview-folder")) {
folder.classList.add("open");
}
}
function getPageId() {
const match = location.href.match(/posts\/(?:(\d+)|new\?origin=(\d+))/);
console.log(match)
if (!match) return;
return match[1] ?? match[2];
}
async function renderTreeView() {
const id = "docbase-treeview-tree";
document.getElementById(id)?.remove();
const wrapDiv = document.createElement("div");
wrapDiv.id = id;
const nav = document.querySelector(".emUosV").parentNode;
nav.appendChild(wrapDiv);
const pageId = getPageId();
if (pageId) {
const newPageButton = document.createElement("button");
newPageButton.textContent = "子ページを作成";
newPageButton.id = "docbase-treeview-create-child"
wrapDiv.appendChild(newPageButton);
newPageButton.addEventListener("click", async () => {
newPageButton.disabled = true;
const url = await createChild(pageId);
setTimeout(()=>{
location.href = url;
}, 500);
});
}
const tree = JSON.parse(localStorage.getItem(TREE_CACHE_KEY) ?? []);
renderChildren(wrapDiv, tree);
const folders = document.querySelectorAll(".docbase-treeview-folder");
for (const folder of folders) {
folder.addEventListener("click", function () {
this.classList.toggle("open");
});
}
const links = document.querySelectorAll(".docbase-treeview-folder > a");
for (const link of links) {
link.addEventListener("click", function (e) {
e.stopPropagation();
});
}
if (!pageId) return;
const current = document.querySelector(`[data-page-id="${pageId}"]`);
if (!current) return;
current.classList.add("active");
openParent(current);
openChild(current);
}
async function buildCache() {
const pages = await getAllMarkedPages();
localStorage.setItem(TREE_CACHE_KEY, JSON.stringify(convertToTree(pages)));
}
async function main() {
await buildCache();
renderTreeView();
}
main();
window.addEventListener("popstate", (event) => {
main();
});
const newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = chrome.runtime.getURL("inline.js");
document.querySelector("head").appendChild(newScript);
window.addEventListener("state-changed", function (e) {
main();
});