@@ -132,28 +132,28 @@ async function fillXiaohongshuContent(page, title, content) {
132132 title || ""
133133 ) ;
134134
135- // Fill in the content by clipboard copying pasting
136- await page . evaluate ( async ( text ) => {
137- await navigator . clipboard . writeText ( text ) ;
138- } , content || "" ) ;
139135 await page . waitForTimeout ( 1000 ) ;
140136 await page . waitForSelector ( ".ql-editor" ) ;
141137 await page . focus ( ".ql-editor" ) ;
142- // await page.keyboard.type(content || "", { delay: 100 });
143- console . log ( "platform:" , process . platform ) ;
144- await page . keyboard . press (
145- process . platform === "darwin" ? "Meta+V" : "Control+V"
138+ const { tags, content : contentWithoutTags } = getTagsFromContent (
139+ content || ""
146140 ) ;
141+
142+ // Fill in the content by clipboard copying pasting
143+ await copyPasteContent ( page , contentWithoutTags ) ;
147144 await page . waitForTimeout ( 2000 ) ;
145+
146+ await page . keyboard . press ( "Enter" ) ;
147+
148+ await page . waitForTimeout ( 1000 ) ;
149+
148150 // add hashtags
149- const tags = getTagsFromContent ( content || "" ) ;
150151 console . log ( "🦄🦄tags:" , tags ) ;
151152 for ( const tag of tags ) {
152- await page . keyboard . type ( "#" ) ;
153- await page . waitForTimeout ( 100 ) ;
154- await page . keyboard . type ( tag , { delay : 300 } ) ;
155- await page . waitForTimeout ( 1000 ) ;
153+ await copyPasteContent ( page , `#${ tag } ` ) ;
154+ await page . waitForTimeout ( 2000 ) ;
156155 await page . keyboard . press ( "Enter" ) ;
156+ await page . waitForTimeout ( 1000 ) ;
157157 }
158158
159159 await page . waitForTimeout ( 1000 ) ;
@@ -222,8 +222,35 @@ async function publishBilibili(data) {
222222
223223 // Use the filechooser to set your file
224224 await fileChooser . setFiles ( data . video ) ;
225+ // fill in title
226+ await page . locator ( 'input[placeholder="请输入稿件标题"]' ) . click ( ) ;
227+ await page . keyboard . press (
228+ process . platform === "darwin" ? "Meta+A" : "Control+A"
229+ ) ;
230+ await page . keyboard . press ( "Delete" ) ;
231+ await copyPasteContent ( page , data . title ) ;
232+
233+ const { tags, content : contentWithoutTags } = getTagsFromContent (
234+ data . content || ""
235+ ) ;
236+ // fill in content
237+ await page . focus ( ".ql-editor" ) ;
238+ await copyPasteContent ( page , contentWithoutTags ) ;
239+ await page . waitForTimeout ( 1000 ) ;
240+ // fill in tags
241+ const tagInput = await page
242+ . locator ( 'input[placeholder="按回车键Enter创建标签"]' )
243+ . nth ( 0 ) ;
244+ await tagInput . click ( ) ;
245+ await page . waitForTimeout ( 1000 ) ;
246+ for ( const tag of tags ) {
247+ await copyPasteContent ( page , `${ tag } ` ) ;
248+ await page . waitForTimeout ( 1000 ) ;
249+ await page . keyboard . press ( "Enter" ) ;
250+ await page . waitForTimeout ( 1000 ) ;
251+ }
225252
226- console . log ( "File selected via file chooser" ) ;
253+ await page . waitForTimeout ( 2000 ) ;
227254 } catch ( err ) {
228255 console . error ( "Upload error:" , err ) ;
229256 throw err ;
@@ -232,11 +259,26 @@ async function publishBilibili(data) {
232259
233260/**
234261 * @param {string } content - The content of the post
235- * @returns {string[] } - The tags of the post
262+ * @returns {{tags: string[], content: string} } - The tags of the post and the content without tags
236263 */
237264function getTagsFromContent ( content ) {
238265 const tags = content . match ( / # ( \w + ) / g) ;
239266 const ret = tags ? tags . map ( ( tag ) => tag . slice ( 1 ) ) : [ ] ;
240267 console . log ( "🦄🦄ret:" , ret ) ;
241- return ret ;
268+ // remove tags from content
269+ for ( const tag of ret ) {
270+ content = content . replace ( `#${ tag } ` , "" ) ;
271+ }
272+ // remove spaces and trailing \n from content
273+ content = content . trim ( ) . replace ( / \n + $ / , "" ) ;
274+
275+ return { tags : ret , content } ;
276+ }
277+ async function copyPasteContent ( page , content ) {
278+ await page . evaluate ( async ( text ) => {
279+ await navigator . clipboard . writeText ( text ) ;
280+ } , content || "" ) ;
281+ await page . keyboard . press (
282+ process . platform === "darwin" ? "Meta+V" : "Control+V"
283+ ) ;
242284}
0 commit comments