feat: Add platform adapters for various content publishing platforms

- Implemented Dongchedi adapter for user detection and publishing.
- Implemented Jianshu adapter for user detection and publishing.
- Implemented Juejin adapter for user detection and publishing.
- Implemented Qiehao adapter for user detection and publishing.
- Implemented Smzdm adapter for user detection and publishing.
- Implemented Sohuhao adapter for user detection and publishing.
- Implemented Toutiaohao adapter for user detection and publishing.
- Implemented Wangyihao adapter for user detection and publishing.
- Implemented Weixin Gzh adapter for user detection and publishing.
- Implemented Zol adapter for user detection and publishing.
- Added documentation for manual testing of media publishing.
This commit is contained in:
2026-04-03 17:48:30 +08:00
parent 32d6a462cd
commit 134dd063c3
33 changed files with 2722 additions and 457 deletions
@@ -4,6 +4,16 @@ import { defineBackground } from "wxt/utils/define-background";
import { handlePublisherAction } from "../src/runtime";
import { getExtensionState } from "../src/storage";
type BackgroundSuccessResponse = {
ok: true;
data: Awaited<ReturnType<typeof handlePublisherAction>>;
};
type BackgroundErrorResponse = {
ok: false;
error: string;
};
export default defineBackground(() => {
void getExtensionState();
@@ -21,11 +31,25 @@ export default defineBackground(() => {
await getExtensionState();
});
browser.runtime.onMessage.addListener((message) => {
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (!message || message.type !== "geo.publisher.request") {
return undefined;
}
return handlePublisherAction(message.action, message.payload);
void handlePublisherAction(message.action, message.payload)
.then((data) => {
sendResponse({
ok: true,
data,
} satisfies BackgroundSuccessResponse);
})
.catch((error: unknown) => {
sendResponse({
ok: false,
error: error instanceof Error ? error.message : "publisher_plugin_unknown_error",
} satisfies BackgroundErrorResponse);
});
return true;
});
});
+28 -1
View File
@@ -9,6 +9,20 @@ type PublisherRequestMessage = {
payload?: unknown;
};
type BackgroundSuccessResponse = {
ok: true;
data: unknown;
};
type BackgroundErrorResponse = {
ok: false;
error?: string;
};
function isBackgroundResponse(value: unknown): value is BackgroundSuccessResponse | BackgroundErrorResponse {
return typeof value === "object" && value !== null && "ok" in value;
}
export default defineContentScript({
matches: ["http://*/*", "https://*/*"],
main() {
@@ -23,12 +37,25 @@ export default defineContentScript({
const { requestId, action, payload } = event.data;
try {
const data = await browser.runtime.sendMessage({
const response = await browser.runtime.sendMessage({
type: "geo.publisher.request",
action,
payload,
});
const data = isBackgroundResponse(response)
? (() => {
if (!response.ok) {
throw new Error(response.error || "publisher_plugin_unknown_error");
}
return response.data;
})()
: response;
if (typeof data === "undefined") {
throw new Error("publisher_plugin_empty_response");
}
window.postMessage(
{
source: "geo-extension",
+65 -209
View File
@@ -2,14 +2,22 @@
import { computed, onMounted, ref } from "vue";
import { browser } from "wxt/browser";
import { getExtensionState, resetPlatformStates, updatePlatformState } from "../../src/storage";
import { normalizeRemoteUrl } from "../../src/adapters/common";
import { getExtensionState } from "../../src/storage";
import type { StoredPlatformState } from "../../src/platforms";
type BackgroundSuccessResponse = {
ok: true;
data: unknown;
};
type BackgroundErrorResponse = {
ok: false;
error?: string;
};
const loading = ref(true);
const refreshing = ref(false);
const resetting = ref(false);
const savingId = ref<string | null>(null);
const expandedId = ref<string | null>(null);
const installationKey = ref("");
const installationId = ref<number | null>(null);
@@ -33,6 +41,10 @@ function clonePlatforms(items: StoredPlatformState[]): StoredPlatformState[] {
return items.map((platform) => ({ ...platform }));
}
function isBackgroundResponse(value: unknown): value is BackgroundSuccessResponse | BackgroundErrorResponse {
return typeof value === "object" && value !== null && "ok" in value;
}
async function loadState(): Promise<void> {
loading.value = true;
try {
@@ -64,8 +76,9 @@ function uidFor(platform: StoredPlatformState): string {
}
function avatarFor(platform: StoredPlatformState): string | null {
if (platform.avatar_url?.trim()) {
return platform.avatar_url.trim();
const normalizedAvatar = normalizeRemoteUrl(platform.avatar_url);
if (normalizedAvatar) {
return normalizedAvatar;
}
if (platform.connected) {
return `https://api.dicebear.com/9.x/initials/svg?seed=${encodeURIComponent(titleFor(platform))}`;
@@ -73,13 +86,6 @@ function avatarFor(platform: StoredPlatformState): string | null {
return null;
}
function statusLabel(platform: StoredPlatformState): string {
if (refreshing.value || loading.value) {
return "检测中...";
}
return platform.connected ? "已登录" : "未登录";
}
function statusTone(platform: StoredPlatformState): "success" | "danger" | "pending" {
if (refreshing.value || loading.value) {
return "pending";
@@ -87,36 +93,18 @@ function statusTone(platform: StoredPlatformState): "success" | "danger" | "pend
return platform.connected ? "success" : "danger";
}
function fillDemo(platform: StoredPlatformState): void {
platform.connected = true;
platform.nickname = platform.nickname?.trim() || `${platform.platform_name} Demo`;
platform.platform_uid = (platform.platform_uid != null ? String(platform.platform_uid).trim() : "") || `${platform.platform_id}-demo`;
platform.avatar_url =
platform.avatar_url?.trim() ||
`https://api.dicebear.com/9.x/initials/svg?seed=${encodeURIComponent(platform.nickname || platform.platform_name)}`;
platform.message = null;
}
function toggleExpanded(platformId: string): void {
expandedId.value = expandedId.value === platformId ? null : platformId;
}
async function savePlatform(platform: StoredPlatformState): Promise<void> {
savingId.value = platform.platform_id;
try {
const state = await updatePlatformState(platform.platform_id, {
connected: platform.connected,
nickname: platform.nickname?.trim() || null,
platform_uid: platform.platform_uid != null ? String(platform.platform_uid).trim() || null : null,
avatar_url: platform.avatar_url?.trim() || null,
message: platform.connected ? null : "Sign in required",
});
installationId.value = state.plugin_installation_id;
apiBaseUrl.value = state.api_base_url;
platforms.value = clonePlatforms(state.platforms);
} finally {
savingId.value = null;
function loginButtonLabel(platform: StoredPlatformState): string {
if (refreshing.value || loading.value) {
return "检测中...";
}
return "未登录";
}
async function openLoginPage(platform: StoredPlatformState): Promise<void> {
if (!platform.login_url || refreshing.value || loading.value) {
return;
}
await browser.tabs.create({ url: platform.login_url });
}
async function simulateDetect(): Promise<void> {
@@ -126,10 +114,13 @@ async function simulateDetect(): Promise<void> {
async function refreshDetectedPlatforms(): Promise<void> {
refreshing.value = true;
try {
await browser.runtime.sendMessage({
const response = await browser.runtime.sendMessage({
type: "geo.publisher.request",
action: "detectPlatforms",
});
if (isBackgroundResponse(response) && !response.ok) {
throw new Error(response.error || "publisher_plugin_unknown_error");
}
const state = await getExtensionState();
installationId.value = state.plugin_installation_id;
apiBaseUrl.value = state.api_base_url;
@@ -138,17 +129,6 @@ async function refreshDetectedPlatforms(): Promise<void> {
refreshing.value = false;
}
}
async function handleReset(): Promise<void> {
resetting.value = true;
try {
const state = await resetPlatformStates();
platforms.value = clonePlatforms(state.platforms);
expandedId.value = null;
} finally {
resetting.value = false;
}
}
</script>
<template>
@@ -169,9 +149,8 @@ async function handleReset(): Promise<void> {
v-for="platform in platforms"
:key="platform.platform_id"
class="platform-card"
:class="[`is-${statusTone(platform)}`, { 'is-expanded': expandedId === platform.platform_id }]"
:class="[`is-${statusTone(platform)}`]"
:style="{ '--platform-accent': platform.accent_color }"
@click="toggleExpanded(platform.platform_id)"
>
<div class="platform-card__accent" />
@@ -203,47 +182,17 @@ async function handleReset(): Promise<void> {
</div>
</div>
<div v-else class="platform-card__status-chip" :class="`is-${statusTone(platform)}`">
{{ statusLabel(platform) }}
</div>
<button
v-else
type="button"
class="platform-card__status-chip"
:class="`is-${statusTone(platform)}`"
:disabled="!platform.login_url || refreshing || loading"
@click.stop="openLoginPage(platform)"
>
{{ loginButtonLabel(platform) }}
</button>
</div>
<section v-if="expandedId === platform.platform_id" class="platform-editor" @click.stop>
<div class="platform-editor__grid">
<label>
<span>昵称</span>
<input v-model="platform.nickname" type="text" placeholder="比如 CharmingGeeker" />
</label>
<label>
<span>UID</span>
<input v-model="platform.platform_uid" type="text" placeholder="平台账号 UID" />
</label>
<label class="platform-editor__full">
<span>头像 URL</span>
<input v-model="platform.avatar_url" type="text" placeholder="可选" />
</label>
</div>
<div class="platform-editor__actions">
<label class="platform-editor__toggle">
<input v-model="platform.connected" type="checkbox" />
<span>{{ platform.connected ? "已登录" : "未登录" }}</span>
</label>
<div class="platform-editor__buttons">
<button type="button" class="ghost" @click="fillDemo(platform)">填充示例</button>
<button type="button" class="ghost" @click="platform.connected = false">设为未登录</button>
<button
type="button"
class="primary"
:disabled="savingId === platform.platform_id"
@click="savePlatform(platform)"
>
{{ savingId === platform.platform_id ? "保存中..." : "保存本地状态" }}
</button>
</div>
</div>
</section>
</article>
<p class="popup-status">{{ statusText }}</p>
@@ -264,15 +213,11 @@ async function handleReset(): Promise<void> {
<dd>{{ installationKey }}</dd>
</div>
</dl>
<button type="button" class="popup-reset" :disabled="resetting" @click="handleReset">
{{ resetting ? "重置中..." : "重置全部平台" }}
</button>
</details>
</section>
<footer class="popup-footer">
<button type="button" class="popup-detect" :disabled="refreshing || resetting" @click="simulateDetect">
<button type="button" class="popup-detect" :disabled="refreshing" @click="simulateDetect">
<span class="popup-detect__icon" :class="{ 'is-spinning': refreshing }"></span>
<span>{{ refreshing ? "检测中..." : "重新检测" }}</span>
</button>
@@ -390,7 +335,6 @@ async function handleReset(): Promise<void> {
transform 180ms ease,
box-shadow 180ms ease,
border-color 180ms ease;
cursor: pointer;
}
.platform-card:hover {
@@ -398,10 +342,6 @@ async function handleReset(): Promise<void> {
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}
.platform-card.is-expanded {
border-color: rgba(128, 156, 213, 0.6);
}
.platform-card__accent {
position: absolute;
inset: 0 auto 0 0;
@@ -527,6 +467,7 @@ async function handleReset(): Promise<void> {
}
.platform-card__status-chip {
appearance: none;
min-width: 72px;
padding: 6px 12px;
border: 1px solid transparent;
@@ -534,6 +475,21 @@ async function handleReset(): Promise<void> {
text-align: center;
font-size: 13px;
font-weight: 500;
cursor: pointer;
transition:
transform 150ms ease,
opacity 150ms ease,
box-shadow 150ms ease;
}
.platform-card__status-chip:hover:not(:disabled) {
transform: translateY(-1px);
}
.platform-card__status-chip:disabled {
cursor: not-allowed;
opacity: 0.72;
transform: none;
}
.platform-card__status-chip.is-danger,
@@ -548,80 +504,7 @@ async function handleReset(): Promise<void> {
border-color: rgba(74, 183, 159, 0.3);
background: #ffffff;
}
.platform-editor {
padding: 0 18px 18px 20px;
}
.platform-editor__grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.platform-editor__full {
grid-column: 1 / -1;
}
.platform-editor label {
display: block;
}
.platform-editor span {
display: block;
margin-bottom: 6px;
color: #8793a4;
font-size: 12px;
font-weight: 700;
}
.platform-editor input {
width: 100%;
padding: 12px 14px;
border: 1px solid rgba(216, 224, 238, 1);
border-radius: 14px;
outline: none;
background: rgba(248, 251, 255, 0.92);
color: #516173;
font-size: 13px;
}
.platform-editor input:focus {
border-color: rgba(90, 124, 255, 0.6);
box-shadow: 0 0 0 4px rgba(91, 126, 255, 0.08);
}
.platform-editor__actions {
display: flex;
flex-direction: column;
gap: 12px;
margin-top: 12px;
}
.platform-editor__toggle {
display: inline-flex;
align-items: center;
gap: 8px;
color: #6a788b;
font-size: 13px;
font-weight: 700;
}
.platform-editor__toggle input {
width: 16px;
height: 16px;
margin: 0;
}
.platform-editor__buttons {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.platform-editor__buttons button,
.popup-detect,
.popup-reset {
.popup-detect {
appearance: none;
border: none;
border-radius: 14px;
@@ -633,33 +516,16 @@ async function handleReset(): Promise<void> {
box-shadow 150ms ease;
}
.platform-editor__buttons button:hover,
.popup-detect:hover,
.popup-reset:hover {
.popup-detect:hover {
transform: translateY(-1px);
}
.platform-editor__buttons button:disabled,
.popup-detect:disabled,
.popup-reset:disabled {
.popup-detect:disabled {
cursor: not-allowed;
opacity: 0.68;
transform: none;
}
.ghost {
padding: 10px 14px;
background: rgba(242, 246, 252, 0.98);
color: #65758a;
}
.primary {
padding: 10px 16px;
background: linear-gradient(135deg, #4f7dff, #6a83ff);
color: #ffffff;
box-shadow: 0 10px 20px rgba(79, 125, 255, 0.2);
}
.popup-footer {
flex-shrink: 0;
margin: 0;
@@ -746,16 +612,6 @@ async function handleReset(): Promise<void> {
word-break: break-all;
}
.popup-reset {
width: calc(100% - 28px);
margin: 0 14px 14px;
padding: 10px 14px;
background: rgba(244, 114, 118, 0.12);
color: #df5e6d;
font-size: 13px;
font-weight: 800;
}
@keyframes spin {
from {
transform: rotate(0deg);