134dd063c3
- 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.
84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import { browser } from "wxt/browser";
|
|
import { defineContentScript } from "wxt/utils/define-content-script";
|
|
|
|
type PublisherRequestMessage = {
|
|
source: "geo-admin";
|
|
type: "geo.publisher.request";
|
|
requestId: string;
|
|
action: "ping" | "detectPlatforms" | "registerInstallation" | "bindAccount" | "publishArticle";
|
|
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() {
|
|
window.addEventListener("message", async (event: MessageEvent<PublisherRequestMessage>) => {
|
|
if (event.source !== window || !event.data || event.data.type !== "geo.publisher.request") {
|
|
return;
|
|
}
|
|
if (event.data.source !== "geo-admin") {
|
|
return;
|
|
}
|
|
|
|
const { requestId, action, payload } = event.data;
|
|
|
|
try {
|
|
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",
|
|
type: "geo.publisher.response",
|
|
requestId,
|
|
success: true,
|
|
data,
|
|
},
|
|
window.location.origin,
|
|
);
|
|
} catch (error) {
|
|
window.postMessage(
|
|
{
|
|
source: "geo-extension",
|
|
type: "geo.publisher.response",
|
|
requestId,
|
|
success: false,
|
|
error: error instanceof Error ? error.message : "publisher_plugin_unknown_error",
|
|
},
|
|
window.location.origin,
|
|
);
|
|
}
|
|
});
|
|
},
|
|
});
|