-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript.js
More file actions
144 lines (140 loc) · 3.48 KB
/
Copy pathuserscript.js
File metadata and controls
144 lines (140 loc) · 3.48 KB
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
import * as Diff3 from "npm:node-diff3";
function getPage(projectName, pageName) {
return fetch(
"https://scrapbox.io/api/pages/" +
projectName +
"/" +
encodeURIComponent(pageName) + "/text",
).then((pageRes) => {
if (pageRes.ok) {
return pageRes.text();
} else {
return pageRes.text().then((text) => {
console.error(pageRes.status, text);
alert(`${pageName}:${pageRes.status}:${text}`);
});
}
}).then((text) => text.split("\n").slice(1).join("\n"));
}
function getAvailability(projectName, pageName) {
return fetch(
"https://scrapbox.io/api/pages/" +
projectName +
"/" +
encodeURIComponent(pageName),
).then((pageRes) => {
if (pageRes.ok) {
return pageRes.json();
} else {
return pageRes.text().then((text) => {
console.error(pageRes.status, text);
alert(`${pageName}:${pageRes.status}:${text}`);
});
}
}).then((json) => {
return json.persistent;
});
}
const userName =
document.querySelector("ul.user-menu li.list-header").innerText;
const projectName = scrapbox.Project.name;
scrapbox.PageMenu.addMenu({
title: "branchTool",
image: "https://i.gyazo.com/5587d1731c863ec1fdb2d46f706d8a08.png",
// <a href="https://www.flaticon.com/free-icons/commit-git" title="commit git icons">Commit git icons created by Ferdinand - Flaticon</a>
});
scrapbox.PageMenu("branchTool").addItem({
title: "branch",
onClick: () => {
const your = `${scrapbox.Page.title} (${userName})`;
const older = `${scrapbox.Page.title} (${userName} backup)`;
getAvailability(projectName, your).then((branchalreadyexits) => {
if (branchalreadyexits) {
window.open(
encodeURIComponent(
your,
),
);
} else {
let b = "";
const lines = scrapbox.Page.lines.slice(1);
lines.unshift({ text: `from ${scrapbox.Page.title}` });
for (const a of lines) {
b += a.text + "\n";
}
console.log(b);
scrapbox.Page.insertLine(`${userName} branch:[${your}]`, 1);
scrapbox.Page.insertLine(` backup:[${older}]`, 2);
window.open(
encodeURIComponent(
older,
) + "?body=" +
encodeURIComponent(
b,
),
);
window.open(
encodeURIComponent(
your,
) + "?body=" +
encodeURIComponent(
b,
),
);
}
});
},
});
scrapbox.PageMenu("branchTool").addItem({
title: "merge",
onClick: () => {
const mine = scrapbox.Page.title;
const your = `${scrapbox.Page.title} (${userName})`;
const older = `${scrapbox.Page.title} (${userName} backup)`;
getPage(projectName, mine).then((
mineData,
) => [mineData]).then(([mineData]) => {
return getPage(projectName, older).then((
olderData,
) => [mineData, olderData]);
})
.then(([mineData, olderData]) => {
return getPage(projectName, your).then((
yourData,
) => [mineData, olderData, yourData]);
}).then(
([mineData, olderData, yourData]) => {
const diff3dat = Diff3.mergeDiff3(
mineData,
olderData,
yourData,
{
excludeFalseConflicts: true,
stringSeparator: /\n/,
label: { a: "main", o: "backup", b: userName },
},
);
console.log(diff3dat);
const joined = `${scrapbox.Page.title}(${userName} merged)`;
window.open(
encodeURIComponent(
older,
),
);
window.open(
encodeURIComponent(
your,
),
);
window.open(
encodeURIComponent(
joined,
) + "?body=" +
encodeURIComponent(
diff3dat.result.join("\n"),
),
);
},
);
},
});