@jackwener/opencli 1.6.1 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +1 -1
- package/README.md +27 -45
- package/README.zh-CN.md +32 -34
- package/autoresearch/browse-tasks.json +18 -20
- package/autoresearch/commands/debug.ts +163 -0
- package/autoresearch/commands/fix.ts +145 -0
- package/autoresearch/commands/plan.ts +88 -0
- package/autoresearch/commands/run.ts +138 -0
- package/autoresearch/config.ts +82 -0
- package/autoresearch/engine.ts +359 -0
- package/autoresearch/eval-all.ts +127 -0
- package/autoresearch/eval-browse.ts +1 -1
- package/autoresearch/eval-publish.ts +238 -0
- package/autoresearch/eval-save.ts +249 -0
- package/autoresearch/eval-skill.ts +14 -8
- package/autoresearch/eval-v2ex.ts +220 -0
- package/autoresearch/eval-zhihu.ts +230 -0
- package/autoresearch/logger.ts +69 -0
- package/autoresearch/presets/combined-reliability.ts +27 -0
- package/autoresearch/presets/index.ts +23 -0
- package/autoresearch/presets/operate-reliability.ts +24 -0
- package/autoresearch/presets/save-reliability.ts +26 -0
- package/autoresearch/presets/skill-quality.ts +20 -0
- package/autoresearch/presets/v2ex-reliability.ts +24 -0
- package/autoresearch/presets/zhihu-reliability.ts +25 -0
- package/autoresearch/publish-tasks.json +345 -0
- package/autoresearch/run-save.sh +11 -0
- package/autoresearch/save-adapters/xhs-explore-deep.ts +64 -0
- package/autoresearch/save-adapters/xhs-note-comments.ts +61 -0
- package/autoresearch/save-adapters/xhs-search-full.ts +62 -0
- package/autoresearch/save-adapters/zhihu-hot-detail.ts +52 -0
- package/autoresearch/save-adapters/zhihu-question-full.ts +57 -0
- package/autoresearch/save-adapters/zhihu-search-detail.ts +53 -0
- package/autoresearch/save-tasks.json +281 -0
- package/autoresearch/v2ex-tasks.json +899 -0
- package/autoresearch/zhihu-tasks.json +848 -0
- package/dist/browser/base-page.d.ts +4 -2
- package/dist/browser/base-page.js +37 -4
- package/dist/browser/bridge.js +10 -8
- package/dist/browser/cdp.js +2 -6
- package/dist/browser/daemon-client.d.ts +11 -1
- package/dist/browser/daemon-client.js +3 -0
- package/dist/browser/dom-helpers.d.ts +4 -2
- package/dist/browser/dom-helpers.js +42 -31
- package/dist/browser/dom-snapshot.js +23 -1
- package/dist/browser/page.d.ts +7 -2
- package/dist/browser/page.js +112 -30
- package/dist/browser.test.js +1 -1
- package/dist/build-manifest.d.ts +1 -0
- package/dist/build-manifest.js +1 -0
- package/dist/cli-manifest.json +1135 -184
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +48 -7
- package/dist/cli.test.d.ts +1 -0
- package/dist/cli.test.js +88 -0
- package/dist/clis/1688/item.d.ts +70 -0
- package/dist/clis/1688/item.js +187 -0
- package/dist/clis/1688/item.test.d.ts +1 -0
- package/dist/clis/1688/item.test.js +67 -0
- package/dist/clis/1688/search.d.ts +56 -0
- package/dist/clis/1688/search.js +309 -0
- package/dist/clis/1688/search.test.d.ts +1 -0
- package/dist/clis/1688/search.test.js +75 -0
- package/dist/clis/1688/shared.d.ts +112 -0
- package/dist/clis/1688/shared.js +514 -0
- package/dist/clis/1688/shared.test.d.ts +1 -0
- package/dist/clis/1688/shared.test.js +57 -0
- package/dist/clis/1688/store.d.ts +45 -0
- package/dist/clis/1688/store.js +226 -0
- package/dist/clis/1688/store.test.d.ts +1 -0
- package/dist/clis/1688/store.test.js +62 -0
- package/dist/clis/amazon/bestsellers.d.ts +0 -20
- package/dist/clis/amazon/bestsellers.js +6 -129
- package/dist/clis/amazon/bestsellers.test.js +12 -3
- package/dist/clis/amazon/movers-shakers.d.ts +1 -0
- package/dist/clis/amazon/movers-shakers.js +7 -0
- package/dist/clis/amazon/new-releases.d.ts +1 -0
- package/dist/clis/amazon/new-releases.js +7 -0
- package/dist/clis/amazon/rankings.d.ts +59 -0
- package/dist/clis/amazon/rankings.js +226 -0
- package/dist/clis/amazon/rankings.test.d.ts +1 -0
- package/dist/clis/amazon/rankings.test.js +41 -0
- package/dist/clis/amazon/shared.d.ts +11 -0
- package/dist/clis/amazon/shared.js +121 -11
- package/dist/clis/amazon/shared.test.js +11 -0
- package/dist/clis/bilibili/comments.js +2 -2
- package/dist/clis/bilibili/comments.test.js +3 -2
- package/dist/clis/bilibili/download.js +2 -1
- package/dist/clis/bilibili/subtitle.js +4 -3
- package/dist/clis/bilibili/subtitle.test.js +2 -1
- package/dist/clis/bilibili/utils.d.ts +5 -0
- package/dist/clis/bilibili/utils.js +30 -0
- package/dist/clis/bilibili/utils.test.d.ts +1 -0
- package/dist/clis/bilibili/utils.test.js +17 -0
- package/dist/clis/douban/marks.js +1 -1
- package/dist/clis/douban/subject.yaml +50 -19
- package/dist/clis/doubao/utils.js +32 -12
- package/dist/clis/douyin/_shared/browser-fetch.test.js +0 -1
- package/dist/clis/douyin/_shared/transcode.test.js +0 -2
- package/dist/clis/douyin/draft.test.js +0 -2
- package/dist/clis/facebook/search.test.js +0 -2
- package/dist/clis/gemini/ask.js +9 -3
- package/dist/clis/gemini/ask.test.d.ts +1 -0
- package/dist/clis/gemini/ask.test.js +100 -0
- package/dist/clis/gemini/reply-state.test.d.ts +1 -0
- package/dist/clis/gemini/reply-state.test.js +641 -0
- package/dist/clis/gemini/utils.d.ts +44 -1
- package/dist/clis/gemini/utils.js +528 -61
- package/dist/clis/gemini/utils.test.js +149 -2
- package/dist/clis/hupu/detail.d.ts +1 -0
- package/dist/clis/hupu/detail.js +72 -0
- package/dist/clis/hupu/hot.yaml +43 -0
- package/dist/clis/hupu/like.d.ts +1 -0
- package/dist/clis/hupu/like.js +75 -0
- package/dist/clis/hupu/reply.d.ts +1 -0
- package/dist/clis/hupu/reply.js +71 -0
- package/dist/clis/hupu/search.d.ts +1 -0
- package/dist/clis/hupu/search.js +59 -0
- package/dist/clis/hupu/unlike.d.ts +1 -0
- package/dist/clis/hupu/unlike.js +75 -0
- package/dist/clis/hupu/utils.d.ts +20 -0
- package/dist/clis/hupu/utils.js +319 -0
- package/dist/clis/instagram/_shared/private-publish.d.ts +138 -0
- package/dist/clis/instagram/_shared/private-publish.js +1030 -0
- package/dist/clis/instagram/_shared/private-publish.test.d.ts +1 -0
- package/dist/clis/instagram/_shared/private-publish.test.js +705 -0
- package/dist/clis/instagram/_shared/protocol-capture.d.ts +26 -0
- package/dist/clis/instagram/_shared/protocol-capture.js +282 -0
- package/dist/clis/instagram/_shared/protocol-capture.test.d.ts +1 -0
- package/dist/clis/instagram/_shared/protocol-capture.test.js +114 -0
- package/dist/clis/instagram/_shared/runtime-info.d.ts +9 -0
- package/dist/clis/instagram/_shared/runtime-info.js +81 -0
- package/dist/clis/instagram/note.d.ts +1 -0
- package/dist/clis/instagram/note.js +222 -0
- package/dist/clis/instagram/note.test.d.ts +1 -0
- package/dist/clis/instagram/note.test.js +81 -0
- package/dist/clis/instagram/post.d.ts +4 -0
- package/dist/clis/instagram/post.js +1496 -0
- package/dist/clis/instagram/post.test.d.ts +1 -0
- package/dist/clis/instagram/post.test.js +1647 -0
- package/dist/clis/instagram/reel.d.ts +1 -0
- package/dist/clis/instagram/reel.js +826 -0
- package/dist/clis/instagram/reel.test.d.ts +1 -0
- package/dist/clis/instagram/reel.test.js +167 -0
- package/dist/clis/instagram/story.d.ts +1 -0
- package/dist/clis/instagram/story.js +115 -0
- package/dist/clis/instagram/story.test.d.ts +1 -0
- package/dist/clis/instagram/story.test.js +167 -0
- package/dist/clis/sinafinance/stock-rank.d.ts +4 -0
- package/dist/clis/sinafinance/stock-rank.js +65 -0
- package/dist/clis/substack/utils.test.js +0 -2
- package/dist/clis/twitter/post.js +72 -45
- package/dist/clis/twitter/post.test.d.ts +1 -0
- package/dist/clis/twitter/post.test.js +116 -0
- package/dist/clis/twitter/reply.d.ts +12 -0
- package/dist/clis/twitter/reply.js +257 -35
- package/dist/clis/twitter/reply.test.d.ts +1 -0
- package/dist/clis/twitter/reply.test.js +151 -0
- package/dist/clis/xianyu/chat.d.ts +7 -0
- package/dist/clis/xianyu/chat.js +146 -0
- package/dist/clis/xianyu/chat.test.d.ts +1 -0
- package/dist/clis/xianyu/chat.test.js +15 -0
- package/dist/clis/xianyu/item.d.ts +7 -0
- package/dist/clis/xianyu/item.js +152 -0
- package/dist/clis/xianyu/item.test.d.ts +1 -0
- package/dist/clis/xianyu/item.test.js +56 -0
- package/dist/clis/xianyu/search.d.ts +10 -0
- package/dist/clis/xianyu/search.js +134 -0
- package/dist/clis/xianyu/search.test.d.ts +1 -0
- package/dist/clis/xianyu/search.test.js +17 -0
- package/dist/clis/xianyu/utils.d.ts +1 -0
- package/dist/clis/xianyu/utils.js +8 -0
- package/dist/clis/xiaoe/catalog.yaml +129 -0
- package/dist/clis/xiaoe/content.yaml +43 -0
- package/dist/clis/xiaoe/courses.yaml +73 -0
- package/dist/clis/xiaoe/detail.yaml +39 -0
- package/dist/clis/xiaoe/play-url.yaml +124 -0
- package/dist/clis/xiaohongshu/comments.test.js +0 -2
- package/dist/clis/xiaohongshu/creator-note-detail.test.js +0 -2
- package/dist/clis/xiaohongshu/creator-notes.test.js +0 -2
- package/dist/clis/xiaohongshu/download.test.js +0 -2
- package/dist/clis/xiaohongshu/note.test.js +0 -2
- package/dist/clis/xiaohongshu/publish.test.js +0 -2
- package/dist/clis/xiaohongshu/search.js +29 -20
- package/dist/clis/xiaohongshu/search.test.js +56 -48
- package/dist/clis/yuanbao/ask.d.ts +21 -0
- package/dist/clis/yuanbao/ask.js +427 -0
- package/dist/clis/yuanbao/ask.test.d.ts +1 -0
- package/dist/clis/yuanbao/ask.test.js +124 -0
- package/dist/clis/yuanbao/new.d.ts +1 -0
- package/dist/clis/yuanbao/new.js +70 -0
- package/dist/clis/yuanbao/new.test.d.ts +1 -0
- package/dist/clis/yuanbao/new.test.js +30 -0
- package/dist/clis/yuanbao/shared.d.ts +13 -0
- package/dist/clis/yuanbao/shared.js +49 -0
- package/dist/clis/zhihu/question.js +30 -19
- package/dist/clis/zhihu/question.test.js +34 -16
- package/dist/commanderAdapter.js +8 -4
- package/dist/commanderAdapter.test.js +42 -0
- package/dist/completion.js +3 -1
- package/dist/completion.test.d.ts +1 -0
- package/dist/completion.test.js +23 -0
- package/dist/doctor.js +1 -1
- package/dist/electron-apps.d.ts +2 -0
- package/dist/electron-apps.js +7 -1
- package/dist/errors.js +1 -1
- package/dist/execution.js +25 -35
- package/dist/explore.js +1 -1
- package/dist/launcher.d.ts +4 -0
- package/dist/launcher.js +64 -8
- package/dist/launcher.test.js +88 -7
- package/dist/output.d.ts +2 -0
- package/dist/output.js +10 -1
- package/dist/output.test.d.ts +0 -3
- package/dist/output.test.js +59 -92
- package/dist/pipeline/executor.test.js +0 -2
- package/dist/pipeline/steps/download.test.js +0 -2
- package/dist/registry.d.ts +2 -0
- package/dist/serialization.d.ts +1 -0
- package/dist/serialization.js +1 -0
- package/dist/types.d.ts +9 -2
- package/docs/.vitepress/config.mts +4 -0
- package/docs/adapters/browser/1688.md +52 -0
- package/docs/adapters/browser/36kr.md +2 -1
- package/docs/adapters/browser/doubao.md +5 -1
- package/docs/adapters/browser/hupu.md +53 -0
- package/docs/adapters/browser/sinafinance.md +32 -2
- package/docs/adapters/browser/weibo.md +6 -1
- package/docs/adapters/browser/wikipedia.md +2 -0
- package/docs/adapters/browser/xianyu.md +42 -0
- package/docs/adapters/browser/xiaoe.md +44 -0
- package/docs/adapters/browser/yuanbao.md +64 -0
- package/docs/adapters/index.md +14 -5
- package/docs/comparison.md +1 -1
- package/docs/developer/ai-workflow.md +2 -2
- package/docs/developer/contributing.md +1 -1
- package/docs/developer/testing.md +2 -0
- package/docs/guide/plugins.md +1 -0
- package/docs/guide/troubleshooting.md +11 -0
- package/docs/superpowers/specs/2026-04-03-v2ex-autoresearch-design.md +41 -0
- package/docs/zh/guide/plugins.md +1 -0
- package/extension/dist/background.js +1127 -0
- package/extension/src/background.test.ts +39 -0
- package/extension/src/background.ts +223 -34
- package/extension/src/cdp.ts +194 -4
- package/extension/src/protocol.ts +22 -1
- package/package.json +3 -2
- package/scripts/postinstall.js +1 -1
- package/skills/opencli-explorer/SKILL.md +1 -1
- package/skills/opencli-oneshot/SKILL.md +2 -2
- package/skills/opencli-operate/SKILL.md +120 -27
- package/skills/opencli-usage/SKILL.md +31 -20
- package/skills/opencli-usage/browser.md +114 -16
- package/skills/opencli-usage/public-api.md +32 -3
- package/skills/smart-search/SKILL.md +156 -0
- package/skills/smart-search/references/sources-ai.md +74 -0
- package/skills/smart-search/references/sources-info.md +43 -0
- package/skills/smart-search/references/sources-media.md +50 -0
- package/skills/smart-search/references/sources-other.md +42 -0
- package/skills/smart-search/references/sources-shopping.md +31 -0
- package/skills/smart-search/references/sources-social.md +51 -0
- package/skills/smart-search/references/sources-tech.md +42 -0
- package/skills/smart-search/references/sources-travel.md +20 -0
- package/src/browser/base-page.ts +41 -6
- package/src/browser/bridge.ts +11 -8
- package/src/browser/cdp.ts +1 -8
- package/src/browser/daemon-client.ts +11 -1
- package/src/browser/dom-helpers.ts +43 -31
- package/src/browser/dom-snapshot.ts +23 -1
- package/src/browser/page.ts +115 -31
- package/src/browser.test.ts +1 -1
- package/src/build-manifest.ts +2 -0
- package/src/cli.test.ts +133 -0
- package/src/cli.ts +73 -11
- package/src/clis/1688/item.test.ts +69 -0
- package/src/clis/1688/item.ts +282 -0
- package/src/clis/1688/search.test.ts +81 -0
- package/src/clis/1688/search.ts +402 -0
- package/src/clis/1688/shared.test.ts +75 -0
- package/src/clis/1688/shared.ts +623 -0
- package/src/clis/1688/store.test.ts +69 -0
- package/src/clis/1688/store.ts +300 -0
- package/src/clis/amazon/bestsellers.test.ts +12 -3
- package/src/clis/amazon/bestsellers.ts +6 -178
- package/src/clis/amazon/movers-shakers.ts +8 -0
- package/src/clis/amazon/new-releases.ts +8 -0
- package/src/clis/amazon/rankings.test.ts +47 -0
- package/src/clis/amazon/rankings.ts +312 -0
- package/src/clis/amazon/shared.test.ts +16 -0
- package/src/clis/amazon/shared.ts +134 -12
- package/src/clis/bilibili/comments.test.ts +4 -3
- package/src/clis/bilibili/comments.ts +2 -2
- package/src/clis/bilibili/download.ts +2 -1
- package/src/clis/bilibili/subtitle.test.ts +2 -1
- package/src/clis/bilibili/subtitle.ts +4 -3
- package/src/clis/bilibili/utils.test.ts +21 -0
- package/src/clis/bilibili/utils.ts +27 -0
- package/src/clis/douban/marks.ts +1 -1
- package/src/clis/douban/subject.yaml +50 -19
- package/src/clis/doubao/utils.ts +32 -12
- package/src/clis/douyin/_shared/browser-fetch.test.ts +0 -1
- package/src/clis/douyin/_shared/transcode.test.ts +0 -2
- package/src/clis/douyin/draft.test.ts +0 -2
- package/src/clis/facebook/search.test.ts +0 -2
- package/src/clis/gemini/ask.test.ts +116 -0
- package/src/clis/gemini/ask.ts +10 -3
- package/src/clis/gemini/reply-state.test.ts +708 -0
- package/src/clis/gemini/utils.test.ts +184 -2
- package/src/clis/gemini/utils.ts +588 -60
- package/src/clis/hupu/detail.ts +126 -0
- package/src/clis/hupu/hot.yaml +43 -0
- package/src/clis/hupu/like.ts +76 -0
- package/src/clis/hupu/reply.ts +76 -0
- package/src/clis/hupu/search.ts +95 -0
- package/src/clis/hupu/unlike.ts +76 -0
- package/src/clis/hupu/utils.ts +381 -0
- package/src/clis/instagram/_shared/private-publish.test.ts +827 -0
- package/src/clis/instagram/_shared/private-publish.ts +1303 -0
- package/src/clis/instagram/_shared/protocol-capture.test.ts +148 -0
- package/src/clis/instagram/_shared/protocol-capture.ts +321 -0
- package/src/clis/instagram/_shared/runtime-info.ts +91 -0
- package/src/clis/instagram/note.test.ts +96 -0
- package/src/clis/instagram/note.ts +254 -0
- package/src/clis/instagram/post.test.ts +1716 -0
- package/src/clis/instagram/post.ts +1620 -0
- package/src/clis/instagram/reel.test.ts +191 -0
- package/src/clis/instagram/reel.ts +886 -0
- package/src/clis/instagram/story.test.ts +191 -0
- package/src/clis/instagram/story.ts +151 -0
- package/src/clis/sinafinance/stock-rank.ts +68 -0
- package/src/clis/substack/utils.test.ts +0 -2
- package/src/clis/twitter/post.test.ts +157 -0
- package/src/clis/twitter/post.ts +82 -48
- package/src/clis/twitter/reply.test.ts +177 -0
- package/src/clis/twitter/reply.ts +285 -39
- package/src/clis/xianyu/chat.test.ts +20 -0
- package/src/clis/xianyu/chat.ts +175 -0
- package/src/clis/xianyu/item.test.ts +67 -0
- package/src/clis/xianyu/item.ts +172 -0
- package/src/clis/xianyu/search.test.ts +22 -0
- package/src/clis/xianyu/search.ts +151 -0
- package/src/clis/xianyu/utils.ts +9 -0
- package/src/clis/xiaoe/catalog.yaml +129 -0
- package/src/clis/xiaoe/content.yaml +43 -0
- package/src/clis/xiaoe/courses.yaml +73 -0
- package/src/clis/xiaoe/detail.yaml +39 -0
- package/src/clis/xiaoe/play-url.yaml +124 -0
- package/src/clis/xiaohongshu/comments.test.ts +0 -2
- package/src/clis/xiaohongshu/creator-note-detail.test.ts +0 -2
- package/src/clis/xiaohongshu/creator-notes.test.ts +0 -2
- package/src/clis/xiaohongshu/download.test.ts +0 -2
- package/src/clis/xiaohongshu/note.test.ts +0 -2
- package/src/clis/xiaohongshu/publish.test.ts +0 -2
- package/src/clis/xiaohongshu/search.test.ts +59 -48
- package/src/clis/xiaohongshu/search.ts +31 -21
- package/src/clis/yuanbao/ask.test.ts +156 -0
- package/src/clis/yuanbao/ask.ts +522 -0
- package/src/clis/yuanbao/new.test.ts +36 -0
- package/src/clis/yuanbao/new.ts +81 -0
- package/src/clis/yuanbao/shared.ts +57 -0
- package/src/clis/zhihu/question.test.ts +42 -17
- package/src/clis/zhihu/question.ts +31 -26
- package/src/commanderAdapter.test.ts +51 -0
- package/src/commanderAdapter.ts +8 -4
- package/src/completion.test.ts +30 -0
- package/src/completion.ts +3 -1
- package/src/doctor.ts +1 -1
- package/src/electron-apps.ts +9 -1
- package/src/errors.ts +1 -1
- package/src/execution.ts +26 -30
- package/src/explore.ts +1 -1
- package/src/launcher.test.ts +121 -7
- package/src/launcher.ts +87 -9
- package/src/output.test.ts +50 -90
- package/src/output.ts +10 -1
- package/src/pipeline/executor.test.ts +0 -2
- package/src/pipeline/steps/download.test.ts +0 -2
- package/src/registry.ts +2 -0
- package/src/serialization.ts +2 -0
- package/src/types.ts +9 -2
- package/tests/e2e/browser-auth.test.ts +9 -0
- package/CLI-EXPLORER.md +0 -724
- package/CLI-ONESHOT.md +0 -216
- package/SKILL.md +0 -59
|
@@ -0,0 +1,886 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as os from 'node:os';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { Page as BrowserPage } from '../../browser/page.js';
|
|
6
|
+
import { cli, Strategy } from '../../registry.js';
|
|
7
|
+
import { ArgumentError, AuthRequiredError, CommandExecutionError } from '../../errors.js';
|
|
8
|
+
import type { BrowserCookie, IPage } from '../../types.js';
|
|
9
|
+
import {
|
|
10
|
+
buildClickActionJs,
|
|
11
|
+
buildEnsureComposerOpenJs,
|
|
12
|
+
buildInspectUploadStageJs,
|
|
13
|
+
} from './post.js';
|
|
14
|
+
import { resolveInstagramRuntimeInfo } from './_shared/runtime-info.js';
|
|
15
|
+
|
|
16
|
+
const INSTAGRAM_HOME_URL = 'https://www.instagram.com/';
|
|
17
|
+
const SUPPORTED_VIDEO_EXTENSIONS = new Set(['.mp4']);
|
|
18
|
+
const INSTAGRAM_REEL_TIMEOUT_SECONDS = 600;
|
|
19
|
+
|
|
20
|
+
type InstagramReelSuccessRow = {
|
|
21
|
+
status: string;
|
|
22
|
+
detail: string;
|
|
23
|
+
url: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type ReelStageState = {
|
|
27
|
+
state: 'crop' | 'edit' | 'composer' | 'failed' | 'pending';
|
|
28
|
+
detail?: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type PreparedVideoUpload = {
|
|
32
|
+
originalPath: string;
|
|
33
|
+
uploadPath: string;
|
|
34
|
+
cleanupPath?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function requirePage(page: IPage | null): IPage {
|
|
38
|
+
if (!page) throw new CommandExecutionError('Browser session required for instagram reel');
|
|
39
|
+
return page;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function gotoInstagramHome(page: IPage, forceReload = false): Promise<void> {
|
|
43
|
+
if (forceReload) {
|
|
44
|
+
await page.goto(`${INSTAGRAM_HOME_URL}?__opencli_reset=${Date.now()}`);
|
|
45
|
+
await page.wait({ time: 1 });
|
|
46
|
+
}
|
|
47
|
+
await page.goto(INSTAGRAM_HOME_URL);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function validateVideoPath(input: unknown): string {
|
|
51
|
+
const resolved = path.resolve(String(input || '').trim());
|
|
52
|
+
if (!resolved) {
|
|
53
|
+
throw new ArgumentError('Video path cannot be empty');
|
|
54
|
+
}
|
|
55
|
+
if (!fs.existsSync(resolved)) {
|
|
56
|
+
throw new ArgumentError(`Video file not found: ${resolved}`);
|
|
57
|
+
}
|
|
58
|
+
const ext = path.extname(resolved).toLowerCase();
|
|
59
|
+
if (!SUPPORTED_VIDEO_EXTENSIONS.has(ext)) {
|
|
60
|
+
throw new ArgumentError(`Unsupported video format: ${ext}`, 'Supported formats: .mp4');
|
|
61
|
+
}
|
|
62
|
+
return resolved;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function validateInstagramReelArgs(kwargs: Record<string, unknown>): void {
|
|
66
|
+
if (kwargs.video === undefined) {
|
|
67
|
+
throw new ArgumentError(
|
|
68
|
+
'Argument "video" is required.',
|
|
69
|
+
'Provide --video /path/to/file.mp4',
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function buildInstagramReelSuccessResult(url: string): InstagramReelSuccessRow[] {
|
|
75
|
+
return [{
|
|
76
|
+
status: '✅ Posted',
|
|
77
|
+
detail: 'Single reel shared successfully',
|
|
78
|
+
url,
|
|
79
|
+
}];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function isRecoverableReelSessionError(error: unknown): boolean {
|
|
83
|
+
if (!(error instanceof CommandExecutionError)) return false;
|
|
84
|
+
return error.message === 'Instagram reel upload input not found'
|
|
85
|
+
|| error.message === 'Instagram reel preview did not appear after upload'
|
|
86
|
+
|| error.message === 'Instagram reel upload failed';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function buildSafeTempVideoPath(filePath: string): string {
|
|
90
|
+
const ext = path.extname(filePath).toLowerCase() || '.mp4';
|
|
91
|
+
return path.join(os.tmpdir(), `opencli-instagram-video-real${ext}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function prepareVideoUpload(filePath: string): PreparedVideoUpload {
|
|
95
|
+
const baseName = path.basename(filePath);
|
|
96
|
+
if (/^[a-zA-Z0-9._-]+$/.test(baseName)) {
|
|
97
|
+
return { originalPath: filePath, uploadPath: filePath };
|
|
98
|
+
}
|
|
99
|
+
const uploadPath = buildSafeTempVideoPath(filePath);
|
|
100
|
+
fs.copyFileSync(filePath, uploadPath);
|
|
101
|
+
return {
|
|
102
|
+
originalPath: filePath,
|
|
103
|
+
uploadPath,
|
|
104
|
+
cleanupPath: uploadPath,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function ensureComposerOpen(page: IPage): Promise<void> {
|
|
109
|
+
const result = await page.evaluate(buildEnsureComposerOpenJs()) as { ok?: boolean; reason?: string };
|
|
110
|
+
if (!result?.ok) {
|
|
111
|
+
if (result?.reason === 'auth') {
|
|
112
|
+
throw new AuthRequiredError('www.instagram.com', 'Instagram login required before posting a reel');
|
|
113
|
+
}
|
|
114
|
+
throw new CommandExecutionError('Failed to open Instagram reel composer');
|
|
115
|
+
}
|
|
116
|
+
for (let attempt = 0; attempt < 12; attempt += 1) {
|
|
117
|
+
const ready = await page.evaluate(`
|
|
118
|
+
(() => {
|
|
119
|
+
const isVisible = (el) => {
|
|
120
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
121
|
+
const style = window.getComputedStyle(el);
|
|
122
|
+
const rect = el.getBoundingClientRect();
|
|
123
|
+
return style.display !== 'none'
|
|
124
|
+
&& style.visibility !== 'hidden'
|
|
125
|
+
&& rect.width > 0
|
|
126
|
+
&& rect.height > 0;
|
|
127
|
+
};
|
|
128
|
+
const inputs = Array.from(document.querySelectorAll('input[type="file"]'))
|
|
129
|
+
.filter((el) => el instanceof HTMLInputElement)
|
|
130
|
+
.filter((el) => {
|
|
131
|
+
const dialog = el.closest('[role="dialog"]');
|
|
132
|
+
return dialog instanceof HTMLElement && isVisible(dialog);
|
|
133
|
+
});
|
|
134
|
+
return { ok: inputs.length > 0 };
|
|
135
|
+
})()
|
|
136
|
+
`) as { ok?: boolean };
|
|
137
|
+
if (ready?.ok) return;
|
|
138
|
+
if (attempt < 11) await page.wait({ time: 0.5 });
|
|
139
|
+
}
|
|
140
|
+
throw new CommandExecutionError('Instagram reel upload input not found', 'Open the new-post composer in a logged-in browser session and retry');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function dismissResidualDialogs(page: IPage): Promise<void> {
|
|
144
|
+
for (let attempt = 0; attempt < 4; attempt += 1) {
|
|
145
|
+
const result = await page.evaluate(`
|
|
146
|
+
(() => {
|
|
147
|
+
const isVisible = (el) => {
|
|
148
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
149
|
+
const style = window.getComputedStyle(el);
|
|
150
|
+
const rect = el.getBoundingClientRect();
|
|
151
|
+
return style.display !== 'none'
|
|
152
|
+
&& style.visibility !== 'hidden'
|
|
153
|
+
&& rect.width > 0
|
|
154
|
+
&& rect.height > 0;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]'))
|
|
158
|
+
.filter((el) => el instanceof HTMLElement && isVisible(el));
|
|
159
|
+
for (const dialog of dialogs) {
|
|
160
|
+
const text = (dialog.textContent || '').replace(/\\s+/g, ' ').trim().toLowerCase();
|
|
161
|
+
if (!text) continue;
|
|
162
|
+
if (
|
|
163
|
+
text.includes('post shared')
|
|
164
|
+
|| text.includes('your post has been shared')
|
|
165
|
+
|| text.includes('your reel has been shared')
|
|
166
|
+
|| text.includes('video posts are now reels')
|
|
167
|
+
|| text.includes('something went wrong')
|
|
168
|
+
|| text.includes('sharing')
|
|
169
|
+
|| text.includes('create new post')
|
|
170
|
+
|| text.includes('new reel')
|
|
171
|
+
|| text.includes('crop')
|
|
172
|
+
|| text.includes('edit')
|
|
173
|
+
) {
|
|
174
|
+
const close = dialog.querySelector('[aria-label="Close"], button[aria-label="Close"], div[role="button"][aria-label="Close"]');
|
|
175
|
+
if (close instanceof HTMLElement && isVisible(close)) {
|
|
176
|
+
close.click();
|
|
177
|
+
return { ok: true };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return { ok: false };
|
|
182
|
+
})()
|
|
183
|
+
`) as { ok?: boolean };
|
|
184
|
+
|
|
185
|
+
if (!result?.ok) return;
|
|
186
|
+
await page.wait({ time: 0.5 });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function resolveUploadSelectors(page: IPage): Promise<string[]> {
|
|
191
|
+
const result = await page.evaluate(`
|
|
192
|
+
(() => {
|
|
193
|
+
const isVisible = (el) => {
|
|
194
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
195
|
+
const style = window.getComputedStyle(el);
|
|
196
|
+
const rect = el.getBoundingClientRect();
|
|
197
|
+
return style.display !== 'none'
|
|
198
|
+
&& style.visibility !== 'hidden'
|
|
199
|
+
&& rect.width > 0
|
|
200
|
+
&& rect.height > 0;
|
|
201
|
+
};
|
|
202
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]'))
|
|
203
|
+
.filter((el) => el instanceof HTMLElement && isVisible(el));
|
|
204
|
+
const roots = dialogs.length ? dialogs : [document.body];
|
|
205
|
+
const selectors = [];
|
|
206
|
+
let index = 0;
|
|
207
|
+
|
|
208
|
+
for (const root of roots) {
|
|
209
|
+
const inputs = Array.from(root.querySelectorAll('input[type="file"]'));
|
|
210
|
+
for (const input of inputs) {
|
|
211
|
+
if (!(input instanceof HTMLInputElement)) continue;
|
|
212
|
+
if (input.disabled) continue;
|
|
213
|
+
const accept = (input.getAttribute('accept') || '').toLowerCase();
|
|
214
|
+
if (accept && !accept.includes('video') && !accept.includes('.mp4')) continue;
|
|
215
|
+
input.setAttribute('data-opencli-reel-upload-index', String(index));
|
|
216
|
+
selectors.push('[data-opencli-reel-upload-index="' + index + '"]');
|
|
217
|
+
index += 1;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return { ok: selectors.length > 0, selectors };
|
|
222
|
+
})()
|
|
223
|
+
`) as { ok?: boolean; selectors?: string[] };
|
|
224
|
+
|
|
225
|
+
if (!result?.ok || !Array.isArray(result.selectors) || result.selectors.length === 0) {
|
|
226
|
+
throw new CommandExecutionError(
|
|
227
|
+
'Instagram reel upload input not found',
|
|
228
|
+
'Open the new-post composer in a logged-in browser session and retry',
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
return result.selectors;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function uploadVideo(page: IPage, videoPath: string, selector: string): Promise<void> {
|
|
235
|
+
if (!page.setFileInput) {
|
|
236
|
+
throw new CommandExecutionError(
|
|
237
|
+
'Instagram reel upload requires Browser Bridge file upload support',
|
|
238
|
+
'Use Browser Bridge or another browser mode that supports setFileInput',
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
await page.setFileInput([videoPath], selector);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function readSelectedFileCount(page: IPage, selector: string): Promise<number | null> {
|
|
245
|
+
const result = await page.evaluate(`
|
|
246
|
+
(() => {
|
|
247
|
+
const input = document.querySelector(${JSON.stringify(selector)});
|
|
248
|
+
if (!(input instanceof HTMLInputElement)) return { count: null };
|
|
249
|
+
return { count: input.files?.length || 0 };
|
|
250
|
+
})()
|
|
251
|
+
`) as { count?: number | null };
|
|
252
|
+
if (result?.count === null || result?.count === undefined) return null;
|
|
253
|
+
return Number(result.count);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async function waitForVideoPreview(page: IPage, maxWaitSeconds = 20): Promise<void> {
|
|
257
|
+
let lastDetail = '';
|
|
258
|
+
for (let attempt = 0; attempt < maxWaitSeconds * 2; attempt += 1) {
|
|
259
|
+
const result = await page.evaluate(buildInspectUploadStageJs()) as { state?: string; detail?: string };
|
|
260
|
+
lastDetail = String(result?.detail || '').trim();
|
|
261
|
+
if (result?.state === 'preview') return;
|
|
262
|
+
if (result?.state === 'failed') {
|
|
263
|
+
throw new CommandExecutionError(
|
|
264
|
+
'Instagram reel upload failed',
|
|
265
|
+
result.detail ? `Instagram rejected the reel upload: ${result.detail}` : 'Instagram rejected the reel upload before the preview stage',
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
if (attempt < maxWaitSeconds * 2 - 1) await page.wait({ time: 0.5 });
|
|
269
|
+
}
|
|
270
|
+
await page.screenshot({ path: '/tmp/instagram_reel_preview_debug.png' });
|
|
271
|
+
throw new CommandExecutionError(
|
|
272
|
+
'Instagram reel preview did not appear after upload',
|
|
273
|
+
lastDetail
|
|
274
|
+
? `Inspect /tmp/instagram_reel_preview_debug.png. Last visible dialog text: ${lastDetail}`
|
|
275
|
+
: 'Inspect /tmp/instagram_reel_preview_debug.png for the upload state',
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function clickAction(page: IPage, labels: string[], scope: 'any' | 'media' | 'caption' = 'any'): Promise<string> {
|
|
280
|
+
const result = await page.evaluate(buildClickActionJs(labels, scope)) as { ok?: boolean; label?: string };
|
|
281
|
+
if (!result?.ok) {
|
|
282
|
+
throw new CommandExecutionError(`Instagram action button not found: ${labels.join(' / ')}`);
|
|
283
|
+
}
|
|
284
|
+
return result.label || labels[0] || '';
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async function clickActionMaybe(page: IPage, labels: string[], scope: 'any' | 'media' | 'caption' = 'any'): Promise<boolean> {
|
|
288
|
+
const result = await page.evaluate(buildClickActionJs(labels, scope)) as { ok?: boolean };
|
|
289
|
+
return !!result?.ok;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function buildInspectReelStageJs(): string {
|
|
293
|
+
return `
|
|
294
|
+
(() => {
|
|
295
|
+
const isVisible = (el) => {
|
|
296
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
297
|
+
const style = window.getComputedStyle(el);
|
|
298
|
+
const rect = el.getBoundingClientRect();
|
|
299
|
+
return style.display !== 'none'
|
|
300
|
+
&& style.visibility !== 'hidden'
|
|
301
|
+
&& rect.width > 0
|
|
302
|
+
&& rect.height > 0;
|
|
303
|
+
};
|
|
304
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]')).filter((el) => isVisible(el));
|
|
305
|
+
const text = dialogs.map((el) => (el.textContent || '').replace(/\\s+/g, ' ').trim()).join(' ');
|
|
306
|
+
const lower = text.toLowerCase();
|
|
307
|
+
const hasVisibleButton = (labels) => dialogs.some((dialog) =>
|
|
308
|
+
Array.from(dialog.querySelectorAll('button, div[role="button"]')).some((el) => {
|
|
309
|
+
const value = (el.textContent || '').replace(/\\s+/g, ' ').trim().toLowerCase();
|
|
310
|
+
return isVisible(el) && labels.includes(value);
|
|
311
|
+
})
|
|
312
|
+
);
|
|
313
|
+
if (/something went wrong|please try again|share failed|couldn['’]t be shared|could not be shared|失败|出错/.test(lower)) {
|
|
314
|
+
return { state: 'failed', detail: text };
|
|
315
|
+
}
|
|
316
|
+
if (/new reel|write a caption|add location|tag people/.test(lower) && hasVisibleButton(['share'])) {
|
|
317
|
+
return { state: 'composer', detail: text };
|
|
318
|
+
}
|
|
319
|
+
if (/edit|cover photo|trim|video has no audio/.test(lower) && hasVisibleButton(['next'])) {
|
|
320
|
+
return { state: 'edit', detail: text };
|
|
321
|
+
}
|
|
322
|
+
if (/crop|select crop|open media gallery/.test(lower) && hasVisibleButton(['next'])) {
|
|
323
|
+
return { state: 'crop', detail: text };
|
|
324
|
+
}
|
|
325
|
+
return { state: 'pending', detail: text };
|
|
326
|
+
})()
|
|
327
|
+
`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function waitForReelStage(page: IPage, expected: ReelStageState['state'], maxWaitSeconds = 20): Promise<void> {
|
|
331
|
+
for (let attempt = 0; attempt < maxWaitSeconds * 2; attempt += 1) {
|
|
332
|
+
const result = await page.evaluate(buildInspectReelStageJs()) as ReelStageState;
|
|
333
|
+
if (result?.state === expected) return;
|
|
334
|
+
if (result?.state === 'failed') {
|
|
335
|
+
throw new CommandExecutionError(
|
|
336
|
+
'Instagram reel editor did not appear',
|
|
337
|
+
result.detail ? `Instagram reel flow failed: ${result.detail}` : 'Instagram reel flow failed before the next editor stage',
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
if (attempt < maxWaitSeconds * 2 - 1) await page.wait({ time: 0.5 });
|
|
341
|
+
}
|
|
342
|
+
throw new CommandExecutionError(`Instagram reel ${expected} editor did not appear`);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function focusCaptionEditor(page: IPage): Promise<boolean> {
|
|
346
|
+
const result = await page.evaluate(`
|
|
347
|
+
(() => {
|
|
348
|
+
const isVisible = (el) => {
|
|
349
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
350
|
+
const style = window.getComputedStyle(el);
|
|
351
|
+
const rect = el.getBoundingClientRect();
|
|
352
|
+
return style.display !== 'none'
|
|
353
|
+
&& style.visibility !== 'hidden'
|
|
354
|
+
&& rect.width > 0
|
|
355
|
+
&& rect.height > 0;
|
|
356
|
+
};
|
|
357
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]')).filter((el) => isVisible(el));
|
|
358
|
+
for (const dialog of dialogs) {
|
|
359
|
+
const textarea = dialog.querySelector('[aria-label="Write a caption..."], textarea');
|
|
360
|
+
if (textarea instanceof HTMLTextAreaElement && isVisible(textarea)) {
|
|
361
|
+
textarea.focus();
|
|
362
|
+
textarea.select();
|
|
363
|
+
return { ok: true, kind: 'textarea' };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const editor = dialog.querySelector('[aria-label="Write a caption..."][contenteditable="true"]')
|
|
367
|
+
|| dialog.querySelector('[contenteditable="true"]');
|
|
368
|
+
if (editor instanceof HTMLElement && isVisible(editor)) {
|
|
369
|
+
const lexical = editor.__lexicalEditor;
|
|
370
|
+
try {
|
|
371
|
+
if (lexical && typeof lexical.getEditorState === 'function' && typeof lexical.parseEditorState === 'function') {
|
|
372
|
+
const emptyState = {
|
|
373
|
+
root: {
|
|
374
|
+
children: [{
|
|
375
|
+
children: [],
|
|
376
|
+
direction: null,
|
|
377
|
+
format: '',
|
|
378
|
+
indent: 0,
|
|
379
|
+
textFormat: 0,
|
|
380
|
+
textStyle: '',
|
|
381
|
+
type: 'paragraph',
|
|
382
|
+
version: 1,
|
|
383
|
+
}],
|
|
384
|
+
direction: null,
|
|
385
|
+
format: '',
|
|
386
|
+
indent: 0,
|
|
387
|
+
type: 'root',
|
|
388
|
+
version: 1,
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
const nextState = lexical.parseEditorState(JSON.stringify(emptyState));
|
|
392
|
+
try {
|
|
393
|
+
lexical.setEditorState(nextState, { tag: 'history-merge', discrete: true });
|
|
394
|
+
} catch {
|
|
395
|
+
lexical.setEditorState(nextState);
|
|
396
|
+
}
|
|
397
|
+
} else {
|
|
398
|
+
editor.textContent = '';
|
|
399
|
+
}
|
|
400
|
+
} catch {
|
|
401
|
+
editor.textContent = '';
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
editor.focus();
|
|
405
|
+
const selection = window.getSelection();
|
|
406
|
+
if (selection) {
|
|
407
|
+
selection.removeAllRanges();
|
|
408
|
+
const range = document.createRange();
|
|
409
|
+
range.selectNodeContents(editor);
|
|
410
|
+
range.collapse(false);
|
|
411
|
+
selection.addRange(range);
|
|
412
|
+
}
|
|
413
|
+
return { ok: true, kind: 'contenteditable' };
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return { ok: false };
|
|
417
|
+
})()
|
|
418
|
+
`) as { ok?: boolean };
|
|
419
|
+
return !!result?.ok;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async function captionMatches(page: IPage, content: string): Promise<boolean> {
|
|
423
|
+
const result = await page.evaluate(`
|
|
424
|
+
(() => {
|
|
425
|
+
const target = ${JSON.stringify(content.trim())}.replace(/\\u00a0/g, ' ').replace(/\\s+/g, ' ').trim();
|
|
426
|
+
const isVisible = (el) => {
|
|
427
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
428
|
+
const style = window.getComputedStyle(el);
|
|
429
|
+
const rect = el.getBoundingClientRect();
|
|
430
|
+
return style.display !== 'none'
|
|
431
|
+
&& style.visibility !== 'hidden'
|
|
432
|
+
&& rect.width > 0
|
|
433
|
+
&& rect.height > 0;
|
|
434
|
+
};
|
|
435
|
+
const readLexicalText = (node) => {
|
|
436
|
+
if (!node || typeof node !== 'object') return '';
|
|
437
|
+
if (node.type === 'text' && typeof node.text === 'string') return node.text;
|
|
438
|
+
if (!Array.isArray(node.children)) return '';
|
|
439
|
+
if (node.type === 'root') return node.children.map((child) => readLexicalText(child)).join('\\n');
|
|
440
|
+
if (node.type === 'paragraph') return node.children.map((child) => readLexicalText(child)).join('');
|
|
441
|
+
return node.children.map((child) => readLexicalText(child)).join('');
|
|
442
|
+
};
|
|
443
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]')).filter((el) => isVisible(el));
|
|
444
|
+
for (const dialog of dialogs) {
|
|
445
|
+
const textarea = dialog.querySelector('[aria-label="Write a caption..."], textarea');
|
|
446
|
+
if (textarea instanceof HTMLTextAreaElement && isVisible(textarea)) {
|
|
447
|
+
if (textarea.value.replace(/\\u00a0/g, ' ').replace(/\\s+/g, ' ').trim() === target) {
|
|
448
|
+
return { ok: true };
|
|
449
|
+
}
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const editor = dialog.querySelector('[aria-label="Write a caption..."][contenteditable="true"]')
|
|
454
|
+
|| dialog.querySelector('[contenteditable="true"]');
|
|
455
|
+
if (!(editor instanceof HTMLElement) || !isVisible(editor)) continue;
|
|
456
|
+
|
|
457
|
+
const lexical = editor.__lexicalEditor;
|
|
458
|
+
if (lexical && typeof lexical.getEditorState === 'function') {
|
|
459
|
+
const currentState = lexical.getEditorState();
|
|
460
|
+
const pendingState = lexical._pendingEditorState;
|
|
461
|
+
const current = currentState && typeof currentState.toJSON === 'function' ? currentState.toJSON() : null;
|
|
462
|
+
const pending = pendingState && typeof pendingState.toJSON === 'function' ? pendingState.toJSON() : null;
|
|
463
|
+
const currentText = readLexicalText(current && current.root).replace(/\\u00a0/g, ' ').replace(/\\s+/g, ' ').trim();
|
|
464
|
+
const pendingText = readLexicalText(pending && pending.root).replace(/\\u00a0/g, ' ').replace(/\\s+/g, ' ').trim();
|
|
465
|
+
if (currentText === target || pendingText === target) {
|
|
466
|
+
return { ok: true };
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const value = (editor.textContent || '').replace(/\\u00a0/g, ' ').replace(/\\s+/g, ' ').trim();
|
|
471
|
+
if (value === target) {
|
|
472
|
+
return { ok: true };
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return { ok: false };
|
|
476
|
+
})()
|
|
477
|
+
`) as { ok?: boolean };
|
|
478
|
+
return !!result?.ok;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
async function fillCaption(page: IPage, content: string): Promise<void> {
|
|
482
|
+
const focused = await focusCaptionEditor(page);
|
|
483
|
+
if (!focused) {
|
|
484
|
+
throw new CommandExecutionError('Instagram reel caption editor did not appear');
|
|
485
|
+
}
|
|
486
|
+
if (page.insertText) {
|
|
487
|
+
try {
|
|
488
|
+
await page.insertText(content);
|
|
489
|
+
await page.wait({ time: 0.3 });
|
|
490
|
+
await page.evaluate(`
|
|
491
|
+
(() => {
|
|
492
|
+
const isVisible = (el) => {
|
|
493
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
494
|
+
const style = window.getComputedStyle(el);
|
|
495
|
+
const rect = el.getBoundingClientRect();
|
|
496
|
+
return style.display !== 'none'
|
|
497
|
+
&& style.visibility !== 'hidden'
|
|
498
|
+
&& rect.width > 0
|
|
499
|
+
&& rect.height > 0;
|
|
500
|
+
};
|
|
501
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]')).filter((el) => isVisible(el));
|
|
502
|
+
for (const dialog of dialogs) {
|
|
503
|
+
const textarea = dialog.querySelector('[aria-label="Write a caption..."], textarea');
|
|
504
|
+
if (textarea instanceof HTMLTextAreaElement && isVisible(textarea)) {
|
|
505
|
+
textarea.dispatchEvent(new InputEvent('input', { bubbles: true, composed: true, inputType: 'insertText' }));
|
|
506
|
+
textarea.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
|
507
|
+
textarea.blur();
|
|
508
|
+
return { ok: true };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const editor = dialog.querySelector('[aria-label="Write a caption..."][contenteditable="true"]')
|
|
512
|
+
|| dialog.querySelector('[contenteditable="true"]');
|
|
513
|
+
if (!(editor instanceof HTMLElement) || !isVisible(editor)) continue;
|
|
514
|
+
try {
|
|
515
|
+
editor.dispatchEvent(new InputEvent('input', { bubbles: true, composed: true, inputType: 'insertText' }));
|
|
516
|
+
} catch {
|
|
517
|
+
editor.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
|
|
518
|
+
}
|
|
519
|
+
editor.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
|
520
|
+
editor.blur();
|
|
521
|
+
return { ok: true };
|
|
522
|
+
}
|
|
523
|
+
return { ok: false };
|
|
524
|
+
})()
|
|
525
|
+
`);
|
|
526
|
+
return;
|
|
527
|
+
} catch {
|
|
528
|
+
// Fall back to browser-side editor manipulation below.
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
await page.evaluate(`
|
|
532
|
+
((content) => {
|
|
533
|
+
const createParagraph = (text) => ({
|
|
534
|
+
children: text
|
|
535
|
+
? [{ detail: 0, format: 0, mode: 'normal', style: '', text, type: 'text', version: 1 }]
|
|
536
|
+
: [],
|
|
537
|
+
direction: null,
|
|
538
|
+
format: '',
|
|
539
|
+
indent: 0,
|
|
540
|
+
textFormat: 0,
|
|
541
|
+
textStyle: '',
|
|
542
|
+
type: 'paragraph',
|
|
543
|
+
version: 1,
|
|
544
|
+
});
|
|
545
|
+
const isVisible = (el) => {
|
|
546
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
547
|
+
const style = window.getComputedStyle(el);
|
|
548
|
+
const rect = el.getBoundingClientRect();
|
|
549
|
+
return style.display !== 'none'
|
|
550
|
+
&& style.visibility !== 'hidden'
|
|
551
|
+
&& rect.width > 0
|
|
552
|
+
&& rect.height > 0;
|
|
553
|
+
};
|
|
554
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]')).filter((el) => isVisible(el));
|
|
555
|
+
for (const dialog of dialogs) {
|
|
556
|
+
const textarea = dialog.querySelector('[aria-label="Write a caption..."], textarea');
|
|
557
|
+
if (textarea instanceof HTMLTextAreaElement && isVisible(textarea)) {
|
|
558
|
+
textarea.focus();
|
|
559
|
+
const dt = new DataTransfer();
|
|
560
|
+
dt.setData('text/plain', content);
|
|
561
|
+
textarea.dispatchEvent(new ClipboardEvent('paste', {
|
|
562
|
+
clipboardData: dt,
|
|
563
|
+
bubbles: true,
|
|
564
|
+
cancelable: true,
|
|
565
|
+
}));
|
|
566
|
+
const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')?.set;
|
|
567
|
+
setter?.call(textarea, content);
|
|
568
|
+
textarea.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
|
|
569
|
+
textarea.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
|
570
|
+
textarea.blur();
|
|
571
|
+
return { ok: true, mode: 'textarea' };
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const editor = dialog.querySelector('[aria-label="Write a caption..."][contenteditable="true"]')
|
|
575
|
+
|| dialog.querySelector('[contenteditable="true"]');
|
|
576
|
+
if (!(editor instanceof HTMLElement) || !isVisible(editor)) continue;
|
|
577
|
+
|
|
578
|
+
editor.focus();
|
|
579
|
+
const lexical = editor.__lexicalEditor;
|
|
580
|
+
if (lexical && typeof lexical.getEditorState === 'function' && typeof lexical.parseEditorState === 'function') {
|
|
581
|
+
const currentState = lexical.getEditorState && lexical.getEditorState();
|
|
582
|
+
const base = currentState && typeof currentState.toJSON === 'function' ? currentState.toJSON() : {};
|
|
583
|
+
const lines = String(content).split(/\\r?\\n/);
|
|
584
|
+
const paragraphs = lines.map((line) => createParagraph(line));
|
|
585
|
+
base.root = {
|
|
586
|
+
children: paragraphs.length ? paragraphs : [createParagraph('')],
|
|
587
|
+
direction: null,
|
|
588
|
+
format: '',
|
|
589
|
+
indent: 0,
|
|
590
|
+
type: 'root',
|
|
591
|
+
version: 1,
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
const nextState = lexical.parseEditorState(JSON.stringify(base));
|
|
595
|
+
try {
|
|
596
|
+
lexical.setEditorState(nextState, { tag: 'history-merge', discrete: true });
|
|
597
|
+
} catch {
|
|
598
|
+
lexical.setEditorState(nextState);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
editor.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
|
|
602
|
+
editor.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
|
603
|
+
editor.blur();
|
|
604
|
+
return { ok: true, mode: 'lexical' };
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const selection = window.getSelection();
|
|
608
|
+
if (selection) {
|
|
609
|
+
selection.removeAllRanges();
|
|
610
|
+
const range = document.createRange();
|
|
611
|
+
range.selectNodeContents(editor);
|
|
612
|
+
selection.addRange(range);
|
|
613
|
+
}
|
|
614
|
+
const dt = new DataTransfer();
|
|
615
|
+
dt.setData('text/plain', content);
|
|
616
|
+
editor.dispatchEvent(new ClipboardEvent('paste', {
|
|
617
|
+
clipboardData: dt,
|
|
618
|
+
bubbles: true,
|
|
619
|
+
cancelable: true,
|
|
620
|
+
}));
|
|
621
|
+
editor.blur();
|
|
622
|
+
return { ok: true, mode: 'contenteditable' };
|
|
623
|
+
}
|
|
624
|
+
return { ok: false };
|
|
625
|
+
})(${JSON.stringify(content)})
|
|
626
|
+
`);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
async function ensureCaptionFilled(page: IPage, content: string): Promise<void> {
|
|
630
|
+
for (let attempt = 0; attempt < 6; attempt += 1) {
|
|
631
|
+
if (await captionMatches(page, content)) return;
|
|
632
|
+
if (attempt < 5) await page.wait({ time: 0.5 });
|
|
633
|
+
}
|
|
634
|
+
throw new CommandExecutionError('Instagram reel caption did not stick before sharing');
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function buildReelPublishStatusProbeJs(): string {
|
|
638
|
+
return `
|
|
639
|
+
(() => {
|
|
640
|
+
const isVisible = (el) => {
|
|
641
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
642
|
+
const style = window.getComputedStyle(el);
|
|
643
|
+
const rect = el.getBoundingClientRect();
|
|
644
|
+
return style.display !== 'none'
|
|
645
|
+
&& style.visibility !== 'hidden'
|
|
646
|
+
&& rect.width > 0
|
|
647
|
+
&& rect.height > 0;
|
|
648
|
+
};
|
|
649
|
+
const dialogs = Array.from(document.querySelectorAll('[role="dialog"]')).filter((el) => isVisible(el));
|
|
650
|
+
const dialogText = dialogs.map((el) => (el.textContent || '').replace(/\\s+/g, ' ').trim()).join(' ');
|
|
651
|
+
const lower = dialogText.toLowerCase();
|
|
652
|
+
const url = window.location.href;
|
|
653
|
+
const sharingVisible = /sharing/.test(lower);
|
|
654
|
+
const shared = /your reel has been shared|reel shared|已分享|已发布/.test(lower) || /\\/reel\\//.test(url);
|
|
655
|
+
const failed = !shared && !sharingVisible && (
|
|
656
|
+
/couldn['’]t be shared|could not be shared|share failed|无法分享|分享失败/.test(lower)
|
|
657
|
+
|| (/something went wrong/.test(lower) && /try again/.test(lower))
|
|
658
|
+
);
|
|
659
|
+
const composerOpen = dialogs.some((dialog) =>
|
|
660
|
+
!!dialog.querySelector('textarea, [contenteditable="true"], input[type="file"]')
|
|
661
|
+
|| /new reel|cover photo|trim|select from computer|crop|sharing/.test((dialog.textContent || '').toLowerCase())
|
|
662
|
+
);
|
|
663
|
+
const settled = !shared && !composerOpen && !sharingVisible;
|
|
664
|
+
return { ok: shared, failed, settled, url: /\\/reel\\//.test(url) ? url : '' };
|
|
665
|
+
})()
|
|
666
|
+
`;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
async function waitForPublishSuccess(page: IPage): Promise<string> {
|
|
670
|
+
let settledStreak = 0;
|
|
671
|
+
for (let attempt = 0; attempt < 120; attempt += 1) {
|
|
672
|
+
const result = await page.evaluate(buildReelPublishStatusProbeJs()) as { ok?: boolean; failed?: boolean; settled?: boolean; url?: string };
|
|
673
|
+
if (result?.failed) {
|
|
674
|
+
throw new CommandExecutionError('Instagram reel share failed');
|
|
675
|
+
}
|
|
676
|
+
if (result?.ok) {
|
|
677
|
+
return result.url || '';
|
|
678
|
+
}
|
|
679
|
+
if (result?.settled) {
|
|
680
|
+
settledStreak += 1;
|
|
681
|
+
if (settledStreak >= 3) return '';
|
|
682
|
+
} else {
|
|
683
|
+
settledStreak = 0;
|
|
684
|
+
}
|
|
685
|
+
if (attempt < 119) await page.wait({ time: 1 });
|
|
686
|
+
}
|
|
687
|
+
throw new CommandExecutionError('Instagram reel share confirmation did not appear');
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
async function resolveCurrentUserId(page: IPage): Promise<string> {
|
|
691
|
+
const cookies = await page.getCookies({ domain: 'instagram.com' });
|
|
692
|
+
return cookies.find((cookie: BrowserCookie) => cookie.name === 'ds_user_id')?.value || '';
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
async function resolveProfileUrl(page: IPage, currentUserId = ''): Promise<string> {
|
|
696
|
+
if (currentUserId) {
|
|
697
|
+
const runtimeInfo = await resolveInstagramRuntimeInfo(page);
|
|
698
|
+
const apiResult = await page.evaluate(`
|
|
699
|
+
(async () => {
|
|
700
|
+
const userId = ${JSON.stringify(currentUserId)};
|
|
701
|
+
const appId = ${JSON.stringify(runtimeInfo.appId || '')};
|
|
702
|
+
try {
|
|
703
|
+
const res = await fetch(
|
|
704
|
+
'https://www.instagram.com/api/v1/users/' + encodeURIComponent(userId) + '/info/',
|
|
705
|
+
{
|
|
706
|
+
credentials: 'include',
|
|
707
|
+
headers: appId ? { 'X-IG-App-ID': appId } : {},
|
|
708
|
+
},
|
|
709
|
+
);
|
|
710
|
+
if (!res.ok) return { ok: false };
|
|
711
|
+
const data = await res.json();
|
|
712
|
+
const username = data?.user?.username || '';
|
|
713
|
+
return { ok: !!username, username };
|
|
714
|
+
} catch {
|
|
715
|
+
return { ok: false };
|
|
716
|
+
}
|
|
717
|
+
})()
|
|
718
|
+
`) as { ok?: boolean; username?: string };
|
|
719
|
+
|
|
720
|
+
if (apiResult?.ok && apiResult.username) {
|
|
721
|
+
return new URL(`/${apiResult.username}/`, INSTAGRAM_HOME_URL).toString();
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return '';
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
async function collectVisibleProfileMediaPaths(page: IPage): Promise<string[]> {
|
|
728
|
+
const result = await page.evaluate(`
|
|
729
|
+
(() => {
|
|
730
|
+
const isVisible = (el) => {
|
|
731
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
732
|
+
const style = window.getComputedStyle(el);
|
|
733
|
+
const rect = el.getBoundingClientRect();
|
|
734
|
+
return style.display !== 'none'
|
|
735
|
+
&& style.visibility !== 'hidden'
|
|
736
|
+
&& rect.width > 0
|
|
737
|
+
&& rect.height > 0;
|
|
738
|
+
};
|
|
739
|
+
const hrefs = Array.from(document.querySelectorAll('a[href*="/reel/"], a[href*="/p/"]'))
|
|
740
|
+
.filter((el) => el instanceof HTMLAnchorElement && isVisible(el))
|
|
741
|
+
.map((el) => el.getAttribute('href') || '')
|
|
742
|
+
.filter((href) => /^\\/(?:[^/?#]+\\/)?(?:reel|p)\\/[^/?#]+\\/?$/.test(href))
|
|
743
|
+
.filter((href, index, arr) => arr.indexOf(href) === index);
|
|
744
|
+
return { hrefs };
|
|
745
|
+
})()
|
|
746
|
+
`) as { hrefs?: string[] };
|
|
747
|
+
return Array.isArray(result?.hrefs) ? result.hrefs.filter(Boolean) : [];
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
async function captureExistingProfileMediaPaths(page: IPage): Promise<Set<string>> {
|
|
751
|
+
const currentUserId = await resolveCurrentUserId(page);
|
|
752
|
+
if (!currentUserId) return new Set();
|
|
753
|
+
const profileUrl = await resolveProfileUrl(page, currentUserId);
|
|
754
|
+
if (!profileUrl) return new Set();
|
|
755
|
+
try {
|
|
756
|
+
await page.goto(profileUrl);
|
|
757
|
+
await page.wait({ time: 3 });
|
|
758
|
+
return new Set(await collectVisibleProfileMediaPaths(page));
|
|
759
|
+
} catch {
|
|
760
|
+
return new Set();
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
async function resolveLatestReelUrl(page: IPage, existingPaths: ReadonlySet<string>): Promise<string> {
|
|
765
|
+
const currentUrl = await page.getCurrentUrl?.();
|
|
766
|
+
if (currentUrl && /\/reel\//.test(currentUrl)) return currentUrl;
|
|
767
|
+
|
|
768
|
+
const currentUserId = await resolveCurrentUserId(page);
|
|
769
|
+
const profileUrl = await resolveProfileUrl(page, currentUserId);
|
|
770
|
+
if (!profileUrl) return '';
|
|
771
|
+
|
|
772
|
+
await page.goto(profileUrl);
|
|
773
|
+
await page.wait({ time: 4 });
|
|
774
|
+
|
|
775
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
776
|
+
const hrefs = await collectVisibleProfileMediaPaths(page);
|
|
777
|
+
const href = hrefs.find((candidate) => candidate.includes('/reel/') && !existingPaths.has(candidate))
|
|
778
|
+
|| hrefs.find((candidate) => !existingPaths.has(candidate))
|
|
779
|
+
|| '';
|
|
780
|
+
if (href) {
|
|
781
|
+
return new URL(href, INSTAGRAM_HOME_URL).toString();
|
|
782
|
+
}
|
|
783
|
+
if (attempt < 7) await page.wait({ time: 1 });
|
|
784
|
+
}
|
|
785
|
+
return '';
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
cli({
|
|
789
|
+
site: 'instagram',
|
|
790
|
+
name: 'reel',
|
|
791
|
+
description: 'Post an Instagram reel video',
|
|
792
|
+
domain: 'www.instagram.com',
|
|
793
|
+
strategy: Strategy.UI,
|
|
794
|
+
browser: true,
|
|
795
|
+
timeoutSeconds: INSTAGRAM_REEL_TIMEOUT_SECONDS,
|
|
796
|
+
args: [
|
|
797
|
+
{ name: 'video', required: false, valueRequired: true, help: 'Path to a single .mp4 video file' },
|
|
798
|
+
{ name: 'content', positional: true, required: false, help: 'Caption text' },
|
|
799
|
+
],
|
|
800
|
+
columns: ['status', 'detail', 'url'],
|
|
801
|
+
validateArgs: validateInstagramReelArgs,
|
|
802
|
+
func: async (page: IPage | null, kwargs) => {
|
|
803
|
+
const browserPage = requirePage(page);
|
|
804
|
+
const videoPath = validateVideoPath(kwargs.video);
|
|
805
|
+
const content = String(kwargs.content ?? '').trim();
|
|
806
|
+
const preparedUpload = prepareVideoUpload(videoPath);
|
|
807
|
+
|
|
808
|
+
const run = async (
|
|
809
|
+
activePage: IPage,
|
|
810
|
+
existingMediaPaths: ReadonlySet<string> = new Set(),
|
|
811
|
+
): Promise<InstagramReelSuccessRow[]> => {
|
|
812
|
+
if (typeof activePage.startNetworkCapture === 'function') {
|
|
813
|
+
await activePage.startNetworkCapture('/rupload_igvideo/|/api/v1/|/reel/|/clips/|/media/|/configure|/upload');
|
|
814
|
+
}
|
|
815
|
+
await gotoInstagramHome(activePage, true);
|
|
816
|
+
await activePage.wait({ time: 2 });
|
|
817
|
+
await dismissResidualDialogs(activePage);
|
|
818
|
+
await ensureComposerOpen(activePage);
|
|
819
|
+
await activePage.wait({ time: 2 });
|
|
820
|
+
const selectors = await resolveUploadSelectors(activePage);
|
|
821
|
+
let uploaded = false;
|
|
822
|
+
let uploadError: unknown;
|
|
823
|
+
for (const selector of selectors) {
|
|
824
|
+
try {
|
|
825
|
+
await uploadVideo(activePage, preparedUpload.uploadPath, selector);
|
|
826
|
+
const selectedFileCount = await readSelectedFileCount(activePage, selector);
|
|
827
|
+
if (selectedFileCount === 0) {
|
|
828
|
+
throw new CommandExecutionError('Instagram reel upload failed', 'The selected reel input never received the video file');
|
|
829
|
+
}
|
|
830
|
+
await waitForVideoPreview(activePage, 10);
|
|
831
|
+
uploaded = true;
|
|
832
|
+
break;
|
|
833
|
+
} catch (error) {
|
|
834
|
+
uploadError = error;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
if (!uploaded) {
|
|
838
|
+
throw uploadError instanceof Error
|
|
839
|
+
? uploadError
|
|
840
|
+
: new CommandExecutionError('Instagram reel preview did not appear after upload');
|
|
841
|
+
}
|
|
842
|
+
await clickActionMaybe(activePage, ['OK'], 'any');
|
|
843
|
+
await clickAction(activePage, ['Next', '下一步'], 'media');
|
|
844
|
+
await waitForReelStage(activePage, 'edit', 20);
|
|
845
|
+
await clickAction(activePage, ['Next', '下一步'], 'media');
|
|
846
|
+
await waitForReelStage(activePage, 'composer', 20);
|
|
847
|
+
|
|
848
|
+
if (content) {
|
|
849
|
+
await fillCaption(activePage, content);
|
|
850
|
+
await ensureCaptionFilled(activePage, content);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
await clickAction(activePage, ['Share', '分享'], 'caption');
|
|
854
|
+
const sharedUrl = await waitForPublishSuccess(activePage);
|
|
855
|
+
const url = sharedUrl || await resolveLatestReelUrl(activePage, existingMediaPaths);
|
|
856
|
+
return buildInstagramReelSuccessResult(url);
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
try {
|
|
860
|
+
if (!process.env.VITEST) {
|
|
861
|
+
const runIsolated = async (): Promise<InstagramReelSuccessRow[]> => {
|
|
862
|
+
const isolatedPage = new BrowserPage(`site:instagram-reel-${Date.now()}`);
|
|
863
|
+
try {
|
|
864
|
+
return await run(isolatedPage, new Set());
|
|
865
|
+
} finally {
|
|
866
|
+
await isolatedPage.closeWindow?.();
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
try {
|
|
871
|
+
return await runIsolated();
|
|
872
|
+
} catch (error) {
|
|
873
|
+
if (!isRecoverableReelSessionError(error)) throw error;
|
|
874
|
+
return await runIsolated();
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
const existingMediaPaths = await captureExistingProfileMediaPaths(browserPage);
|
|
879
|
+
return await run(browserPage, existingMediaPaths);
|
|
880
|
+
} finally {
|
|
881
|
+
if (preparedUpload.cleanupPath) {
|
|
882
|
+
fs.rmSync(preparedUpload.cleanupPath, { force: true });
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
},
|
|
886
|
+
});
|