This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
267 lines (210 loc) · 8.16 KB
/
index.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const os = require('os');
const fs = require('fs');
const path = require('path');
const playwright = require('playwright');
const { chromium } = playwright;
const dotenv = require('dotenv');
dotenv.config();
const isMac = (os.platform() === 'darwin');
(async () => {
// debug mode?
let debug = process.env.DEBUG == "true" ? true : false;
// the md files to process
let dirPath = process.env.FILES_DIR
const files = fs.readdirSync(dirPath).filter(file => {
return file.endsWith('.md') || file.endsWith('.MD');
}).sort((a, b) => {
const aStat = fs.statSync(path.join(dirPath, a));
const bStat = fs.statSync(path.join(dirPath, b));
return aStat.birthtimeMs - bStat.birthtimeMs;
});
// init browser
const browser = await chromium.launch({
headless: process.env.DEBUG !== 'true'
});
const context = await browser.newContext();
const page = await context.newPage();
let pageLoadElement = "[data-testid=current-workspace]"
run()
// functions
async function run(){
console.log('Browser init: heading to ' + process.env.TARGET_URL)
await page.goto('http://' + process.env.TARGET_URL);
console.log('Creating new workspace: ' + process.env.WORKSPACE_NAME)
await newWorkspace(process.env.WORKSPACE_NAME);
// calls newPage for each file in the directory
console.log('Being processing files')
await processFiles()
console.log('Attempting to login to AFFiNE')
await login()
console.log('Attempting to publish workspace')
await publish()
if(!debug){
await browser.close();
}
}
async function newWorkspace(workspaceName){
// wait for page loaded
await page.waitForSelector(pageLoadElement);
// open workspaces list
await selectorFunction('[data-testid=current-workspace]');
// wait for workspace modal
await page.waitForSelector("[role='presentation']");
//const newWorkspaceDiv = await page.$('div.modal-popup > div:scope > div:has-text("New Workspace")');
// activate new workspace prompt
// this div is child child child of the modal popup
await selectorFunction('div:has-text("New Workspace") > *:has-text("New Workspace") > *:has-text("New Workspace") > *:has-text("New Workspace")');
// fill workspace name
await page.fill("[role='presentation'] input", workspaceName);
// create workspace
await selectorFunction('button:has-text("Create")');
}
async function newPage(pageTitle, pageContent){
// create new page
await selectorFunction('[data-testid=sliderBar] > div:has-text("New Page")');
// copy contents to clipboard
await page.evaluate(content => {
// create a temporary textarea element to hold the content
const textarea = document.createElement('textarea');
textarea.value = content;
// add the textarea to the DOM and select its contents
document.body.appendChild(textarea);
textarea.select();
// copy the contents to the clipboard and remove the textarea
document.execCommand('copy');
document.body.removeChild(textarea);
}, pageContent);
// give time for the clipboard to be updated
await new Promise(resolve => setTimeout(resolve, 500));
// fill the title
await page.getByPlaceholder('Title').fill(pageTitle);
//await page.getByRole('paragraph').click();
await selectorFunction('.affine-block-children-container');
// paste the contents
const modifier = isMac ? 'Meta' : 'Control';
await page.keyboard.press(`${modifier}+KeyV`);
// give time for the content to be pasted
await new Promise(resolve => setTimeout(resolve, 1500));
/* type unreliable, so use clipboard
//await selectorFunction('.affine-block-children-container');
//await page.type(".affine-block-children-container", pageContent);
*/
if(debug){
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
async function login(){
// login
let refreshToken = process.env.LOGIN_TOKEN
await page.evaluate((refreshToken) => {
window.localStorage.setItem('affine:login', JSON.stringify({
refresh: refreshToken
}));
}, refreshToken);
// reload page and wait loaded
await page.reload();
await page.waitForSelector(pageLoadElement);
}
async function publish(){
// define workspace settings url (replace last part of url)
const urlParts = page.url().replace(/\/$/, "").split('/');
urlParts.pop();
settingsPage= urlParts.join('/') + '/setting';
// load settings page
await page.goto(settingsPage);
await page.waitForSelector(pageLoadElement);
await selectorFunction('[data-setting-tab-button="Publish"]');
// enable affine cloud, if necessary
if(await page.$('span:has-text("Enable AFFiNE Cloud")')) {
console.log("Enabling AFFiNE Cloud on workspace")
await selectorFunction('button:has-text("Enable")');
await page.waitForSelector('[role="presentation"]');
await selectorFunction('div[tabindex="-1"] button:has-text("Enable")');
// reload page and wait loaded
await waitForElementToDisappear('div[tabindex="-1"]');
await page.reload();
await selectorFunction('[data-setting-tab-button="Publish"]');
}
// publish to web, if necessary
if(await page.$("span:has-text('Publish to web')")) {
console.log("Publishing workspace")
await selectorFunction('button:has(span:has-text("Publish to web"))');
await selectorFunction('[data-setting-tab-button="Publish"]');
// reload page and wait loaded
await page.reload();
await selectorFunction('[data-setting-tab-button="Publish"]');
}
// get workspace public url
console.log("Getting public URL")
const input = await page.$('input[value^="app.affine.pro"]');
const value = await input.evaluate((el) => el.value);
const PUBLIC_WORKSPACE_URL = value;
console.log('Finished:')
console.log(PUBLIC_WORKSPACE_URL);
}
async function highlight(selector){
const element = await page.$(selector);
await page.evaluate((el) => {
const div = document.createElement('div');
div.id = 'highlight';
div.style.position = 'absolute';
div.style.border = '2px solid red';
div.style.zIndex = '9999';
const rect = el.getBoundingClientRect();
div.style.left = `${rect.left}px`;
div.style.top = `${rect.top}px`;
div.style.width = `${rect.width}px`;
div.style.height = `${rect.height}px`;
document.body.appendChild(div);
}, element);
}
async function unhighlight(){
await page.evaluate(() => {
const highlightDiv = document.querySelector('div[id="highlight"]');
if (highlightDiv) {
highlightDiv.remove();
}
});
}
async function selectorFunction(selector, click = true) {
debug ? console.log(`Waiting for selector ${selector}`) : null
await page.waitForSelector(selector);
if(debug){
highlight(selector)
console.log(`Highlighting selector ${selector}`)
await new Promise(resolve => setTimeout(resolve, 1500));
console.log(`Unhighlight selector ${selector}`)
await unhighlight()
}
if(click) {
debug ? console.log(`Clicking selector ${selector}`) : null
await page.click(selector);
}
debug ? console.log(`\r\n`) : null
}
async function waitForElementToDisappear(selector, timeout = 10000) {
const endTime = Date.now() + timeout;
while (Date.now() < endTime) {
const element = await page.$(selector);
if (!element) {
return;
}
await new Promise(resolve => setTimeout(resolve, 100));
}
//throw new Error(`Timed out waiting for element with selector ${selector}`);
console.error('AFFiNE Cloud may not have activated, forcing a refresh to try to continue')
await page.reload();
}
const processFile = async (fileName) => {
const filePath = path.join(dirPath, fileName);
const title = path.parse(fileName).name;
const content = fs.readFileSync(filePath, 'utf8');
console.log(`Processing file: ${fileName}`);
await newPage(title, content);
};
const processFiles = async () => {
for (const fileName of files) {
await processFile(fileName);
}
};
})();