Bilibili Dynamic Publishing Feature Development Record
Add Platform at Corresponding Location
Register Bilibili
platform's dynamic publishing in the src/sync/dynamic.ts
file.
Dynamic Publishing Function
The dynamic publishing function is the core of the dynamic publishing feature. This function will be injected into the page opened by injectUrl
.
We implement the DynamicBilibili
function in the src/sync/dynamic/bilibili.ts
file. You can view the specific code by clicking here.
The following record is extracted from the DynamicBilibili
function, mainly recording some key steps of dynamic publishing.
1. Open Dynamic Publishing Page
Bilibili's dynamic publishing page is https://t.bilibili.com
, we first need to open this page; this step is already defined when injecting the script.
2. Find Title and Content Input Boxes and Input Content
After entering the dynamic publishing page, we need to find the title and content input boxes.
Manually open Bilibili's dynamic publishing page, then open F12 developer tools, right-click on the title and content input boxes, select Inspect
, and find the corresponding elements.
Through element inspection, we can find the title and content input box elements as follows:
From the above code, we can see the key elements of the title and content input boxes are:
- Title input box:
<input maxlength="20" placeholder="好的标题更容易获得支持,选填20字" class="bili-dyn-publishing__title__input">
- Content input box:
<div placeholder="有什么想和大家分享的?" contenteditable="true" class="bili-rich-textarea__inner empty" style="font-size: 15px; line-height: 24px; min-height: 24px;"></div>
To input content into the title and content input boxes, we need to first find these two elements, then input content into them. In the code it appears as follows:
3. Upload Images
After completing the above steps, we need to upload images.
For general platforms, image uploading is done through type="file"
input tags.
But in Bilibili's dynamic publishing page, we can't directly find type="file"
input tags.
For this, we think this Input might be hidden and not directly mounted to the page.
So we create content scripts to listen for Input creation. The specific code is in the src/contents/helper.ts
file.
By rewriting the document.createElement
method, we listened to the creation of input
tags, then use the createdInputs
array to store the created input
tags.
During development, you can print out the created input
tags through console.log(createdInputs)
.
After injecting the script, we found the hidden input
tag. Next, we need to use window.postMessage
to pass files to the content script during publishing. This is why the helper.ts
file needs to listen for message
events through window.addEventListener('message', handleMessage);
.
We return to the src/sync/dynamic/bilibili.ts
file and use window.postMessage
to pass files to the content script during publishing.
First, because Bilibili's dynamic publishing page caches images from the last unsubmitted upload, we need to clean up uploaded images first.
4. Publish Dynamic
After completing the above steps, we need additional processing for auto-publishing. Specifically, find the publish button and click it.
The specific code is in the src/sync/dynamic/bilibili.ts
file.
Mission Accomplished
So far, we have completed Bilibili's dynamic publishing feature.
The difficulty of Bilibili's dynamic publishing mainly lies in image upload processing, because there's no obvious type="file"
input tag.
For this, we listened to the creation of input
tags by rewriting the document.createElement
method, then used the createdInputs
array to store the created input
tags.
During publishing, we use window.postMessage
to pass files to the content script.