@skrillex1224/chrome-article-publish-extension
v1.0.14
Published
Chrome-extension runtime article publishing adapters for Chinese content platforms.
Readme
@skrillex1224/chrome-article-publish-extension
Chrome extension runtime package for publishing articles through platform adapters.
This repository contains two separate parts:
src/: the reusable npm package. It exposes onlycheckAuth,publish,getStatus, andcreateExtensionRuntime.extension-app/: a standalone Chrome extension UI that imports the npm package and provides article storage, editing, import/export, publishing, retry, and status pages.
The npm package is the kernel. The extension UI is one host application. Other Chrome extensions can install the package and call the same kernel APIs directly.
Current Scope
The package is designed for low-frequency publishing from a user's own logged-in Chrome profile. It follows the public frontend behavior of each platform and uses platform cookies/session state from the current browser profile.
It is not a Node CLI package and should not be used from a server to copy or reuse user cookies.
Supported Platforms
| Platform ID | UI Name | Publish Type | Main Inputs |
|---|---|---|---|
| baijiahao | 百家号 | Article | title, markdown, description, cover, tags |
| bilibili | 哔哩哔哩 | Opus/article | title, markdown, description, cover, tags |
| cnblogs | 博客园 | Article | title, markdown, tags |
| csdn | CSDN | Article | title, markdown, description, cover, tags |
| 51cto | 51CTO | Article | title, markdown, description, cover, tags |
| douyin | 抖音图文 | Image-text note | title, description, tags, images |
| douyin | 抖音视频 | Video | title, description, tags, video, cover |
| juejin | 掘金 | Article | title, markdown, description, cover, tags |
| netease | 网易号 | Article | title, markdown, description, cover |
| sohu | 搜狐号 | Article | title, markdown, description, cover |
| tencent | 腾讯号 | Article | title, markdown, description, cover |
| toutiao | 今日头条 | Article | title, markdown, description, cover |
| weixin | 微信公众号 | Article | title, markdown, description, cover |
Notes:
douyinuses one package adapter for both image-text and video. The extension UI exposes them as two entries:douyinanddouyin-video.- Platform policy can still put a post into review, reject it, require security verification, or block it because of account limits.
publish()success means the platform accepted the publish/submit request and returned a queryablestatusRef. Public visibility must be checked throughgetStatus().
Install The Npm Package
npm install @skrillex1224/chrome-article-publish-extensionOr with pnpm:
pnpm add @skrillex1224/chrome-article-publish-extensionChrome extension pages and service workers cannot use a bare npm import at runtime by themselves. Bundle this package into your extension with Vite, Rollup, tsup, webpack, or another build tool.
Manifest Requirements
A host extension needs enough Chrome permissions for cookies, cross-origin requests, platform page context, downloads, and managed temporary tabs:
{
"manifest_version": 3,
"background": {
"service_worker": "background.js",
"type": "module"
},
"permissions": [
"storage",
"unlimitedStorage",
"cookies",
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess",
"scripting",
"tabs",
"tabGroups",
"downloads"
],
"host_permissions": [
"http://*/*",
"https://*/*"
]
}If your extension uses a side panel UI, also add sidePanel. The npm package itself does not require a side panel.
Quick Start
import {
API_STATES,
API_STATUS,
checkAuth,
createExtensionRuntime,
getStatus,
publish,
type PublishEvent,
} from '@skrillex1224/chrome-article-publish-extension'
const runtime = createExtensionRuntime({
timeout: 3_600_000,
})
const auth = await checkAuth({
platform: 'bilibili',
runtime,
timeoutMs: 60_000,
})
if (auth.code !== API_STATUS.SUCCESS.code || !auth.data.isAuthenticated) {
throw new Error(auth.data.reason || auth.text)
}
try {
const publishResult = await publish({
platform: 'bilibili',
runtime,
timeoutMs: 180_000,
article: {
title: '人为什么会饿',
markdown: '# 人为什么会饿\n\n饥饿是一套身体提醒系统。',
description: '人会饿,不只是因为胃空了。',
cover: 'https://example.com/cover.jpg',
tags: ['健康', '科普'],
},
async onEvent(event: PublishEvent) {
if (event.type === 'security_check') {
console.log(event.method, event.message, event.url, event.expiresAt)
}
},
})
if (publishResult.state !== API_STATES.SUCCESS) {
throw new Error(publishResult.data.reason || publishResult.text)
}
if (!publishResult.data.statusRef) {
throw new Error('发布成功但没有返回 statusRef')
}
const status = await getStatus({
platform: 'bilibili',
runtime,
statusRef: publishResult.data.statusRef,
timeoutMs: 60_000,
})
console.log(status.code, status.state, status.text, status.data.postUrl, status.data.reason)
} catch (error) {
throw error
}Public API
Public Result Envelope
checkAuth(), publish(), and getStatus() all return the same envelope shape:
type ApiStateCode = 0 | 10000 | 20000 | 50000 | 60000
type ApiResult<TData extends object = Record<string, unknown>> = {
code: number
state: ApiStateCode
text: string
data: TData
}state is the coarse flow-control value:
| State | Constant | Meaning | Caller behavior |
|---:|---|---|---|
| 0 | API_STATES.SUCCESS | Operation succeeded. | Continue normally. |
| 10000 | API_STATES.PENDING | Platform is still processing, syncing, or reviewing. | Poll later. |
| 20000 | API_STATES.STUCKING | Human action is required, such as login or security verification. | Ask the user to act. |
| 50000 | API_STATES.FAILED_RETRYABLE | Retryable failure such as timeout, temporary platform request failure, or media transfer failure. | The host app may retry. |
| 60000 | API_STATES.FAILED_FINAL | Final failure such as invalid input, platform rejection, unsupported platform, adapter protocol failure, or unknown fallback. | Show the reason and fix input, account, environment, or code before retrying. |
code is the specific reason. Use API_STATUS constants instead of hard-coded numbers when possible. text is a Chinese UI-safe label. Platform or adapter detail belongs in data.reason; there is no top-level message field.
Important codes:
| Code | Constant | Meaning |
|---:|---|---|
| 0 | API_STATUS.SUCCESS | 成功 |
| 10001 | API_STATUS.CONTENT_REVIEWING | 文章审核中 |
| 10002 | API_STATUS.PUBLIC_URL_SYNCING | 公开链接等待同步 |
| 20001 | API_STATUS.AUTH_REQUIRED | 未登录或登录已失效 |
| 20002 | API_STATUS.SECURITY_CHECK_REQUIRED | 需要安全验证 |
| 50004 | API_STATUS.OPERATION_TIMEOUT | 操作超时 |
| 60001 | API_STATUS.INVALID_INPUT | 请求参数无效 |
| 61003 | API_STATUS.CONTENT_REJECTED | 内容审核未通过 |
| 66001 | API_STATUS.ADAPTER_PROTOCOL_ERROR | 适配器协议错误 |
| 900001 | API_STATUS.UNKNOWN | 未知状态 |
createExtensionRuntime(config?)
Creates the Chrome extension runtime used by all adapters.
type RuntimeConfig = {
timeout?: number
userAgent?: string
}Recommended timeout:
- Article-only publishing: at least
120_000. - Image/video upload publishing: use a longer timeout, for example
3_600_000.
The runtime handles:
- authenticated
fetch()from the extension profile - cookie read/write
- local/session storage helpers
- declarative header rules
- downloads
- tab creation/reuse/close
chrome.scripting.executeScript
Temporary tabs created by the runtime are grouped under a Chrome tab group named Article Publish Runtime. Existing user-opened matching platform tabs are reused and not closed by the runtime.
checkAuth(request)
Checks whether the current Chrome profile is logged in to the platform.
type CheckAuthRequest = {
platform: string
runtime: RuntimeInterface
timeoutMs?: number
}
type CheckAuthData = {
isAuthenticated: boolean
username?: string
userId?: string
avatar?: string
reason?: string
traceId?: string
}
type CheckAuthResult = ApiResult<CheckAuthData>Use this before showing a platform as ready to publish. A failed auth check returns code: 20001, state: 20000, and data.isAuthenticated: false. Display data.reason || text.
Authentication Preflight
publish() and getStatus() always run the target adapter's checkAuth() before doing the real platform action. If the current Chrome profile is not logged in, the public call returns an auth envelope instead of reaching the platform publish or status endpoint:
{
code: 20001,
state: 20000,
text: '未登录或登录已失效',
data: {
reason: '请先登录目标平台',
traceId: '...',
},
}This is intentional. Host extensions should mark the platform as not logged in and show a login action. checkAuth() itself uses the same envelope and does not require a separate exception path.
publish(request)
Publishes or submits an article to one platform.
type PublishRequest = {
platform: string
runtime: RuntimeInterface
article: PublishArticleInput
onEvent?: (event: PublishEvent) => void | Promise<void>
timeoutMs?: number
options?: {
onImageProgress?: (current: number, total: number) => void
}
}
type PublishArticleInput = {
title: string
markdown: string
description?: string
cover?: string
tags?: string[]
images?: Array<{ url: string }>
video?: {
url: string
filename?: string
mimeType?: string
}
}Field contract:
title: platform title.markdown: canonical article body. This is the only public body input.description: summary, digest, excerpt, caption, or platform description field depending on the target platform.cover: cover image URL. Onlyhttp://andhttps://URLs are supported.tags: tag names asstring[]. The caller should pass an array, not a semicolon-joined string.images: image URL list for image-text media flows, currently mainly Douyin image-text.video: video URL for video media flows, currently Douyin video.
Do not pass html, body, content, summary, or category. The new contract intentionally does not support those aliases. Adapters convert markdown to HTML, rich text, or platform-specific payloads internally.
Return type:
type PublishData = {
statusRef?: PublishStatusRef
postId?: string
postUrl?: string
reason?: string
traceId?: string
event?: PublishEvent
}
type PublicPublishResult = ApiResult<PublishData>state: 0 means:
- the platform accepted the publish/submit request
- the adapter returned enough data for
getStatus() data.statusRefmust be stored by the host extension
It does not guarantee that the article is already publicly visible. Use getStatus() for that.
If state is not 0, display data.reason || text. The host app should only retry automatically when state === API_STATES.FAILED_RETRYABLE.
getStatus(request)
Queries the platform status using the statusRef returned by publish().
type GetStatusRequest = {
platform: string
runtime: RuntimeInterface
statusRef: PublishStatusRef
timeoutMs?: number
}
type PublishStatusRef = {
platform: string
kind: string
id?: string
url?: string
data?: Record<string, string | number | boolean | null>
}
type GetStatusData = {
statusRef?: PublishStatusRef
postId?: string
postUrl?: string
reason?: string
traceId?: string
rawStatus?: string | number
checkedAt?: number
}
type GetStatusResult = ApiResult<GetStatusData>Store statusRef exactly as returned. The host UI should not parse or rewrite it.
Status meanings:
code: 0,state: 0: the platform reports a public or published state. Usedata.postUrlwhen present.code: 10001,state: 10000: the platform accepted the content but it is still in review or pending. Poll later.code: 61003,state: 60000: the platform rejected or blocked the content. Showdata.reason || text.code: 61004,state: 60000: the platform could not find the content by the returned reference.code: 900001,state: 60000: the adapter could not determine the state. Export diagnostics and investigate before treating it as a normal retry case.
Operation Timeout And Retry
The npm package has operation-level timeout protection for its three public calls:
| Operation | Default Timeout | Timeout Result |
|---|---:|---|
| publish() | 180_000ms | code: 50004, state: 50000, text: '操作超时' |
| checkAuth() | 60_000ms | code: 50004, state: 50000, data.isAuthenticated: false |
| getStatus() | 60_000ms | code: 50004, state: 50000, text: '操作超时' |
Override the operation timeout by passing timeoutMs on the request. This timeout is separate from createExtensionRuntime({ timeout }), which is used by lower-level runtime requests.
The timeout makes the public call settle; it does not guarantee that an already-started browser request or platform-side action was cancelled. The npm package therefore does not retry operations. Retry policy belongs to the host extension or business application because only the caller knows whether a retry may duplicate work or should be exposed to the user.
The included extension app retries public API results only when state === API_STATES.FAILED_RETRYABLE (50000) and marks stale publishing tasks as failed so they can be retried from the UI. It does not retry PENDING, STUCKING, or FAILED_FINAL automatically.
Diagnostics Logs
The npm package records lightweight diagnostics for checkAuth(), publish(), and getStatus() without requiring host UI changes. Logs are kept in memory and, when the runtime provides storage, persisted under:
chromeArticlePublish:diagnostics:v1The logs are meant for debugging stuck or failed platform publishing. They include operation start/end, article input, final result/error, runtime fetch() calls, request/response payload previews, managed tab operations, script execution, publish events, and header rule changes.
Enable or tune diagnostics:
import { configureDiagnostics } from '@skrillex1224/chrome-article-publish-extension'
configureDiagnostics({
enabled: true,
level: 'debug',
persist: true,
maxTraces: 100,
maxEventsPerTrace: 300,
capturePayloads: true,
maxPayloadChars: 20000,
})Export logs from code:
import { exportDiagnostics } from '@skrillex1224/chrome-article-publish-extension'
const diagnostics = await exportDiagnostics({ runtime })
console.log(JSON.stringify(diagnostics, null, 2))Export logs from a Chrome extension service worker console:
await globalThis.__CAP_DIAGNOSTICS__.export()If the service worker was restarted and the package hook is not available, read the persisted value directly:
await chrome.storage.local.get('chromeArticlePublish:diagnostics:v1')Useful fields when troubleshooting:
trace.article: title/body/description/tag/media counts.operation.start.data.articleInput: actualtitle,description,tags,cover,images,video, and truncatedmarkdown.auth.preflight.start/end: auth check automatically run beforepublish()andgetStatus().fetch.start.data.requestBody: the body sent to the platform, includingFormDatafields when available.fetch.end.data.responseBody: text/JSON response preview from the platform.publish.event: security check or progress events emitted by an adapter before the finalpublish()result.
Diagnostics density is intentionally high at the package boundary and runtime boundary. For every platform that uses the package runtime, logs show the public operation, auth preflight, all runtime fetches, request/response payload previews, temporary tab/script operations, header rule lifecycle, and the final result. That is enough to debug the usual failures: stuck calls, unauthenticated state, malformed payloads, field truncation, missing tags/images/video, platform business errors, security checks, status lookup failures, and timeout-shaped failures.
When adding or modifying a platform adapter:
- Route platform HTTP calls through
this.get(),this.post(),runtime.fetch(), or the existing adapter helpers so diagnostics can capture URL, request body, response body, status, and duration. - Route temporary browser work through
runtime.tabs.withManagedTab()/executeScript()instead of rawchrome.tabscalls so tab lifecycle is visible. - Use
options.onEvent({ type: 'security_check', ... })for CAPTCHA, QR code, risk control, or any manual verification. The publicpublish()wrapper records these aspublish.event. - Return explicit
SyncResult.error/messagefrom adapter failures. The public wrapper records the exact final error. - Keep adapter-specific logs for human-readable messages if useful, but do not depend on console logs for diagnostics; the exportable diagnostics JSON is the support artifact.
Security Check Events
Some platforms may require QR code confirmation, CAPTCHA, or a manual security step. In that case publish() emits an event and waits until the platform flow completes or times out.
type PublishEvent = {
type: 'progress' | 'security_check'
platform: string
message: string
expiresAt: number
url?: string
method?: 'qrcode' | 'captcha' | 'manual'
}Host UI guidance:
- Display
messagedirectly. - If
urlexists, show it as a QR image or openable verification link. - Respect
expiresAtand show timeout state if the user does not complete the action. - Keep the publish task as
publishingwhile the package is waiting. - If
publish()returns a non-success envelope, showdata.reason || text. Only auto-retrystate: 50000.
Douyin Media Rules
The package uses platform: 'douyin' for both Douyin image-text and Douyin video. The media input decides the mode.
Image-text:
await publish({
platform: 'douyin',
runtime,
article: {
title: '图文标题',
markdown: '',
description: '图文描述',
tags: ['旅行', '生活'],
images: [
{ url: 'https://example.com/1.jpg' },
{ url: 'https://example.com/2.jpg' },
],
},
})Video:
await publish({
platform: 'douyin',
runtime,
article: {
title: '视频标题',
markdown: '',
description: '视频描述',
cover: 'https://example.com/cover.jpg',
tags: ['科技', 'AI'],
video: {
url: 'https://example.com/video.mp4',
filename: 'video.mp4',
mimeType: 'video/mp4',
},
},
})Rules:
- Pass either
imagesorvideo, not both. images[].url,video.url, andcovermust be publichttp(s)URLs.- The package does not convert Markdown into Douyin image cards. The extension UI has an optional Markdown-to-image helper for that UI workflow.
Extension UI
The included extension app lives in extension-app/. It is useful as:
- a ready-to-install manual publishing tool
- a reference implementation for another extension
- an example of how to keep UI state separate from the npm kernel
Build And Package
From article-publish-extension/:
pnpm install
pnpm build:extension-app
pnpm zip:extension-appThe zip output is:
chrome-article-publish-extension-1.0.14.zipFor local development:
pnpm --filter chrome-article-publish-extension build
pnpm --filter chrome-article-publish-extension test
pnpm --filter chrome-article-publish-extension typecheckInstall In Chrome
- Open
chrome://extensions. - Enable Developer Mode.
- Choose "Load unpacked" and select
article-publish-extension/extension-app/dist. - Or unzip
chrome-article-publish-extension-1.0.14.zipand load the extracted directory. - Log in to target platforms in the same Chrome profile.
- Click the extension icon to open the side panel.
UI Flow
Create article
- Click
新建. - The editor opens as a full extension tab.
- Fill
title,description,cover,tags,images,video, andmarkdown. - Save the article into local Chrome storage.
- Click
Import articles
- Click
导入. - Upload a
.csvor.xlsxfile. - Select sheet, map fields, preview rows, and import.
.xlsis not supported.
- Click
Select articles
- The side panel lists saved articles.
- Click article cards or checkboxes to select one or more articles.
- Click
去发布.
Select platforms
- The side panel checks login state with
checkAuth(). - Logged-in platforms can be selected.
- Not-logged-in platforms show the auth failure reason and should be opened manually for login.
- The side panel checks login state with
Publish
- Click
一键发布. - The background service worker calls package
publish()for each selected article/platform task. - Already succeeded tasks are skipped and not published again.
- Failed tasks can be retried.
- Click
Check status
- After successful submission, the UI stores
statusRef. - Status refresh calls package
getStatus(). - The platform detail page shows per-article publish result, current status, reason, and public link when available.
- After successful submission, the UI stores
Export links
- Click
导出链接. - The UI exports an
.xlsxworkbook with article/platform link and status data.
- Click
Extension Storage Model
The UI stores records in Chrome local storage:
articlePublishExtension:articles:v2articlePublishExtension:tasks:v2articlePublishExtension:statuses:v2articlePublishExtension:auth:v2articlePublishExtension:sidePanel:v2
Article records use the same hard-cut input model as the npm package:
type ArticleRecord = {
id: string
title: string
description?: string
markdown: string
cover?: string
tags: string[]
images?: Array<{ url: string }>
video?: {
url: string
filename?: string
mimeType?: string
}
createdAt: number
updatedAt: number
}Publish tasks are keyed by articleId + platform. A task with state: 'succeeded' is not republished by the UI. A failed task can be retried, reusing the same logical task slot.
CSV/XLSX Import Fields
The import page accepts these canonical fields:
| Field | Chinese Alias | Required | Meaning |
|---|---|---:|---|
| title | 标题 | No | Article title. Empty title becomes 未命名文章. |
| markdown | 正文 | No | Canonical article body. Empty string is allowed for media-only tasks. |
| description | 摘要 | No | Summary/description/caption. |
| cover | 封面 | No | Cover image URL. |
| tags | 标签 | No | JSON string array or text split by ;, ;, ,, ,, or newline. |
| images | 图片 | No | JSON array, URL list, or { "url": "..." } array. |
| video | 视频 | No | One video URL. |
The UI intentionally keeps one English field and one Chinese alias per concept.
OSS Upload In The UI
The extension UI can upload local images/videos or generated Markdown image pages to OSS, then fill URL fields back into the article.
Configuration is stored only in Chrome local storage, not in source code or the npm package:
type OssConfig = {
endpoint: string
accessKeyId: string
accessKeySecret: string
bucketName: string
prefix: string
}Do not ship real access keys in a public extension build. If browser direct upload is used, the OSS bucket must allow the extension origin in CORS settings.
The Markdown-to-image feature is UI-only. It is mainly for generating Douyin image-text images. The npm package still receives only URL-based images.
Package And UI Boundary
The npm package owns:
- platform adapters
checkAuthpublishgetStatus- platform-specific Markdown conversion
- Chrome extension runtime helpers
- security check events
- managed runtime tabs
The extension UI owns:
- article creation and editing
- local article/task/status storage
- CSV/XLSX import
- OSS upload configuration
- Markdown preview and Markdown-to-image generation
- platform selection
- retry buttons
- status polling cadence
- export workbook
The extension UI should call the package. It should not import adapter internals.
Local Verification
Useful commands:
pnpm typecheck
pnpm build:npm
pnpm build:extension-app
pnpm zip:extension-app
pnpm --filter chrome-article-publish-extension typecheck
pnpm --filter chrome-article-publish-extension test
pnpm --filter chrome-article-publish-extension buildAdapter-focused checks live in scripts/, for example:
pnpm check:public-api
pnpm check:publish-contract
pnpm check:douyin-images
pnpm check:douyin-video
pnpm check:tencent-auth
pnpm check:tab-groupLive E2E scripts require a logged-in Chrome profile and the target platform account. They should be run one platform at a time and their result should be verified through publish() and getStatus().
Troubleshooting
code: 20001 未登录
The user is not logged in to the platform in the same Chrome profile as the extension. Open the platform homepage, log in, then run checkAuth() again.
code: 60002 不支持的平台
The platform string is not one of the supported package platform IDs. For the extension UI, douyin-video is a UI-only ID and maps to package platform douyin.
Publish succeeded but no public URL
Call getStatus() with the returned data.statusRef. Some platforms only expose a public URL after review or after management status changes to published.
code: 20002 需要安全验证
Listen to onEvent. Show event.message, event.url, and event.expiresAt. The package waits until the platform flow completes or times out.
Images or video fail to upload
Check that media URLs are public http(s) URLs and that the platform account is allowed to upload that media type. For UI OSS uploads, also check OSS CORS and credentials.
Extension opens temporary platform tabs
Some adapters need a platform page context for upload/status data. Runtime-created tabs are grouped under Article Publish Runtime and are closed after use when the adapter requested a temporary tab. Existing matching user tabs are reused and not closed.
Bare import fails in extension page
Bundle the package. Chrome extension pages cannot resolve a bare specifier like @skrillex1224/chrome-article-publish-extension unless your build tool has bundled it.
Not Included
- No server-side publishing.
- No cross-profile cookie copying.
- No old Wechatsync import dependency.
- No public CDN runtime loader.
- No built-in task database in the npm package.
- No publish scheduler in the npm package.
- No compatibility with old
html,body,content,summary, orcategoryinputs.
