Merge branch 'master' of https://gitee.com/TeaCodeNice/crawler-plugin
app/amazon/__pycache__/approve.cpython-39.pyc app/amazon/__pycache__/asin_status.cpython-39.pyc app/amazon/__pycache__/del_brand.cpython-39.pyc app/amazon/__pycache__/match_action.cpython-39.pyc app/amazon/__pycache__/price_match.cpython-39.pyc
This commit is contained in:
109
.gitignore
vendored
109
.gitignore
vendored
@@ -1,47 +1,110 @@
|
||||
# frontend-vue dependencies
|
||||
# ===== frontend-vue =====
|
||||
frontend-vue/node_modules/
|
||||
|
||||
# frontend-vue build output
|
||||
frontend-vue/dist/
|
||||
frontend-vue/new_web_source
|
||||
|
||||
# generated type declarations
|
||||
frontend-vue/auto-imports.d.ts
|
||||
frontend-vue/components.d.ts
|
||||
|
||||
# logs
|
||||
frontend-vue/npm-debug.log*
|
||||
frontend-vue/yarn-debug.log*
|
||||
frontend-vue/yarn-error.log*
|
||||
frontend-vue/pnpm-debug.log*
|
||||
|
||||
# backend-java build output
|
||||
# ===== backend-java =====
|
||||
backend-java/target/
|
||||
|
||||
# backend-java runtime data
|
||||
backend-java/data/
|
||||
backend-java/*.log
|
||||
backend-java/logs/
|
||||
logs/
|
||||
aiimage-backend.log*
|
||||
backend/tmp/
|
||||
|
||||
# backend-java local config
|
||||
backend-java/src/main/resources/application-local.yml
|
||||
|
||||
# backend-java IDE files
|
||||
backend-java/.idea/
|
||||
backend-java/*.iml
|
||||
|
||||
# desktop app
|
||||
desktop/
|
||||
ERP-Demo/
|
||||
xlsx/
|
||||
app/__pycache__
|
||||
# ===== backend / app =====
|
||||
backend/tmp/
|
||||
backend/__pycache__
|
||||
app/__pycache__
|
||||
app/assets/
|
||||
app/new_web_source
|
||||
app/user_data/
|
||||
OPS_REDIS_MYSQL_OPTIMIZATION_NOTES.md
|
||||
|
||||
# ===== logs =====
|
||||
logs/
|
||||
aiimage-backend.log*
|
||||
|
||||
# ===== Python =====
|
||||
__pycache__/
|
||||
**/__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.pyo
|
||||
.Python
|
||||
*.egg-info/
|
||||
*.egg
|
||||
MANIFEST
|
||||
.installed.cfg
|
||||
|
||||
# Build / packaging
|
||||
build/
|
||||
dist/
|
||||
develop-eggs/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
|
||||
# Virtual environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
.python-version
|
||||
|
||||
# Testing
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
htmlcov/
|
||||
|
||||
# Type checking
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
.pyre/
|
||||
.pytype/
|
||||
|
||||
# Jupyter
|
||||
.ipynb_checkpoints
|
||||
*.ipynb_checkpoints/
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# ===== Database =====
|
||||
*.db
|
||||
*.sqlite3
|
||||
|
||||
# ===== Misc =====
|
||||
desktop/
|
||||
ERP-Demo/
|
||||
xlsx/
|
||||
.omx/
|
||||
.codex
|
||||
.rtk
|
||||
OPS_REDIS_MYSQL_OPTIMIZATION_NOTES.md
|
||||
架构.md
|
||||
*ts.%
|
||||
|
||||
7
app/.env
7
app/.env
@@ -1,4 +1,3 @@
|
||||
base_url=http://8.136.19.173:15124
|
||||
workflow_id=7608812635877900322
|
||||
mysql_host=8.136.19.173
|
||||
mysql_user=aiimage
|
||||
@@ -12,10 +11,8 @@ zn_username=%E8%87%AA%E5%8A%A8%E5%8C%96_Robot
|
||||
client_name=ShuFuAI
|
||||
|
||||
|
||||
# java_api_base=http://47.111.163.154:18080
|
||||
java_api_base=http://127.0.0.1:18080
|
||||
java_api_base=http://47.111.163.154:18080
|
||||
# java_api_base=http://127.0.0.1:18080
|
||||
# java_api_base=http://8.136.19.173:18080
|
||||
# java_api_base=http://121.196.149.225:18080
|
||||
|
||||
debug=true
|
||||
|
||||
|
||||
18
app/.env.example
Normal file
18
app/.env.example
Normal file
@@ -0,0 +1,18 @@
|
||||
workflow_id=7608812635877900322
|
||||
mysql_host=8.136.19.173
|
||||
mysql_user=aiimage
|
||||
|
||||
proxy_url=https://api.jikip.com/ip-get?num=1&minute=1&format=json&area=all&protocol=1&mode=2&key=t24g6gi44ubufd8
|
||||
proxy_mode=2
|
||||
|
||||
zn_company=rongchuang123
|
||||
zn_username=%E8%87%AA%E5%8A%A8%E5%8C%96_Robot
|
||||
|
||||
client_name=ShuFuAI
|
||||
|
||||
|
||||
java_api_base=http://47.111.163.154:18080
|
||||
# java_api_base=http://127.0.0.1:18080
|
||||
# java_api_base=http://8.136.19.173:18080
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
760
app/amazon/base.py
Normal file
760
app/amazon/base.py
Normal file
@@ -0,0 +1,760 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
from typing import Any, Dict, List, Literal, Optional, TypedDict
|
||||
|
||||
from loguru import logger
|
||||
import requests
|
||||
from DrissionPage import Chromium
|
||||
from DrissionPage._pages.chromium_tab import ChromiumTab
|
||||
from DrissionPage.common import By
|
||||
|
||||
try:
|
||||
import winreg
|
||||
except ImportError:
|
||||
winreg = None
|
||||
|
||||
|
||||
STATUS_OK = "0"
|
||||
STATUS_LOGIN_FAILED = "-10003"
|
||||
|
||||
DEFAULT_SOCKET_PORT = 19890
|
||||
CLIENT_API_TIMEOUT = 120
|
||||
PORT_CHECK_TIMEOUT = 2
|
||||
UPDATE_CORE_RETRY_DELAY = 2
|
||||
CLIENT_RESTART_DELAY = 5
|
||||
CLIENT_START_RETRIES = 3
|
||||
CLIENT_READY_TIMEOUT = 10
|
||||
CLIENT_READY_INTERVAL = 0.5
|
||||
CLIENT_POST_START_DELAY = 5
|
||||
PROCESS_KILL_DELAY = 3
|
||||
|
||||
DOC_LOAD_TIMEOUT = 30
|
||||
COUNTRY_INITIAL_LOAD_TIMEOUT = 120
|
||||
COUNTRY_LOOKUP_TIMEOUT = 20
|
||||
COUNTRY_CLICK_TIMEOUT = 10
|
||||
COUNTRY_DROPDOWN_DELAY = 1
|
||||
COUNTRY_DROPDOWN_READY_TIMEOUT = 20
|
||||
COUNTRY_DROPDOWN_RETRY_INTERVAL = 0.5
|
||||
IP_CHECK_TIMEOUT = 60
|
||||
|
||||
LOGIN_ATTEMPTS = 4
|
||||
PASSWORD_INPUT_TIMEOUT = 5
|
||||
PASSWORD_SUBMIT_TIMEOUT = 5
|
||||
OTP_SEND_TIMEOUT = 5
|
||||
OTP_INPUT_LOOKUP_ATTEMPTS = 5
|
||||
OTP_INPUT_TIMEOUT = 30
|
||||
OTP_INPUT_DISPLAY_TIMEOUT = 10
|
||||
OTP_CODE_WAIT_SECONDS = 30
|
||||
OTP_SUBMIT_TIMEOUT = 10
|
||||
OTP_RESULT_LOAD_TIMEOUT = 20
|
||||
OTP_ERROR_TIMEOUT = 10
|
||||
OTP_SEND_DELAY = 1
|
||||
LOGIN_FINAL_SUBMIT_TIMEOUT = 10
|
||||
NEED_LOGIN_DELAY = 3
|
||||
NEED_LOGIN_TIMEOUT = 5
|
||||
|
||||
COUNTRY_LABEL_XPATH = 'xpath://div[@class="dropdown-account-switcher-header-label"]/span[last()]'
|
||||
COUNTRY_DROPDOWN_XPATH = 'xpath://div[@class="dropdown-account-switcher-header-label"]'
|
||||
COUNTRY_LIST_ITEM_XPATH = 'xpath://div[@class="dropdown-account-switcher-list-item"]'
|
||||
NEED_LOGIN_XPATH = 'xpath://h1[@class="a-spacing-small"]|//span[contains(text(),"登录")]'
|
||||
PASSWORD_INPUT_XPATH = 'xpath://input[@type="password"]'
|
||||
PASSWORD_SUBMIT_XPATH = 'xpath://input[@id="signInSubmit"]'
|
||||
OTP_SEND_XPATH = 'xpath://span[@id="auth-send-code" and contains(string(.),"发送一次性密码")]'
|
||||
OTP_INPUT_XPATH = 'xpath://input[@name="otpCode"]'
|
||||
OTP_SUBMIT_XPATH = 'xpath://input[@id="auth-signin-button"]'
|
||||
OTP_ERROR_XPATH = 'xpath://div[@id="auth-error-message-box"]'
|
||||
|
||||
|
||||
class UserInfo(TypedDict):
|
||||
username: str
|
||||
password: str
|
||||
company: str
|
||||
|
||||
|
||||
def kill_process(version: Literal["v5", "v6"]):
|
||||
"""结束指定版本的紫鸟客户端进程."""
|
||||
logger.info("准备杀紫鸟客户端进程,version={}", version)
|
||||
driver = ZiniaoDriver({})
|
||||
driver.kill_process(version)
|
||||
|
||||
|
||||
class ZiniaoDriver:
|
||||
"""封装紫鸟客户端启动,店铺浏览器生命周期和客户端 API 调用."""
|
||||
|
||||
def __init__(self, user_info: UserInfo, socket_port: int = DEFAULT_SOCKET_PORT):
|
||||
"""初始化紫鸟浏览器驱动.
|
||||
|
||||
Args:
|
||||
user_info: 紫鸟账号信息,包含 company, username, password.
|
||||
socket_port: 客户端 HTTP 通信端口.
|
||||
"""
|
||||
self.user_info = user_info
|
||||
self.socket_port = socket_port
|
||||
self.client_path = None
|
||||
self.browser = None
|
||||
self.tab: Optional[ChromiumTab] = None
|
||||
self.store_id = None
|
||||
|
||||
@property
|
||||
def client_url(self) -> str:
|
||||
return f"http://127.0.0.1:{self.socket_port}"
|
||||
|
||||
def _build_payload(self, action: str, extra: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
payload = {
|
||||
"action": action,
|
||||
"requestId": str(uuid.uuid4()),
|
||||
}
|
||||
if extra:
|
||||
payload.update(extra)
|
||||
payload.update(self.user_info)
|
||||
return payload
|
||||
|
||||
def _post_client(self, payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||
response = requests.post(
|
||||
self.client_url,
|
||||
json.dumps(payload).encode("utf-8"),
|
||||
timeout=CLIENT_API_TIMEOUT,
|
||||
)
|
||||
return response.json()
|
||||
|
||||
@staticmethod
|
||||
def _status_code(result: Optional[Dict[str, Any]]) -> Optional[str]:
|
||||
if result is None:
|
||||
return None
|
||||
return str(result.get("statusCode"))
|
||||
|
||||
@staticmethod
|
||||
def _result_text(result: Dict[str, Any]) -> str:
|
||||
return json.dumps(result, ensure_ascii=False)
|
||||
|
||||
@staticmethod
|
||||
def _find_store_oauth(shop_list: Optional[List[Dict[str, Any]]], shop_name: str):
|
||||
for shop in shop_list or []:
|
||||
if shop.get("browserName") == shop_name:
|
||||
return shop.get("browserOauth")
|
||||
return None
|
||||
|
||||
def _is_client_ready(self) -> bool:
|
||||
try:
|
||||
requests.get(self.client_url, timeout=PORT_CHECK_TIMEOUT)
|
||||
return True
|
||||
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
|
||||
return False
|
||||
|
||||
def _wait_until_client_ready(self, timeout: float) -> bool:
|
||||
start_check_time = time.time()
|
||||
while time.time() - start_check_time < timeout:
|
||||
if self._is_client_ready():
|
||||
return True
|
||||
time.sleep(CLIENT_READY_INTERVAL)
|
||||
return False
|
||||
|
||||
def get_zinaio_exe(self, protocol_name: str = "superbrowser"):
|
||||
"""从 Windows 注册表读取紫鸟客户端可执行文件路径.
|
||||
|
||||
Args:
|
||||
protocol_name: 注册表协议名称.
|
||||
|
||||
Returns:
|
||||
紫鸟浏览器可执行文件路径. 未找到时返回 None.
|
||||
"""
|
||||
if winreg is None:
|
||||
logger.error("当前系统不支持 winreg,无法从注册表获取紫鸟客户端路径")
|
||||
return None
|
||||
|
||||
key_path = rf"SOFTWARE\Classes\{protocol_name}\shell\open\command"
|
||||
for root_key, root_name in (
|
||||
(winreg.HKEY_CURRENT_USER, "HKEY_CURRENT_USER"),
|
||||
(winreg.HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE"),
|
||||
):
|
||||
try:
|
||||
logger.info("正在从注册表读取紫鸟客户端路径:{}\\{}", root_name, key_path)
|
||||
key = winreg.OpenKey(root_key, key_path)
|
||||
command, _ = winreg.QueryValueEx(key, "")
|
||||
winreg.CloseKey(key)
|
||||
if isinstance(command, str):
|
||||
sub = "ziniao.exe"
|
||||
exe_path = command[0 : command.find(sub) + len(sub) + 1]
|
||||
else:
|
||||
exe_path = command[0]
|
||||
logger.info("已获取紫鸟客户端路径:{}", exe_path)
|
||||
return exe_path
|
||||
except FileNotFoundError:
|
||||
logger.warning("注册表中未找到紫鸟客户端路径:{}\\{}", root_name, key_path)
|
||||
|
||||
logger.error("未能获取紫鸟客户端路径,protocol_name={}", protocol_name)
|
||||
return None
|
||||
|
||||
def update_core(self):
|
||||
"""更新紫鸟浏览器内核.
|
||||
|
||||
打开店铺前调用,要求客户端版本 5.285.7 以上. 接口可能因 HTTP 超时
|
||||
返回未完成状态,因此会循环调用直到成功或判定客户端不支持.
|
||||
"""
|
||||
payload = self._build_payload("updateCore")
|
||||
|
||||
logger.info("开始更新紫鸟浏览器内核")
|
||||
while True:
|
||||
result = self._post_client(payload)
|
||||
logger.info("更新内核返回:{}", result)
|
||||
if self._handle_update_core_result(result):
|
||||
return
|
||||
if result is None:
|
||||
continue
|
||||
|
||||
logger.info("等待更新内核完成:{}", self._result_text(result))
|
||||
time.sleep(UPDATE_CORE_RETRY_DELAY)
|
||||
|
||||
def _handle_update_core_result(self, result: Optional[Dict[str, Any]]) -> bool:
|
||||
if result is None:
|
||||
logger.info("等待客户端启动后继续更新内核")
|
||||
time.sleep(UPDATE_CORE_RETRY_DELAY)
|
||||
return False
|
||||
|
||||
status_code = self._status_code(result)
|
||||
if status_code is None or status_code == STATUS_LOGIN_FAILED:
|
||||
logger.error("当前紫鸟客户端版本不支持更新内核接口,请升级客户端")
|
||||
return True
|
||||
if status_code == STATUS_OK:
|
||||
logger.info("紫鸟浏览器内核更新完成")
|
||||
return True
|
||||
return False
|
||||
|
||||
def kill_process(self, version: Literal["v5", "v6"]):
|
||||
"""结束指定版本的紫鸟客户端进程.
|
||||
|
||||
Args:
|
||||
version: 客户端主版本.
|
||||
"""
|
||||
if version == "v5":
|
||||
process_name = "SuperBrowser.exe"
|
||||
logger.info("准备结束紫鸟 v5 starter.exe")
|
||||
self._kill_process_by_name("starter.exe")
|
||||
else:
|
||||
process_name = "ziniao.exe"
|
||||
|
||||
logger.info("准备结束紫鸟客户端进程:{}", process_name)
|
||||
self._kill_process_by_name(process_name)
|
||||
time.sleep(PROCESS_KILL_DELAY)
|
||||
|
||||
def _kill_process_by_name(self, process_name: str) -> None:
|
||||
result = subprocess.run(
|
||||
["taskkill", "/f", "/t", "/im", process_name],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="ignore",
|
||||
check=False,
|
||||
)
|
||||
output = (result.stdout or result.stderr or "").strip()
|
||||
if result.returncode == 0:
|
||||
if output:
|
||||
logger.info("结束进程返回:{}", output)
|
||||
return
|
||||
|
||||
if "没有找到进程" in output or "not found" in output.lower():
|
||||
logger.info("进程未运行,跳过结束:{}", process_name)
|
||||
return
|
||||
|
||||
logger.warning("结束进程失败:{} | {}", process_name, output or f"returncode={result.returncode}")
|
||||
|
||||
def get_browser_list(self) -> Optional[list[Dict[str, Any]]]:
|
||||
"""获取紫鸟浏览器店铺列表.
|
||||
|
||||
Returns:
|
||||
浏览器店铺列表.登录失败或请求失败时返回 None.
|
||||
"""
|
||||
payload = self._build_payload("getBrowserList")
|
||||
request_id = payload["requestId"]
|
||||
|
||||
logger.info("开始获取紫鸟浏览器列表,requestId={}", request_id)
|
||||
result = self._post_client(payload)
|
||||
status_code = self._status_code(result)
|
||||
if status_code == STATUS_OK:
|
||||
browser_list = result.get("browserList")
|
||||
logger.info("获取紫鸟浏览器列表成功,数量={}", len(browser_list or []))
|
||||
return browser_list
|
||||
if status_code == STATUS_LOGIN_FAILED:
|
||||
logger.error("获取紫鸟浏览器列表登录失败:{}", self._result_text(result))
|
||||
return None
|
||||
|
||||
logger.error("获取紫鸟浏览器列表失败:{}", self._result_text(result))
|
||||
return None
|
||||
|
||||
def open_store(
|
||||
self,
|
||||
store_info,
|
||||
isWebDriverReadOnlyMode=0,
|
||||
isprivacy=0,
|
||||
isHeadless=0,
|
||||
cookieTypeSave=0,
|
||||
jsInfo="",
|
||||
):
|
||||
"""启动紫鸟店铺浏览器.
|
||||
|
||||
Args:
|
||||
store_info: 店铺 browserId 或 browserOauth.
|
||||
isWebDriverReadOnlyMode: WebDriver 只读模式开关.
|
||||
isprivacy: 隐私模式开关.
|
||||
isHeadless: 无头模式开关.
|
||||
cookieTypeSave: Cookie 保存类型.
|
||||
jsInfo: 注入的 JS 信息.
|
||||
|
||||
Returns:
|
||||
紫鸟客户端返回的启动结果.
|
||||
"""
|
||||
payload = self._build_start_browser_payload(
|
||||
store_info=store_info,
|
||||
is_web_driver_read_only_mode=isWebDriverReadOnlyMode,
|
||||
is_privacy=isprivacy,
|
||||
is_headless=isHeadless,
|
||||
cookie_type_save=cookieTypeSave,
|
||||
js_info=jsInfo,
|
||||
)
|
||||
request_id = payload["requestId"]
|
||||
|
||||
logger.info("开始打开紫鸟店铺,store_info={},requestId={}", store_info, request_id)
|
||||
result = self._post_client(payload)
|
||||
status_code = self._status_code(result)
|
||||
if status_code == STATUS_OK:
|
||||
logger.info("紫鸟店铺打开成功,debuggingPort={}", result.get("debuggingPort"))
|
||||
return result
|
||||
if status_code == STATUS_LOGIN_FAILED:
|
||||
logger.error("打开紫鸟店铺登录失败:{}", self._result_text(result))
|
||||
raise RuntimeError(f"[open_store]登录失败 {self._result_text(result)}")
|
||||
|
||||
logger.error("打开紫鸟店铺失败:{}", self._result_text(result))
|
||||
raise RuntimeError(f"[open_store]失败 {self._result_text(result)} ")
|
||||
|
||||
def _build_start_browser_payload(
|
||||
self,
|
||||
*,
|
||||
store_info,
|
||||
is_web_driver_read_only_mode=0,
|
||||
is_privacy=0,
|
||||
is_headless=0,
|
||||
cookie_type_save=0,
|
||||
js_info="",
|
||||
) -> Dict[str, Any]:
|
||||
payload = self._build_payload(
|
||||
"startBrowser",
|
||||
{
|
||||
"isWaitPluginUpdate": 0,
|
||||
"isHeadless": is_headless,
|
||||
"isWebDriverReadOnlyMode": is_web_driver_read_only_mode,
|
||||
"cookieTypeLoad": 0,
|
||||
"cookieTypeSave": cookie_type_save,
|
||||
"runMode": "1",
|
||||
"isLoadUserPlugin": True,
|
||||
"pluginIdType": 1,
|
||||
"privacyMode": is_privacy,
|
||||
},
|
||||
)
|
||||
|
||||
if store_info.isdigit():
|
||||
payload["browserId"] = store_info
|
||||
else:
|
||||
payload["browserOauth"] = store_info
|
||||
|
||||
if len(str(js_info)) > 2:
|
||||
payload["injectJsInfo"] = json.dumps(js_info)
|
||||
|
||||
return payload
|
||||
|
||||
def get_browser(self, port) -> Chromium:
|
||||
"""连接 DrissionPage 浏览器实例.
|
||||
|
||||
Args:
|
||||
port: Chromium 调试端口.
|
||||
|
||||
Returns:
|
||||
DrissionPage Chromium 实例.
|
||||
"""
|
||||
logger.info("开始连接 DrissionPage 浏览器,port={}", port)
|
||||
browser = Chromium(port)
|
||||
logger.info("DrissionPage 浏览器连接完成,port={}", port)
|
||||
return browser
|
||||
|
||||
def start_client(self):
|
||||
"""启动紫鸟客户端并更新浏览器内核."""
|
||||
if self._is_client_ready():
|
||||
logger.info("端口 {} 已启动,跳过启动紫鸟客户端", self.socket_port)
|
||||
return
|
||||
|
||||
logger.info("端口 {} 未启动,开始启动紫鸟客户端", self.socket_port)
|
||||
self.kill_process("v6")
|
||||
time.sleep(CLIENT_RESTART_DELAY)
|
||||
client_path = self.get_zinaio_exe("superbrowserv6")
|
||||
if not client_path:
|
||||
raise RuntimeError("未找到紫鸟客户端路径,无法启动客户端")
|
||||
|
||||
self.client_path = client_path.strip('"')
|
||||
cmd = [
|
||||
self.client_path,
|
||||
"--run_type=web_driver",
|
||||
"--ipc_type=http",
|
||||
"--port=" + str(self.socket_port),
|
||||
]
|
||||
logger.info("紫鸟客户端启动命令:{}", " ".join(cmd))
|
||||
|
||||
for retry_count in range(CLIENT_START_RETRIES):
|
||||
logger.info("第 {}/{} 次尝试启动紫鸟客户端", retry_count + 1, CLIENT_START_RETRIES)
|
||||
subprocess.Popen(cmd)
|
||||
|
||||
if self._wait_until_client_ready(CLIENT_READY_TIMEOUT):
|
||||
logger.info("紫鸟客户端启动成功,第 {} 次尝试", retry_count + 1)
|
||||
time.sleep(CLIENT_POST_START_DELAY)
|
||||
self.update_core()
|
||||
return
|
||||
|
||||
logger.warning("第 {} 次尝试启动失败,10秒内未检测到客户端启动", retry_count + 1)
|
||||
|
||||
logger.error("紫鸟客户端启动失败,已重试 {} 次", CLIENT_START_RETRIES)
|
||||
raise RuntimeError(f"客户端启动失败:重试 {CLIENT_START_RETRIES} 次后仍未成功启动")
|
||||
|
||||
def open_shop(self, shop_name: str):
|
||||
"""打开指定店铺并完成 IP 检测.
|
||||
|
||||
Args:
|
||||
shop_name: 店铺名称.
|
||||
|
||||
Returns:
|
||||
成功时返回浏览器实例.店铺不存在时返回错误信息.
|
||||
"""
|
||||
logger.info("开始打开店铺:{}", shop_name)
|
||||
self.start_client()
|
||||
|
||||
self.store_id = self._find_store_oauth(self.get_browser_list(), shop_name)
|
||||
if not self.store_id:
|
||||
logger.warning("店铺不存在:{}", shop_name)
|
||||
return "店铺不存在"
|
||||
|
||||
ret_json = self.open_store(self.store_id)
|
||||
self.store_id = ret_json.get("browserOauth")
|
||||
if self.store_id is None:
|
||||
self.store_id = ret_json.get("browserId")
|
||||
|
||||
self.browser = self.get_browser(ret_json.get("debuggingPort"))
|
||||
ip_check_url = ret_json.get("ipDetectionPage")
|
||||
if not ip_check_url:
|
||||
logger.error("ip检测页地址为空,准备关闭店铺:{}", shop_name)
|
||||
self.close_store(self.store_id)
|
||||
raise RuntimeError("没有IP检测地址,为了店铺安全不打开店铺")
|
||||
|
||||
if self.open_ip_check(self.browser, ip_check_url):
|
||||
logger.info("IP检测通过,打开店铺平台主页:{}", shop_name)
|
||||
self.open_launcher_page(ret_json.get("launcherPage"), self.browser)
|
||||
return self.browser
|
||||
|
||||
logger.error("IP检测不通过,停止打开店铺:{}", shop_name)
|
||||
raise RuntimeError("IP检测不通过,可能是因为网络环境变化导致的,为了店铺安全不打开店铺")
|
||||
|
||||
def close_store(self, browser_oauth=None):
|
||||
"""关闭紫鸟店铺浏览器.
|
||||
|
||||
Args:
|
||||
browser_oauth: 店铺 OAuth 标识. 未提供时使用当前打开的店铺.
|
||||
|
||||
Returns:
|
||||
紫鸟客户端返回的关闭结果.
|
||||
"""
|
||||
if browser_oauth is None:
|
||||
browser_oauth = self.store_id
|
||||
|
||||
payload = self._build_payload(
|
||||
"stopBrowser",
|
||||
{
|
||||
"duplicate": 0,
|
||||
"browserOauth": browser_oauth,
|
||||
},
|
||||
)
|
||||
request_id = payload["requestId"]
|
||||
|
||||
logger.info("开始关闭紫鸟店铺,browserOauth={},requestId={}", browser_oauth, request_id)
|
||||
result = self._post_client(payload)
|
||||
status_code = self._status_code(result)
|
||||
if status_code == STATUS_OK:
|
||||
logger.info("紫鸟店铺关闭成功,browserOauth={}", browser_oauth)
|
||||
return result
|
||||
if status_code == STATUS_LOGIN_FAILED:
|
||||
logger.error("关闭紫鸟店铺登录失败:{}", self._result_text(result))
|
||||
raise RuntimeError(f"【close_store】登录失败 {self._result_text(result)}")
|
||||
|
||||
logger.error("关闭紫鸟店铺失败:{}", self._result_text(result))
|
||||
raise RuntimeError(f"[close_store]失败: {self._result_text(result)} ")
|
||||
|
||||
def open_launcher_page(self, launcher_page: str, browser: Chromium = None):
|
||||
"""打开店铺平台启动页.
|
||||
|
||||
Args:
|
||||
launcher_page: 要打开的启动页 URL.
|
||||
browser: 浏览器实例. 未提供时使用当前浏览器实例.
|
||||
"""
|
||||
if browser is None:
|
||||
browser = self.browser
|
||||
|
||||
logger.info("打开店铺平台启动页:{}", launcher_page)
|
||||
tab = browser.new_tab(url=launcher_page)
|
||||
self.tab = tab
|
||||
return tab
|
||||
|
||||
def open_ip_check(self, browser: Chromium, ip_check_url: str):
|
||||
"""打开 IP 检测页并判断网络环境是否通过.
|
||||
|
||||
Args:
|
||||
browser: DrissionPage 浏览器会话.
|
||||
ip_check_url: IP 检测页地址.
|
||||
|
||||
Returns:
|
||||
IP 检测通过时返回 True,否则返回 False.
|
||||
"""
|
||||
try:
|
||||
logger.info("开始打开IP检测页:{}", ip_check_url)
|
||||
tab = browser.latest_tab
|
||||
tab.get(ip_check_url)
|
||||
success_button = tab.ele(
|
||||
(By.XPATH, '//button[contains(@class, "styles_btn--success")]'),
|
||||
timeout=IP_CHECK_TIMEOUT,
|
||||
)
|
||||
if success_button:
|
||||
logger.info("IP检测成功")
|
||||
return True
|
||||
|
||||
logger.warning("IP检测超时或未找到成功按钮")
|
||||
return False
|
||||
except Exception:
|
||||
logger.exception("IP检测异常")
|
||||
return False
|
||||
|
||||
|
||||
class AmamzonBase(ZiniaoDriver):
|
||||
"""亚马逊页面操作基类."""
|
||||
|
||||
def switch_to_country(self, country_name: str):
|
||||
"""切换亚马逊账号国家并验证结果.
|
||||
|
||||
流程包括读取当前国家,打开国家下拉框,选择目标国家并等待页面加载.
|
||||
|
||||
Args:
|
||||
country_name: 目标国家名称.
|
||||
|
||||
Returns:
|
||||
切换成功返回 True,否则返回 False.
|
||||
"""
|
||||
try:
|
||||
if self.browser is None:
|
||||
logger.warning("浏览器实例不存在,请先打开店铺")
|
||||
return False
|
||||
|
||||
self.tab.wait.doc_loaded(timeout=COUNTRY_INITIAL_LOAD_TIMEOUT, raise_err=False)
|
||||
current_country = self._get_current_country(timeout=COUNTRY_LOOKUP_TIMEOUT)
|
||||
if current_country is None:
|
||||
logger.warning("无法获取当前国家信息")
|
||||
return False
|
||||
if current_country == country_name:
|
||||
logger.info("当前已经是目标国家 {},无需切换", country_name)
|
||||
return True
|
||||
|
||||
if not self._open_country_dropdown():
|
||||
return False
|
||||
if not self._select_country(country_name):
|
||||
return False
|
||||
|
||||
logger.info("等待国家切换后页面加载")
|
||||
self.tab.wait.doc_loaded()
|
||||
return self._verify_country(country_name)
|
||||
|
||||
except Exception:
|
||||
logger.exception("切换国家时发生异常")
|
||||
return False
|
||||
|
||||
def _get_current_country(self, *, timeout: int) -> Optional[str]:
|
||||
logger.info("正在检查当前国家")
|
||||
current_country_ele = self.tab.ele(COUNTRY_LABEL_XPATH, timeout=timeout)
|
||||
if not current_country_ele:
|
||||
return None
|
||||
|
||||
current_country = current_country_ele.text.strip()
|
||||
logger.info("当前国家:{}", current_country)
|
||||
return current_country
|
||||
|
||||
def _open_country_dropdown(self) -> bool:
|
||||
logger.info("正在打开国家切换下拉框")
|
||||
dropdown_header = self.tab.ele(COUNTRY_DROPDOWN_XPATH, timeout=COUNTRY_CLICK_TIMEOUT)
|
||||
if not dropdown_header:
|
||||
logger.warning("找不到国家切换下拉框")
|
||||
return False
|
||||
|
||||
logger.info("正在展开国家列表")
|
||||
first_item = self._wait_country_list_item(dropdown_header)
|
||||
if first_item is None:
|
||||
logger.warning("找不到国家列表项")
|
||||
return False
|
||||
|
||||
first_item.click()
|
||||
time.sleep(COUNTRY_DROPDOWN_DELAY)
|
||||
return True
|
||||
|
||||
def _wait_country_list_item(self, dropdown_header):
|
||||
deadline = time.time() + COUNTRY_DROPDOWN_READY_TIMEOUT
|
||||
attempt = 0
|
||||
while time.time() < deadline:
|
||||
attempt += 1
|
||||
try:
|
||||
dropdown_header.click()
|
||||
except Exception as exc:
|
||||
logger.info("点击国家切换下拉框失败, attempt={}, error={}", attempt, exc)
|
||||
|
||||
time.sleep(COUNTRY_DROPDOWN_DELAY)
|
||||
first_item = self.tab.ele(COUNTRY_LIST_ITEM_XPATH, timeout=COUNTRY_DROPDOWN_RETRY_INTERVAL)
|
||||
if first_item:
|
||||
return first_item
|
||||
|
||||
self.tab.wait.doc_loaded(timeout=COUNTRY_CLICK_TIMEOUT, raise_err=False)
|
||||
time.sleep(COUNTRY_DROPDOWN_RETRY_INTERVAL)
|
||||
return None
|
||||
|
||||
def _select_country(self, country_name: str) -> bool:
|
||||
logger.info("正在切换到国家:{}", country_name)
|
||||
target_country = self.tab.ele(self._country_option_xpath(country_name), timeout=COUNTRY_CLICK_TIMEOUT)
|
||||
if not target_country:
|
||||
logger.warning("找不到目标国家:{}", country_name)
|
||||
return False
|
||||
target_country.click()
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def _country_option_xpath(country_name: str) -> str:
|
||||
return (
|
||||
"xpath://div[@class="
|
||||
'"dropdown-account-switcher-list-item dropdown-account-switcher-list-item-indented" '
|
||||
f'and @title="{country_name}"]'
|
||||
)
|
||||
|
||||
def _verify_country(self, country_name: str) -> bool:
|
||||
new_country = self._get_current_country(timeout=COUNTRY_CLICK_TIMEOUT)
|
||||
if new_country is None:
|
||||
logger.warning("无法验证国家切换结果")
|
||||
return False
|
||||
if new_country == country_name:
|
||||
logger.info("国家切换成功:{}", new_country)
|
||||
return True
|
||||
|
||||
logger.warning("国家切换失败,当前国家:{},目标国家:{}", new_country, country_name)
|
||||
return False
|
||||
|
||||
def need_login(self):
|
||||
"""判断当前页面是否需要登录."""
|
||||
time.sleep(NEED_LOGIN_DELAY)
|
||||
self.tab.wait.doc_loaded(timeout=DOC_LOAD_TIMEOUT, raise_err=False)
|
||||
need_login_ele = self.tab.eles(NEED_LOGIN_XPATH, timeout=NEED_LOGIN_TIMEOUT)
|
||||
if len(need_login_ele) > 0:
|
||||
logger.info("检测到需要登录元素")
|
||||
return True
|
||||
logger.info("未检测到登录元素")
|
||||
return False
|
||||
|
||||
def login(self, password, username=""):
|
||||
"""登录当前页面,必要时等待并提交一次性验证码.
|
||||
|
||||
Args:
|
||||
password: 登录密码.
|
||||
username: 兼容保留参数,当前未使用.
|
||||
|
||||
Returns:
|
||||
登录成功返回 True,失败返回 False.
|
||||
"""
|
||||
try:
|
||||
self.tab.wait.doc_loaded(timeout=DOC_LOAD_TIMEOUT, raise_err=False)
|
||||
for _ in range(LOGIN_ATTEMPTS):
|
||||
self._submit_password_if_present(password)
|
||||
self.tab.wait.doc_loaded(timeout=DOC_LOAD_TIMEOUT, raise_err=False)
|
||||
|
||||
if self._send_otp_if_present():
|
||||
continue
|
||||
if self._submit_ready_otp():
|
||||
return True
|
||||
|
||||
self._click_final_login_submit_if_present()
|
||||
except Exception:
|
||||
logger.exception("登录过程中发生异常")
|
||||
return False
|
||||
|
||||
def _submit_password_if_present(self, password) -> None:
|
||||
pwd_input = self.tab.eles(PASSWORD_INPUT_XPATH, timeout=PASSWORD_INPUT_TIMEOUT)
|
||||
if len(pwd_input) > 0:
|
||||
logger.info("检测到密码输入框,准备输入密码")
|
||||
pwd_input[0].input(password, clear=True)
|
||||
|
||||
submit_btn = self.tab.eles(PASSWORD_SUBMIT_XPATH, timeout=PASSWORD_SUBMIT_TIMEOUT)
|
||||
if len(submit_btn) > 0:
|
||||
logger.info("点击登录提交按钮")
|
||||
submit_btn[0].click()
|
||||
|
||||
def _send_otp_if_present(self) -> bool:
|
||||
send_code = self.tab.eles(OTP_SEND_XPATH, timeout=OTP_SEND_TIMEOUT)
|
||||
logger.info("发送一次性密码元素数量:{}", len(send_code))
|
||||
if len(send_code) == 0:
|
||||
return False
|
||||
|
||||
logger.info("检测到发送一次性密码按钮,准备点击")
|
||||
send_code[0].click()
|
||||
time.sleep(OTP_SEND_DELAY)
|
||||
self.tab.wait.doc_loaded(timeout=DOC_LOAD_TIMEOUT, raise_err=False)
|
||||
return True
|
||||
|
||||
def _submit_ready_otp(self) -> bool:
|
||||
for _ in range(OTP_INPUT_LOOKUP_ATTEMPTS):
|
||||
otp_input = self._find_otp_input()
|
||||
if otp_input is None:
|
||||
return False
|
||||
if self._wait_for_otp_value(otp_input) and self._submit_otp_if_ready():
|
||||
return True
|
||||
return False
|
||||
|
||||
def _find_otp_input(self):
|
||||
otp_code_input = self.tab.eles(OTP_INPUT_XPATH, timeout=OTP_INPUT_TIMEOUT)
|
||||
logger.info("验证码输入框数量:{}", len(otp_code_input))
|
||||
if len(otp_code_input) == 0:
|
||||
return None
|
||||
|
||||
otp_code_input[0].wait.displayed(timeout=OTP_INPUT_DISPLAY_TIMEOUT, raise_err=False)
|
||||
return otp_code_input[0]
|
||||
|
||||
@staticmethod
|
||||
def _has_otp_value(otp_input) -> bool:
|
||||
return otp_input.value is not None and otp_input.value.strip() != ""
|
||||
|
||||
def _wait_for_otp_value(self, otp_input) -> bool:
|
||||
for _ in range(OTP_CODE_WAIT_SECONDS):
|
||||
if self._has_otp_value(otp_input):
|
||||
logger.info("检测到验证码输入完成")
|
||||
return True
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
def _submit_otp_if_ready(self) -> bool:
|
||||
submit_btn = self.tab.eles(OTP_SUBMIT_XPATH, timeout=OTP_SUBMIT_TIMEOUT)
|
||||
if len(submit_btn) == 0:
|
||||
return False
|
||||
|
||||
submit_btn[0].click()
|
||||
self.tab.wait.doc_loaded(timeout=OTP_RESULT_LOAD_TIMEOUT, raise_err=False)
|
||||
error_mes = self.tab.eles(OTP_ERROR_XPATH, timeout=OTP_ERROR_TIMEOUT)
|
||||
if len(error_mes) > 0:
|
||||
logger.warning("验证码输入错误:{}", error_mes[0].text)
|
||||
self.tab.refresh()
|
||||
return False
|
||||
|
||||
logger.info("登录成功")
|
||||
return True
|
||||
|
||||
def _click_final_login_submit_if_present(self) -> None:
|
||||
submit_btn = self.tab.eles(OTP_SUBMIT_XPATH, timeout=LOGIN_FINAL_SUBMIT_TIMEOUT)
|
||||
if len(submit_btn) > 0:
|
||||
logger.info("点击二次登录提交按钮")
|
||||
submit_btn[0].click()
|
||||
|
||||
@@ -10,6 +10,7 @@ from amazon.approve import ApproveTask
|
||||
from amazon.match_action import MatchTak
|
||||
from amazon.price_match import PriceTask
|
||||
from amazon.asin_status import StatusTask
|
||||
from amazon.patrol_delete import PatrolDeleteTask
|
||||
from amazon.detail_spider import SpiderTask
|
||||
|
||||
|
||||
@@ -59,6 +60,7 @@ class TaskMonitor:
|
||||
"shop-match-run" : "匹配价格",
|
||||
"price-track-run" : "跟价",
|
||||
"query-asin-run" : "状态查询",
|
||||
"patrol-delete-run" : "巡店删除",
|
||||
"appearance-patent-run" : "亚马逊采集",
|
||||
}
|
||||
try:
|
||||
@@ -128,6 +130,7 @@ class TaskMonitor:
|
||||
"匹配价格" : MatchTak,
|
||||
"跟价" : PriceTask,
|
||||
"状态查询" : StatusTask,
|
||||
"巡店删除" : PatrolDeleteTask,
|
||||
"亚马逊采集" : SpiderTask,
|
||||
}
|
||||
self.log(f"线程 {id(task_data)} 开始处理产品风险审批任务...")
|
||||
|
||||
1882
app/amazon/patrol_delete.py
Normal file
1882
app/amazon/patrol_delete.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,55 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function rowOf(el, index) {
|
||||
const attrs = attrsOf(el);
|
||||
return {
|
||||
index,
|
||||
tag: el.tagName.toLowerCase(),
|
||||
visible: isVisible(el),
|
||||
text: textOf(el),
|
||||
attrs,
|
||||
outerHTML: (el.outerHTML || '').slice(0, 2500),
|
||||
};
|
||||
}
|
||||
|
||||
const candidates = allElements()
|
||||
.map((el, index) => rowOf(el, index))
|
||||
.filter((row) => row.visible && /补全草稿|快速查看|quick view|draft/i.test(`${row.tag} ${row.text} ${Object.values(row.attrs).join(' ')}`))
|
||||
.slice(0, 120);
|
||||
|
||||
return candidates;
|
||||
@@ -0,0 +1,73 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function attrsText(attrs) {
|
||||
return Object.entries(attrs).map(([key, value]) => `${key}=${value}`).join(' ');
|
||||
}
|
||||
|
||||
const pattern = /快速查看|quick|draft|草稿|提交|submitted|missing|inventory|myinventory/i;
|
||||
const elements = allElements()
|
||||
.map((el, index) => {
|
||||
const attrs = attrsOf(el);
|
||||
const text = textOf(el);
|
||||
return {
|
||||
index,
|
||||
tag: el.tagName.toLowerCase(),
|
||||
visible: isVisible(el),
|
||||
text: text.slice(0, 500),
|
||||
attrs,
|
||||
outerHTML: (el.outerHTML || '').slice(0, 1000),
|
||||
haystack: `${el.tagName} ${text} ${attrsText(attrs)}`,
|
||||
};
|
||||
})
|
||||
.filter((row) => pattern.test(row.haystack))
|
||||
.map(({ haystack, ...row }) => row)
|
||||
.slice(0, 80);
|
||||
|
||||
const resources = performance
|
||||
.getEntriesByType('resource')
|
||||
.map((entry) => entry.name)
|
||||
.filter((url) => pattern.test(url))
|
||||
.slice(-120);
|
||||
|
||||
return {
|
||||
location: window.location.href,
|
||||
readyState: document.readyState,
|
||||
capturedResponses: (window.__crawlerCompleteDraftResponses || []).slice(-10),
|
||||
elements,
|
||||
resources,
|
||||
};
|
||||
@@ -0,0 +1,121 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function hasCountText(text) {
|
||||
return /[((]\s*[\d,]+\s*[))]/.test(text);
|
||||
}
|
||||
|
||||
function insideListingRow(el) {
|
||||
return Boolean(el.closest('[data-sku], tr, [role="row"], [data-testid*="row"]'));
|
||||
}
|
||||
|
||||
function rowOf(el, index) {
|
||||
const attrs = attrsOf(el);
|
||||
const text = textOf(el);
|
||||
const labelText = attrs.label || attrs.title || attrs['aria-label'] || '';
|
||||
const valueText = attrs.value || attrs.count || attrs['data-count'] || attrs['data-value'] || '';
|
||||
return {
|
||||
index,
|
||||
raw_text: text,
|
||||
label_text: labelText,
|
||||
quantity_text: valueText,
|
||||
attrs,
|
||||
visible: isVisible(el),
|
||||
outerHTML: (el.outerHTML || '').slice(0, 1200),
|
||||
};
|
||||
}
|
||||
|
||||
function splitCountRow(el, index) {
|
||||
const countText = textOf(el);
|
||||
if (!/^\s*[\d,]+\s*$/.test(countText)) return null;
|
||||
|
||||
let parent = el.parentElement;
|
||||
for (let depth = 0; parent && depth < 4; depth += 1, parent = parent.parentElement) {
|
||||
const parentText = textOf(parent);
|
||||
const labelText = parentText
|
||||
.replace(new RegExp(`(^|\\s)${countText.replace(/,/g, '\\,')}(\\s|$)`), ' ')
|
||||
.replace(/[((]\s*[\d,]+\s*[))]/g, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
if (labelText && labelText !== parentText && !/^\s*[\d,]+\s*$/.test(labelText)) {
|
||||
return {
|
||||
index,
|
||||
raw_text: '',
|
||||
label_text: labelText,
|
||||
quantity_text: countText,
|
||||
attrs: attrsOf(parent),
|
||||
visible: isVisible(parent),
|
||||
outerHTML: (parent.outerHTML || '').slice(0, 1200),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const regionRows = allElements().filter((el) => isVisible(el) && !insideListingRow(el));
|
||||
|
||||
const byText = new Map();
|
||||
regionRows
|
||||
.forEach((el, index) => {
|
||||
const row = rowOf(el, index);
|
||||
const combined = `${row.raw_text} ${row.label_text} ${row.quantity_text}`;
|
||||
if (row.raw_text.length > 200) return;
|
||||
if (/[((]\s*[\d,]+\s*[))]/.test(combined)) {
|
||||
const key = combined;
|
||||
if (!byText.has(key)) byText.set(key, row);
|
||||
return;
|
||||
}
|
||||
|
||||
const splitRow = splitCountRow(el, index);
|
||||
if (splitRow) {
|
||||
const key = `${splitRow.label_text} ${splitRow.quantity_text}`;
|
||||
if (!byText.has(key)) byText.set(key, splitRow);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
region: {
|
||||
url: window.location.href,
|
||||
title: document.title,
|
||||
},
|
||||
rows: Array.from(byText.values()).filter((row) => {
|
||||
const combined = `${row.raw_text} ${row.label_text}`.toLowerCase();
|
||||
return !/所有商品|启售商品|改善商品信息/.test(combined);
|
||||
}),
|
||||
candidates: Array.from(byText.values()).slice(0, 80),
|
||||
};
|
||||
@@ -0,0 +1,89 @@
|
||||
function textOf(node) {
|
||||
return ((node && (node.innerText || node.textContent || '')) || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsText(el) {
|
||||
if (!el || !el.attributes) return '';
|
||||
return Array.from(el.attributes).map((attr) => `${attr.name}=${attr.value}`).join(' ');
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
if ((node.tagName || '').toLowerCase() === 'slot' && node.assignedElements) {
|
||||
for (const assigned of node.assignedElements({ flatten: true })) {
|
||||
walkRoots(assigned, rows, seen);
|
||||
}
|
||||
}
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements(root) {
|
||||
const rows = [];
|
||||
walkRoots(root || document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function clickElement(el) {
|
||||
el.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
if (el.focus) el.focus();
|
||||
for (const eventName of ['pointerdown', 'mousedown', 'pointerup', 'mouseup']) {
|
||||
el.dispatchEvent(new MouseEvent(eventName, { bubbles: true, cancelable: true, view: window }));
|
||||
}
|
||||
el.click();
|
||||
}
|
||||
|
||||
function clickTargetFor(el) {
|
||||
if (el && el.shadowRoot) {
|
||||
const button = el.shadowRoot.querySelector('button:not([disabled]), [role="button"]:not([disabled])');
|
||||
if (button) return button;
|
||||
}
|
||||
return el;
|
||||
}
|
||||
|
||||
const modal = document.querySelector('kat-modal[data-testid="action-modal"]') ||
|
||||
allElements(document).find((el) => /删除|Delete/i.test(textOf(el)) && (el.tagName || '').toLowerCase() === 'kat-modal');
|
||||
|
||||
if (!modal) return { ok: false, reason: 'delete modal not found' };
|
||||
|
||||
const buttons = allElements(modal).filter((el) => {
|
||||
const haystack = `${el.tagName || ''} ${attrsText(el)} ${textOf(el)}`;
|
||||
return isVisible(el) && /kat-button|button/i.test(el.tagName || '') && /variant=primary|删除|确认|Delete|Confirm/i.test(haystack);
|
||||
});
|
||||
const button = buttons[0];
|
||||
|
||||
if (!button) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'confirm button not found',
|
||||
modalText: textOf(modal),
|
||||
modalHTML: (modal.outerHTML || '').slice(0, 2000),
|
||||
};
|
||||
}
|
||||
|
||||
const target = clickTargetFor(button);
|
||||
clickElement(target);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
clicked: {
|
||||
tag: button.tagName,
|
||||
text: textOf(button),
|
||||
attrs: attrsText(button),
|
||||
targetTag: target.tagName,
|
||||
},
|
||||
};
|
||||
46
app/amazon/scripts/patrol_delete/delete_success_messages.js
Normal file
46
app/amazon/scripts/patrol_delete/delete_success_messages.js
Normal file
@@ -0,0 +1,46 @@
|
||||
function textOf(node) {
|
||||
return ((node && (node.innerText || node.textContent || '')) || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsText(el) {
|
||||
if (!el || !el.attributes) return '';
|
||||
return Array.from(el.attributes).map((attr) => `${attr.name}=${attr.value}`).join(' ');
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
if ((node.tagName || '').toLowerCase() === 'slot' && node.assignedElements) {
|
||||
for (const assigned of node.assignedElements({ flatten: true })) {
|
||||
walkRoots(assigned, rows, seen);
|
||||
}
|
||||
}
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
|
||||
const seen = new Set();
|
||||
return rows
|
||||
.filter((el) => {
|
||||
const haystack = `${el.tagName || ''} ${attrsText(el)} ${textOf(el)}`;
|
||||
return isVisible(el) && /kat-alert|alert|toast|notification|已删除|已经删除|15\s*分钟|success/i.test(haystack);
|
||||
})
|
||||
.map((el) => textOf(el) || attrsText(el))
|
||||
.filter((text) => text && !seen.has(text) && seen.add(text))
|
||||
.slice(0, 30);
|
||||
187
app/amazon/scripts/patrol_delete/inventory_delete_selected.js
Normal file
187
app/amazon/scripts/patrol_delete/inventory_delete_selected.js
Normal file
@@ -0,0 +1,187 @@
|
||||
function textOf(node) {
|
||||
return ((node && (node.innerText || node.textContent || '')) || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsText(el) {
|
||||
if (!el || !el.attributes) return '';
|
||||
return Array.from(el.attributes).map((attr) => `${attr.name}=${attr.value}`).join(' ');
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
if ((node.tagName || '').toLowerCase() === 'slot' && node.assignedElements) {
|
||||
for (const assigned of node.assignedElements({ flatten: true })) {
|
||||
walkRoots(assigned, rows, seen);
|
||||
}
|
||||
}
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements(root) {
|
||||
const rows = [];
|
||||
walkRoots(root || document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function closestDeep(el, selector) {
|
||||
let node = el;
|
||||
while (node) {
|
||||
if (node.closest) {
|
||||
const found = node.closest(selector);
|
||||
if (found) return found;
|
||||
}
|
||||
const root = node.getRootNode && node.getRootNode();
|
||||
node = root && root.host ? root.host : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function insideInventoryRow(el) {
|
||||
return Boolean(closestDeep(el, 'div[data-sku]'));
|
||||
}
|
||||
|
||||
function isDeleteAction(el) {
|
||||
const haystack = `${el.tagName || ''} ${attrsText(el)} ${textOf(el)}`;
|
||||
return /data-action=DeleteListing|action=DeleteListing|删除商品信息|删除商品和报价|DeleteListing|delete\s+listing/i.test(haystack);
|
||||
}
|
||||
|
||||
function clickElement(el) {
|
||||
el.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
if (el.focus) el.focus();
|
||||
for (const eventName of ['pointerdown', 'mousedown', 'pointerup', 'mouseup']) {
|
||||
el.dispatchEvent(new MouseEvent(eventName, { bubbles: true, cancelable: true, view: window }));
|
||||
}
|
||||
el.click();
|
||||
}
|
||||
|
||||
function hostDropdownOf(el) {
|
||||
const host = closestDeep(el, 'kat-dropdown-button');
|
||||
return host || ((el.tagName || '').toLowerCase() === 'kat-dropdown-button' ? el : null);
|
||||
}
|
||||
|
||||
function dropdownToggleOf(dropdown, fallback) {
|
||||
if (!dropdown) return fallback;
|
||||
if (fallback && textOf(fallback)) return fallback;
|
||||
return fallback || dropdown;
|
||||
}
|
||||
|
||||
function openBulkActionMenu(candidate) {
|
||||
const dropdown = hostDropdownOf(candidate);
|
||||
const toggle = dropdownToggleOf(dropdown, candidate);
|
||||
clickElement(toggle);
|
||||
return { dropdown, toggle };
|
||||
}
|
||||
|
||||
function summarize(el) {
|
||||
return {
|
||||
tag: el.tagName,
|
||||
text: textOf(el),
|
||||
attrs: attrsText(el),
|
||||
visible: isVisible(el),
|
||||
outerHTML: (el.outerHTML || '').slice(0, 1000),
|
||||
};
|
||||
}
|
||||
|
||||
function findVisibleBulkDeleteAction() {
|
||||
return allElements(document).find((el) => isDeleteAction(el) && isVisible(el) && !insideInventoryRow(el));
|
||||
}
|
||||
|
||||
function findDeleteActionAfterMenuOpen(candidate) {
|
||||
const dropdown = hostDropdownOf(candidate) || candidate;
|
||||
const exactMenuButton = allElements(dropdown).find((child) => {
|
||||
const tag = (child.tagName || '').toLowerCase();
|
||||
const role = child.getAttribute && child.getAttribute('role');
|
||||
return tag === 'button' && role === 'menuitem' && isDeleteAction(child);
|
||||
});
|
||||
if (exactMenuButton) return { mode: 'bulk-action-menuitem-delete', el: exactMenuButton };
|
||||
|
||||
const nestedDeleteAction = allElements(dropdown).find((child) => isDeleteAction(child) && isVisible(child));
|
||||
if (nestedDeleteAction) return { mode: 'bulk-action-nested-delete', el: nestedDeleteAction };
|
||||
|
||||
const deleteAction = findVisibleBulkDeleteAction();
|
||||
if (deleteAction) return { mode: 'bulk-action-visible-delete', el: deleteAction };
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function isBulkActionCandidate(el) {
|
||||
const tag = (el.tagName || '').toLowerCase();
|
||||
if (!/^(button|kat-button|kat-dropdown-button|kat-select|kat-popover|kat-menu-button)$/.test(tag)) return false;
|
||||
if (!isVisible(el) || insideInventoryRow(el)) return false;
|
||||
const haystack = `${tag} ${attrsText(el)} ${textOf(el)}`;
|
||||
return /DeleteListing|删除商品信息|删除|操作|更多|Action|Actions|selected|选定|已选择|批量/i.test(haystack);
|
||||
}
|
||||
|
||||
const directDelete = findVisibleBulkDeleteAction();
|
||||
if (directDelete) {
|
||||
clickElement(directDelete);
|
||||
return { ok: true, mode: 'direct-delete-button', clicked: summarize(directDelete) };
|
||||
}
|
||||
|
||||
const rows = allElements(document);
|
||||
const candidates = rows.filter(isBulkActionCandidate);
|
||||
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
const openResult = openBulkActionMenu(candidate);
|
||||
const deleteAction = findDeleteActionAfterMenuOpen(candidate);
|
||||
if (deleteAction) {
|
||||
clickElement(deleteAction.el);
|
||||
return {
|
||||
ok: true,
|
||||
mode: deleteAction.mode,
|
||||
candidate: summarize(candidate),
|
||||
opened: {
|
||||
dropdown: openResult.dropdown ? summarize(openResult.dropdown) : null,
|
||||
toggle: openResult.toggle ? summarize(openResult.toggle) : null,
|
||||
},
|
||||
clicked: summarize(deleteAction.el),
|
||||
};
|
||||
}
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'bulk action menu opened; delete action not visible yet',
|
||||
menuOpened: true,
|
||||
candidate: summarize(candidate),
|
||||
opened: {
|
||||
dropdown: openResult.dropdown ? summarize(openResult.dropdown) : null,
|
||||
toggle: openResult.toggle ? summarize(openResult.toggle) : null,
|
||||
},
|
||||
deleteLikeElements: allElements(document)
|
||||
.filter((el) => /DeleteListing|删除商品信息|删除商品和报价|delete\s+listing/i.test(`${el.tagName || ''} ${attrsText(el)} ${textOf(el)}`) && !insideInventoryRow(el))
|
||||
.slice(0, 20)
|
||||
.map(summarize),
|
||||
};
|
||||
} catch (error) {
|
||||
// Try the next non-row bulk action control.
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'bulk delete action not found',
|
||||
candidates: candidates.slice(0, 20).map(summarize),
|
||||
deleteLikeElements: rows
|
||||
.filter((el) => /DeleteListing|删除商品信息|删除商品和报价|delete\s+listing/i.test(`${el.tagName || ''} ${attrsText(el)} ${textOf(el)}`) && !insideInventoryRow(el))
|
||||
.slice(0, 20)
|
||||
.map(summarize),
|
||||
visibleButtons: rows
|
||||
.filter((el) => /^(button|kat-button|kat-dropdown-button)$/i.test(el.tagName || '') && isVisible(el))
|
||||
.slice(0, 20)
|
||||
.map(summarize),
|
||||
};
|
||||
71
app/amazon/scripts/patrol_delete/inventory_select_all.js
Normal file
71
app/amazon/scripts/patrol_delete/inventory_select_all.js
Normal file
@@ -0,0 +1,71 @@
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function insideInventoryRow(el) {
|
||||
return Boolean(el && el.closest && el.closest('div[data-sku]'));
|
||||
}
|
||||
|
||||
function clickTargetFor(checkbox) {
|
||||
if (!checkbox) return null;
|
||||
if (checkbox.shadowRoot) {
|
||||
const shadowTarget = checkbox.shadowRoot.querySelector('[role="checkbox"], .checkbox');
|
||||
if (shadowTarget) return shadowTarget;
|
||||
}
|
||||
return checkbox;
|
||||
}
|
||||
|
||||
const rowCount = document.querySelectorAll('div[data-sku]').length;
|
||||
if (rowCount <= 0) {
|
||||
return { ok: false, reason: 'no inventory rows after status filter', rowCount };
|
||||
}
|
||||
|
||||
const checkboxes = allElements()
|
||||
.filter((el) => (el.tagName || '').toLowerCase() === 'kat-checkbox' && isVisible(el))
|
||||
.sort((a, b) => {
|
||||
const rectA = a.getBoundingClientRect();
|
||||
const rectB = b.getBoundingClientRect();
|
||||
return rectA.top - rectB.top || rectA.left - rectB.left;
|
||||
});
|
||||
|
||||
const headerCheckbox = checkboxes.find((el) => !insideInventoryRow(el)) || checkboxes[0];
|
||||
if (!headerCheckbox) {
|
||||
return { ok: false, reason: 'select-all checkbox not found', rowCount, checkboxCount: checkboxes.length };
|
||||
}
|
||||
|
||||
const target = clickTargetFor(headerCheckbox);
|
||||
target.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
target.click();
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
rowCount,
|
||||
checkboxCount: checkboxes.length,
|
||||
clicked: {
|
||||
tag: headerCheckbox.tagName,
|
||||
insideInventoryRow: insideInventoryRow(headerCheckbox),
|
||||
outerHTML: (headerCheckbox.outerHTML || '').slice(0, 1000),
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,124 @@
|
||||
function textOf(node) {
|
||||
return ((node && (node.innerText || node.textContent || '')) || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function normalize(text) {
|
||||
return String(text || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function parseStatus(rawText) {
|
||||
const text = normalize(rawText);
|
||||
if (!text) return '';
|
||||
const match = text.match(/^(.*?)\s*[((]\s*[\d,]+\s*[))]\s*$/);
|
||||
return normalize(match ? match[1] : text);
|
||||
}
|
||||
|
||||
function collectStatusTargets() {
|
||||
const pattern = /(全部|在搜索结果中禁止显示|配送问题|缺少报价|已停售|详情页面已删除|订单页面已删除|商品信息草稿|缺少的信息|定价问题|需要批准|不可售|在售)\s*[((]?\s*[\d,]*\s*[))]?/g;
|
||||
const hits = [];
|
||||
|
||||
for (const el of allElements()) {
|
||||
if (!isVisible(el)) continue;
|
||||
const text = normalize(textOf(el));
|
||||
if (!text) continue;
|
||||
const match = text.match(pattern);
|
||||
if (!match) continue;
|
||||
const parsed = parseStatus(match[0]);
|
||||
hits.push({
|
||||
el,
|
||||
text,
|
||||
parsed,
|
||||
tag: el.tagName,
|
||||
clickable: Boolean(el.onclick) || el.matches('button,[role="button"],kat-option,kat-label,a,[tabindex]'),
|
||||
});
|
||||
}
|
||||
|
||||
return hits;
|
||||
}
|
||||
|
||||
const targetStatus = normalize((__crawlerPayload || {}).status);
|
||||
const candidates = collectStatusTargets()
|
||||
.filter((item) => item.parsed === targetStatus || item.text.includes(targetStatus))
|
||||
.map((item) => ({
|
||||
...item,
|
||||
score:
|
||||
(item.parsed === targetStatus ? 300 : 0) +
|
||||
(item.clickable ? 120 : 0) +
|
||||
(/^[^ ]+\s*[((]\s*[\d,]+\s*[))]$/.test(item.text) ? 80 : 0) +
|
||||
(/button|option|label/i.test(item.tag) ? 30 : 0),
|
||||
}))
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
if (!candidates.length) {
|
||||
return { ok: false, reason: 'visible status target not found', targetStatus };
|
||||
}
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const chain = [candidate.el];
|
||||
let node = candidate.el.parentElement;
|
||||
for (let depth = 0; node && depth < 5; depth += 1, node = node.parentElement) {
|
||||
chain.push(node);
|
||||
}
|
||||
|
||||
for (const el of chain) {
|
||||
try {
|
||||
el.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
el.click();
|
||||
return {
|
||||
ok: true,
|
||||
targetStatus,
|
||||
clicked: {
|
||||
tag: el.tagName,
|
||||
text: normalize(textOf(el)).slice(0, 300),
|
||||
},
|
||||
matched: {
|
||||
tag: candidate.tag,
|
||||
text: candidate.text.slice(0, 300),
|
||||
parsed: candidate.parsed,
|
||||
score: candidate.score,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
// try parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'visible status target click failed',
|
||||
targetStatus,
|
||||
candidates: candidates.slice(0, 10).map((item) => ({
|
||||
tag: item.tag,
|
||||
text: item.text.slice(0, 300),
|
||||
parsed: item.parsed,
|
||||
score: item.score,
|
||||
clickable: item.clickable,
|
||||
})),
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', code: 'Escape', bubbles: true }));
|
||||
document.body.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
|
||||
return true;
|
||||
@@ -0,0 +1,95 @@
|
||||
const keywords = ['ListingStatus', 'listing status', '商品状态', '商品狀態', 'status', 'filter', '库存'];
|
||||
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function nearestLabel(el) {
|
||||
const parts = [];
|
||||
let node = el;
|
||||
for (let depth = 0; node && depth < 6; depth += 1, node = node.parentElement) {
|
||||
const text = textOf(node);
|
||||
if (text) parts.push(text.slice(0, 400));
|
||||
}
|
||||
const id = el.getAttribute('id');
|
||||
if (id) {
|
||||
document.querySelectorAll(`label[for="${CSS.escape(id)}"]`).forEach((label) => {
|
||||
const text = textOf(label);
|
||||
if (text) parts.unshift(text);
|
||||
});
|
||||
}
|
||||
return [...new Set(parts)].join(' | ');
|
||||
}
|
||||
|
||||
function hasKeyword(text) {
|
||||
return keywords.some((keyword) => text.toLowerCase().includes(keyword.toLowerCase()));
|
||||
}
|
||||
|
||||
function candidateRow(el, index) {
|
||||
const attrs = attrsOf(el);
|
||||
const attrText = Object.entries(attrs).map(([k, v]) => `${k}=${v}`).join(' ');
|
||||
const label = nearestLabel(el);
|
||||
const text = textOf(el);
|
||||
const haystack = `${el.tagName} ${attrText} ${text} ${label}`;
|
||||
let score = 0;
|
||||
if (/商品状态|商品狀態|ListingStatus|listing status/i.test(haystack)) score += 100;
|
||||
if (/status/i.test(haystack)) score += 25;
|
||||
if (/filter|库存/i.test(haystack)) score += 10;
|
||||
if (isVisible(el)) score += 5;
|
||||
if (el.closest('[data-sku], tr, [role="row"]')) score -= 50;
|
||||
return {
|
||||
index,
|
||||
score,
|
||||
tag: el.tagName.toLowerCase(),
|
||||
visible: isVisible(el),
|
||||
text,
|
||||
label,
|
||||
attrs,
|
||||
outerHTML: (el.outerHTML || '').slice(0, 2000),
|
||||
};
|
||||
}
|
||||
|
||||
const rows = allElements();
|
||||
const candidates = [];
|
||||
rows.forEach((el, index) => {
|
||||
const tag = (el.tagName || '').toLowerCase();
|
||||
const haystack = `${tag} ${textOf(el)} ${Object.values(attrsOf(el)).join(' ')} ${nearestLabel(el)}`;
|
||||
if (tag === 'kat-dropdown' || tag === 'kat-option' || hasKeyword(haystack)) {
|
||||
candidates.push(candidateRow(el, index));
|
||||
}
|
||||
});
|
||||
|
||||
return candidates.sort((a, b) => b.score - a.score).slice(0, 80);
|
||||
295
app/amazon/scripts/patrol_delete/listing_status_dropdown_open.js
Normal file
295
app/amazon/scripts/patrol_delete/listing_status_dropdown_open.js
Normal file
@@ -0,0 +1,295 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function normalize(text) {
|
||||
return String(text || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsText(el) {
|
||||
if (!el || !el.attributes) return '';
|
||||
return Array.from(el.attributes).map((attr) => `${attr.name}=${attr.value}`).join(' ');
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isDisabled(el) {
|
||||
if (!el) return false;
|
||||
return (
|
||||
el.hasAttribute('disabled') ||
|
||||
el.disabled === true ||
|
||||
el.getAttribute('aria-disabled') === 'true' ||
|
||||
/\bdisabled\b/i.test(el.getAttribute('class') || '')
|
||||
);
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function nearestLabel(el) {
|
||||
const parts = [];
|
||||
let node = el;
|
||||
for (let depth = 0; node && depth < 7; depth += 1, node = node.parentElement) {
|
||||
const text = textOf(node);
|
||||
if (text) parts.push(text.slice(0, 500));
|
||||
}
|
||||
const id = el.getAttribute('id');
|
||||
if (id) {
|
||||
document.querySelectorAll(`label[for="${CSS.escape(id)}"]`).forEach((label) => {
|
||||
const text = textOf(label);
|
||||
if (text) parts.unshift(text);
|
||||
});
|
||||
}
|
||||
return [...new Set(parts)].join(' | ');
|
||||
}
|
||||
|
||||
function optionTextOf(dropdown) {
|
||||
const roots = [dropdown];
|
||||
if (dropdown.shadowRoot) roots.push(dropdown.shadowRoot);
|
||||
const texts = [];
|
||||
for (const root of roots) {
|
||||
for (const option of Array.from(root.querySelectorAll('kat-option, kat-label, [role="option"], [class*="parentKatOptionStyle"]'))) {
|
||||
texts.push(`${textOf(option)} ${attrsText(option)}`);
|
||||
}
|
||||
}
|
||||
return normalize(texts.join(' '));
|
||||
}
|
||||
|
||||
function isSearchFieldDropdown(text) {
|
||||
return /(^|\s)(SKUS?|ASIN|FNSKU|UPC\/EAN|TITLE_KEYWORD|GTIN)(\s|$)|商品名称\/关键字|data-value=(SKU|ASIN|FNSKU|TITLE_KEYWORD|GTIN)|value=(SKU|ASIN|FNSKU|TITLE_KEYWORD|GTIN)/i.test(text);
|
||||
}
|
||||
|
||||
function statusHitCount(text) {
|
||||
const matches = normalize(text).match(
|
||||
/(全部|在售|不可售|详情页面已删除|订单页面已删除|商品信息草稿|缺少的信息|定价问题|需要批准|配送问题|缺少报价|已停售|在搜索结果中禁止显示)\s*(?:[((]\s*[\d,]+\s*[))])?/g
|
||||
);
|
||||
return matches ? matches.length : 0;
|
||||
}
|
||||
|
||||
function scoreDropdown(el) {
|
||||
const text = textOf(el);
|
||||
const label = nearestLabel(el);
|
||||
const attrs = attrsText(el);
|
||||
const optionText = optionTextOf(el);
|
||||
const haystack = `${el.tagName} ${attrs} ${text} ${label} ${optionText}`;
|
||||
const statusHits = statusHitCount(`${text} ${label} ${optionText}`);
|
||||
const isStatusDropdown = /statusDropdown|商品状态|商品狀態|ListingStatus|listing status/i.test(haystack);
|
||||
let score = 0;
|
||||
if (isSearchFieldDropdown(haystack)) score -= 1000;
|
||||
if (isStatusDropdown) score += 500;
|
||||
if (/商品状态|商品狀態|ListingStatus|listing status/i.test(haystack)) score += 120;
|
||||
if (/\bstatus\b|状态/i.test(haystack)) score += 25;
|
||||
if (/filter|筛选|筛選|库存/i.test(haystack)) score += 10;
|
||||
if (/全部\s*[((]\s*[\d,]+\s*[))]/.test(`${text} ${label} ${optionText}`)) score += 300;
|
||||
if (statusHits >= 2) score += statusHits * 180;
|
||||
if (!isStatusDropdown && statusHits === 0) score -= 200;
|
||||
if (isVisible(el)) score += 10;
|
||||
if (el.closest('[data-sku], tr, [role="row"], [data-testid*="row"]')) score -= 80;
|
||||
if (isDisabled(el)) score -= 20;
|
||||
return { score, text, label, haystack, statusHits, disabled: isDisabled(el) };
|
||||
}
|
||||
|
||||
function candidateTriggers(dropdown) {
|
||||
const selectors = [
|
||||
'kat-dropdown-button',
|
||||
'kat-button',
|
||||
'button',
|
||||
'[role="button"]',
|
||||
'[aria-haspopup]',
|
||||
'[part="trigger"]',
|
||||
'[part="button"]',
|
||||
'.header',
|
||||
'.trigger',
|
||||
'.button',
|
||||
'span',
|
||||
'div',
|
||||
];
|
||||
|
||||
const seen = new Set();
|
||||
const triggers = [];
|
||||
const roots = [dropdown];
|
||||
if (dropdown.shadowRoot) roots.push(dropdown.shadowRoot);
|
||||
|
||||
for (const root of roots) {
|
||||
for (const selector of selectors) {
|
||||
for (const el of Array.from(root.querySelectorAll(selector))) {
|
||||
if (!el || seen.has(el)) continue;
|
||||
seen.add(el);
|
||||
const text = textOf(el);
|
||||
const attrs = attrsText(el);
|
||||
const score =
|
||||
(isVisible(el) ? 20 : 0) +
|
||||
(/全部\s*[((]\s*[\d,]+\s*[))]/.test(text) ? 200 : 0) +
|
||||
(/商品状态|商品狀態|ListingStatus|listing status/i.test(`${text} ${attrs}`) ? 120 : 0) +
|
||||
(/button|trigger|toggle|dropdown|expand/i.test(`${el.tagName} ${attrs}`) ? 40 : 0);
|
||||
triggers.push({ el, text, attrs, score });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return triggers
|
||||
.filter((item) => item.score > 0)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
}
|
||||
|
||||
function clickTrigger(dropdown) {
|
||||
const triggers = candidateTriggers(dropdown);
|
||||
for (const trigger of triggers) {
|
||||
try {
|
||||
trigger.el.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
trigger.el.click();
|
||||
return {
|
||||
ok: true,
|
||||
trigger: {
|
||||
tag: trigger.el.tagName,
|
||||
text: trigger.text,
|
||||
attrs: trigger.attrs,
|
||||
score: trigger.score,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
// continue
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
dropdown.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
dropdown.click();
|
||||
return {
|
||||
ok: true,
|
||||
trigger: {
|
||||
tag: dropdown.tagName,
|
||||
text: textOf(dropdown),
|
||||
attrs: attrsText(dropdown),
|
||||
score: -1,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: String(error),
|
||||
tried: triggers.slice(0, 10).map((item) => ({
|
||||
tag: item.el.tagName,
|
||||
text: item.text,
|
||||
attrs: item.attrs,
|
||||
score: item.score,
|
||||
})),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const dropdowns = allElements().filter((el) => el.tagName.toLowerCase() === 'kat-dropdown');
|
||||
const ranked = dropdowns
|
||||
.map((el, index) => ({ el, index, ...scoreDropdown(el) }))
|
||||
.filter((row) => row.score > 0)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
if (!ranked.length) {
|
||||
return { ok: false, reason: 'listing status kat-dropdown not found', candidates: [] };
|
||||
}
|
||||
|
||||
const selected = ranked[0];
|
||||
if (selected.disabled) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'listing status dropdown disabled/loading',
|
||||
selected: {
|
||||
index: selected.index,
|
||||
score: selected.score,
|
||||
statusHits: selected.statusHits,
|
||||
text: selected.text,
|
||||
label: selected.label,
|
||||
attrs: attrsOf(selected.el),
|
||||
outerHTML: (selected.el.outerHTML || '').slice(0, 2000),
|
||||
},
|
||||
candidates: ranked.slice(0, 10).map((row) => ({
|
||||
index: row.index,
|
||||
score: row.score,
|
||||
statusHits: row.statusHits,
|
||||
disabled: row.disabled,
|
||||
text: row.text,
|
||||
label: row.label,
|
||||
attrs: attrsText(row.el),
|
||||
outerHTML: (row.el.outerHTML || '').slice(0, 1000),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
selected.el.setAttribute('data-crawler-listing-status-dropdown', 'true');
|
||||
const clickResult = clickTrigger(selected.el);
|
||||
|
||||
if (!clickResult.ok) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'listing status dropdown trigger click failed',
|
||||
selected: {
|
||||
index: selected.index,
|
||||
score: selected.score,
|
||||
text: selected.text,
|
||||
label: selected.label,
|
||||
attrs: attrsText(selected.el),
|
||||
outerHTML: (selected.el.outerHTML || '').slice(0, 2000),
|
||||
},
|
||||
clickResult,
|
||||
candidates: ranked.slice(0, 10).map((row) => ({
|
||||
index: row.index,
|
||||
score: row.score,
|
||||
text: row.text,
|
||||
label: row.label,
|
||||
attrs: attrsText(row.el),
|
||||
outerHTML: (row.el.outerHTML || '').slice(0, 1000),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
selected: {
|
||||
index: selected.index,
|
||||
score: selected.score,
|
||||
statusHits: selected.statusHits,
|
||||
text: selected.text,
|
||||
label: selected.label,
|
||||
attrs: attrsText(selected.el),
|
||||
outerHTML: (selected.el.outerHTML || '').slice(0, 2000),
|
||||
},
|
||||
trigger: clickResult.trigger,
|
||||
candidates: ranked.slice(0, 10).map((row) => ({
|
||||
index: row.index,
|
||||
score: row.score,
|
||||
statusHits: row.statusHits,
|
||||
disabled: row.disabled,
|
||||
text: row.text,
|
||||
label: row.label,
|
||||
attrs: attrsText(row.el),
|
||||
outerHTML: (row.el.outerHTML || '').slice(0, 1000),
|
||||
})),
|
||||
};
|
||||
@@ -0,0 +1,129 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function findMarkedDropdown() {
|
||||
return allElements().find((el) => el.getAttribute('data-crawler-listing-status-dropdown') === 'true') || null;
|
||||
}
|
||||
|
||||
function insideListingRow(el) {
|
||||
return Boolean(el.closest('[data-sku], tr, [role="row"], [data-testid*="row"]'));
|
||||
}
|
||||
|
||||
function normalize(text) {
|
||||
return String(text || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function isOptionLike(el) {
|
||||
if (!el || !el.tagName) return false;
|
||||
const tag = el.tagName.toLowerCase();
|
||||
const role = el.getAttribute('role');
|
||||
const className = el.getAttribute('class') || '';
|
||||
return (
|
||||
tag === 'kat-option' ||
|
||||
tag === 'kat-label' ||
|
||||
role === 'option' ||
|
||||
/parentKatOptionStyle/i.test(className)
|
||||
);
|
||||
}
|
||||
|
||||
function parseStatus(rawText) {
|
||||
const text = normalize(rawText);
|
||||
if (!text) return '';
|
||||
const match = text.match(/^(.*?)\s*[((]\s*[\d,]+\s*[))]\s*$/);
|
||||
return normalize(match ? match[1] : text);
|
||||
}
|
||||
|
||||
function isListingStatusOption(row) {
|
||||
const text = normalize(`${row.raw_text} ${row.value || ''}`);
|
||||
const parsed = parseStatus(row.raw_text);
|
||||
const haystack = normalize(`${text} ${parsed}`);
|
||||
if (/^(所有|SKUS?|ASIN|FNSKU|UPC\/EAN|商品名称\/关键字)$|^(ALL|SKU|ASIN|FNSKU|TITLE_KEYWORD|GTIN)$/i.test(haystack)) {
|
||||
return false;
|
||||
}
|
||||
return /^(全部|在售|不可售|详情页面已删除|订单页面已删除|商品信息草稿|缺少的信息|定价问题|需要批准|配送问题|缺少报价|已停售|在搜索结果中禁止显示)$/.test(parsed);
|
||||
}
|
||||
|
||||
function optionRow(el, index) {
|
||||
const attrs = attrsOf(el);
|
||||
const rawText = textOf(el) || attrs.label || attrs.text || attrs.value || '';
|
||||
return {
|
||||
index,
|
||||
raw_text: rawText,
|
||||
value: attrs.value || attrs['data-value'] || attrs.name || null,
|
||||
tag: (el.tagName || '').toLowerCase(),
|
||||
attrs,
|
||||
visible: isVisible(el),
|
||||
outerHTML: (el.outerHTML || '').slice(0, 1200),
|
||||
};
|
||||
}
|
||||
|
||||
const markedDropdown = findMarkedDropdown();
|
||||
let scoped = [];
|
||||
if (markedDropdown) {
|
||||
const roots = [markedDropdown];
|
||||
if (markedDropdown.shadowRoot) roots.push(markedDropdown.shadowRoot);
|
||||
for (const root of roots) {
|
||||
scoped = scoped.concat(Array.from(root.querySelectorAll('kat-option, kat-label, [role="option"], [class*="parentKatOptionStyle"]')));
|
||||
}
|
||||
}
|
||||
|
||||
const allOptions = allElements().filter((el) => {
|
||||
if (!isOptionLike(el)) return false;
|
||||
if (insideListingRow(el)) return false;
|
||||
return isVisible(el);
|
||||
});
|
||||
|
||||
const byNode = new Map();
|
||||
const byStatus = new Map();
|
||||
scoped.concat(allOptions).forEach((el, index) => {
|
||||
if (!byNode.has(el)) {
|
||||
const row = optionRow(el, index);
|
||||
if (isListingStatusOption(row)) {
|
||||
byNode.set(el, row);
|
||||
const parsed = parseStatus(row.raw_text);
|
||||
const hasValue = Boolean(row.value);
|
||||
const hasQuantity = /[((]\s*[\d,]+\s*[))]/.test(row.raw_text);
|
||||
const quality = (hasValue ? 2 : 0) + (hasQuantity ? 1 : 0);
|
||||
const existing = byStatus.get(parsed);
|
||||
if (!existing || quality > existing.quality) {
|
||||
byStatus.set(parsed, { row, quality });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Array.from(byStatus.values()).map((item) => item.row).filter((row) => row.raw_text);
|
||||
91
app/amazon/scripts/patrol_delete/listing_status_section.js
Normal file
91
app/amazon/scripts/patrol_delete/listing_status_section.js
Normal file
@@ -0,0 +1,91 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function extractStatuses(text) {
|
||||
const pattern = /(全部|在搜索结果中禁止显示|配送问题|缺少报价|已停售|详情页面已删除|订单页面已删除|商品信息草稿|缺少的信息|定价问题|需要批准|不可售|在售)\s*(?:[((]\s*([\d,]+)\s*[))])?/g;
|
||||
const rows = [];
|
||||
let match;
|
||||
while ((match = pattern.exec(text)) !== null) {
|
||||
rows.push({
|
||||
status: match[1],
|
||||
quantity: match[2] || '',
|
||||
raw_text: match[0].trim(),
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
function parseDropdown(dropdown, index) {
|
||||
const nodes = Array.from(dropdown.querySelectorAll('kat-option, kat-label, [class*="parentKatOptionStyle"]'));
|
||||
const seen = new Set();
|
||||
const rows = nodes
|
||||
.flatMap((node) => {
|
||||
const rawText = textOf(node);
|
||||
return extractStatuses(rawText).map((parsed) => ({
|
||||
...parsed,
|
||||
value: node.getAttribute('value') || '',
|
||||
}));
|
||||
})
|
||||
.filter((row) => {
|
||||
const key = `${row.status}::${row.quantity}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
|
||||
const quantityCount = rows.filter((row) => row.quantity).length;
|
||||
const rawText = textOf(dropdown);
|
||||
const className = String(dropdown.className || '');
|
||||
const isStatusDropdown = /statusDropdown/i.test(className);
|
||||
const score =
|
||||
rows.length * 100 +
|
||||
quantityCount * 50 +
|
||||
(isStatusDropdown ? 500 : 0) +
|
||||
(/在搜索结果中禁止显示|需要批准|详情页面已删除/.test(rawText) ? 200 : 0);
|
||||
|
||||
return {
|
||||
index,
|
||||
tag: (dropdown.tagName || '').toLowerCase(),
|
||||
raw_text: rawText,
|
||||
rows,
|
||||
rowCount: rows.length,
|
||||
quantityCount,
|
||||
score,
|
||||
className,
|
||||
outerHTML: (dropdown.outerHTML || '').slice(0, 1200),
|
||||
};
|
||||
}
|
||||
|
||||
const ranked = allElements()
|
||||
.map((el, index) => ({ el, index }))
|
||||
.filter(({ el }) => (el.tagName || '').toLowerCase() === 'kat-dropdown')
|
||||
.map(({ el, index }) => parseDropdown(el, index))
|
||||
.filter((row) => row.rowCount >= 3)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
const best = ranked[0];
|
||||
return {
|
||||
rows: best ? best.rows : [],
|
||||
selected: best || null,
|
||||
candidates: ranked.slice(0, 10),
|
||||
};
|
||||
148
app/amazon/scripts/patrol_delete/listing_status_select_option.js
Normal file
148
app/amazon/scripts/patrol_delete/listing_status_select_option.js
Normal file
@@ -0,0 +1,148 @@
|
||||
const __crawlerPayload = arguments[0] || {};
|
||||
|
||||
function textOf(node) {
|
||||
return ((node && (node.innerText || node.textContent || '')) || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function findMarkedDropdown() {
|
||||
return allElements().find((el) => el.getAttribute('data-crawler-listing-status-dropdown') === 'true') || null;
|
||||
}
|
||||
|
||||
function insideListingRow(el) {
|
||||
return Boolean(el.closest('[data-sku], tr, [role="row"], [data-testid*="row"]'));
|
||||
}
|
||||
|
||||
function isOptionLike(el) {
|
||||
if (!el || !el.tagName) return false;
|
||||
const tag = el.tagName.toLowerCase();
|
||||
const role = el.getAttribute('role');
|
||||
const className = el.getAttribute('class') || '';
|
||||
return (
|
||||
tag === 'kat-option' ||
|
||||
tag === 'kat-label' ||
|
||||
role === 'option' ||
|
||||
/parentKatOptionStyle/i.test(className)
|
||||
);
|
||||
}
|
||||
|
||||
function isSearchFieldOption(meta) {
|
||||
return /^(所有|SKUS?|ASIN|FNSKU|UPC\/EAN|商品名称\/关键字)$|^(ALL|SKU|ASIN|FNSKU|TITLE_KEYWORD|GTIN)$/i.test(
|
||||
`${meta.rawText} ${meta.parsedStatus} ${meta.value}`
|
||||
);
|
||||
}
|
||||
|
||||
function isListingStatusOption(meta) {
|
||||
if (isSearchFieldOption(meta)) return false;
|
||||
return /^(全部|在售|不可售|详情页面已删除|订单页面已删除|商品信息草稿|缺少的信息|定价问题|需要批准|配送问题|缺少报价|已停售|在搜索结果中禁止显示)$/.test(
|
||||
meta.parsedStatus
|
||||
);
|
||||
}
|
||||
|
||||
function normalize(text) {
|
||||
return String(text || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function parseStatus(rawText) {
|
||||
const text = normalize(rawText);
|
||||
if (!text) return '';
|
||||
const match = text.match(/^(.*?)\s*[((]\s*[\d,]+\s*[))]\s*$/);
|
||||
return normalize(match ? match[1] : text);
|
||||
}
|
||||
|
||||
function optionMeta(el, index) {
|
||||
const rawText = textOf(el) || el.getAttribute('label') || el.getAttribute('text') || el.getAttribute('value') || '';
|
||||
return {
|
||||
index,
|
||||
rawText: normalize(rawText),
|
||||
parsedStatus: parseStatus(rawText),
|
||||
value: el.getAttribute('value') || el.getAttribute('data-value') || '',
|
||||
visible: isVisible(el),
|
||||
};
|
||||
}
|
||||
|
||||
function clickTargetFor(el) {
|
||||
if (!el) return el;
|
||||
return el.closest('[class*="parentKatOptionStyle"], kat-option, [role="option"]') || el;
|
||||
}
|
||||
|
||||
const targetStatus = normalize((__crawlerPayload || {}).status);
|
||||
const dropdown = findMarkedDropdown();
|
||||
const options = [];
|
||||
|
||||
if (dropdown) {
|
||||
const roots = [dropdown];
|
||||
if (dropdown.shadowRoot) roots.push(dropdown.shadowRoot);
|
||||
for (const root of roots) {
|
||||
for (const el of Array.from(root.querySelectorAll('kat-option, kat-label, [role="option"], [class*="parentKatOptionStyle"]'))) {
|
||||
options.push(el);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!options.length) {
|
||||
for (const el of allElements()) {
|
||||
if (!isOptionLike(el)) continue;
|
||||
if (!isVisible(el) || insideListingRow(el)) continue;
|
||||
options.push(el);
|
||||
}
|
||||
}
|
||||
|
||||
const metas = options
|
||||
.map((el, index) => ({ el, ...optionMeta(el, index) }))
|
||||
.filter((item) => isListingStatusOption(item));
|
||||
const matched = metas.find((item) => item.parsedStatus === targetStatus || item.rawText === targetStatus);
|
||||
if (!matched) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'status option not found',
|
||||
targetStatus,
|
||||
options: metas.map(({ index, rawText, parsedStatus, value, visible }) => ({
|
||||
index,
|
||||
rawText,
|
||||
parsedStatus,
|
||||
value,
|
||||
visible,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
const clickTarget = clickTargetFor(matched.el);
|
||||
clickTarget.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
clickTarget.click();
|
||||
return {
|
||||
ok: true,
|
||||
targetStatus,
|
||||
selected: {
|
||||
index: matched.index,
|
||||
rawText: matched.rawText,
|
||||
parsedStatus: matched.parsedStatus,
|
||||
value: matched.value,
|
||||
clickedTag: clickTarget.tagName,
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function snapshot(el, index) {
|
||||
return {
|
||||
index,
|
||||
tag: el.tagName.toLowerCase(),
|
||||
text: textOf(el),
|
||||
attrs: attrsOf(el),
|
||||
outerHTML: (el.outerHTML || '').slice(0, 2000),
|
||||
};
|
||||
}
|
||||
|
||||
const KEYWORD_RE = /推荐报价|featured offer|buy box|商城|marketplace|欧洲|英国|德国|法国|西班牙|意大利|2\s*天前|30\s*天前|2\s*days?\s*ago|30\s*days?\s*ago/i;
|
||||
const visible = allElements().filter((el) => isVisible(el));
|
||||
|
||||
const keywordMatches = visible
|
||||
.map((el, index) => ({ el, index, text: textOf(el) }))
|
||||
.filter((row) => KEYWORD_RE.test(`${row.el.tagName} ${row.text} ${Object.values(attrsOf(row.el)).join(' ')}`))
|
||||
.slice(0, 120)
|
||||
.map(({ el, index }) => snapshot(el, index));
|
||||
|
||||
const buyBoxCard = visible.find((el) => (el.getAttribute('data-key') || '') === 'KPI_CARD_BUYBOX')
|
||||
|| visible.find((el) => (el.id || '') === 'KPI_CARD_BUYBOX');
|
||||
|
||||
const buyBoxNeighborhood = [];
|
||||
if (buyBoxCard) {
|
||||
let node = buyBoxCard;
|
||||
for (let depth = 0; node && depth < 6; depth += 1, node = node.parentElement) {
|
||||
buyBoxNeighborhood.push({
|
||||
depth,
|
||||
tag: node.tagName.toLowerCase(),
|
||||
text: textOf(node),
|
||||
attrs: attrsOf(node),
|
||||
outerHTML: (node.outerHTML || '').slice(0, 2500),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const buyBoxLeafMatches = [];
|
||||
if (buyBoxCard) {
|
||||
const scoped = [];
|
||||
walkRoots(buyBoxCard, scoped, new Set());
|
||||
scoped
|
||||
.filter((el) => isVisible(el))
|
||||
.map((el, index) => ({ el, index, text: textOf(el) }))
|
||||
.filter((row) => row.text && row.text.length <= 200)
|
||||
.filter((row) => /推荐报价|商城|marketplace|欧洲|英国|德国|法国|西班牙|意大利|\d+(?:\.\d+)?\s*%|2\s*天前|30\s*天前|2\s*days?\s*ago|30\s*days?\s*ago/i.test(row.text))
|
||||
.slice(0, 120)
|
||||
.forEach(({ el, index }) => buyBoxLeafMatches.push(snapshot(el, index)));
|
||||
}
|
||||
|
||||
return {
|
||||
url: window.location.href,
|
||||
title: document.title,
|
||||
buyBoxFound: !!buyBoxCard,
|
||||
buyBoxNeighborhood,
|
||||
buyBoxLeafMatches,
|
||||
keywordMatches,
|
||||
};
|
||||
@@ -0,0 +1,132 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function attrsText(el) {
|
||||
return Object.entries(attrsOf(el)).map(([key, value]) => `${key}=${value}`).join(' ');
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function clickableFor(el) {
|
||||
if (el && el.querySelector) {
|
||||
const preferred = el.querySelector(
|
||||
'casino-button[data-actionname="expand"], [data-actionname="expand"], casino-button[aria-label="chevron-down"], [aria-label="chevron-down"]'
|
||||
);
|
||||
if (preferred) return preferred;
|
||||
}
|
||||
|
||||
let node = el;
|
||||
for (let depth = 0; node && depth < 7; depth += 1, node = node.parentElement) {
|
||||
if (node.querySelector) {
|
||||
const preferred = node.querySelector(
|
||||
'casino-button[data-actionname="expand"], [data-actionname="expand"], casino-button[aria-label="chevron-down"], [aria-label="chevron-down"]'
|
||||
);
|
||||
if (preferred) return preferred;
|
||||
}
|
||||
const tag = node.tagName.toLowerCase();
|
||||
const role = node.getAttribute('role');
|
||||
if (tag === 'button' || tag === 'a' || tag === 'kat-button' || role === 'button' || node.onclick) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return el;
|
||||
}
|
||||
|
||||
function scoreEntry(el) {
|
||||
const text = textOf(el);
|
||||
const haystack = `${el.tagName} ${attrsText(el)} ${text}`;
|
||||
let score = 0;
|
||||
if (/推荐报价百分比|推荐报价.*百分比|featured offer percentage|featured offer.*%|featured offer/i.test(haystack)) {
|
||||
score += 120;
|
||||
}
|
||||
if (/buy box|购物车|报价/i.test(haystack)) score += 20;
|
||||
if (/%/.test(haystack)) score += 15;
|
||||
if (/button|link|href|click|card|metric/i.test(haystack)) score += 10;
|
||||
if (isVisible(el)) score += 10;
|
||||
if (text.length > 800) score -= 30;
|
||||
return { score, text, haystack };
|
||||
}
|
||||
|
||||
const ranked = allElements()
|
||||
.filter((el) => isVisible(el))
|
||||
.map((el, index) => ({ el, index, ...scoreEntry(el) }))
|
||||
.filter((row) => row.score > 0)
|
||||
.sort((a, b) => {
|
||||
if (b.score !== a.score) return b.score - a.score;
|
||||
return a.text.length - b.text.length;
|
||||
});
|
||||
|
||||
if (!ranked.length) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: 'recommended offer percentage entry not found',
|
||||
url: window.location.href,
|
||||
title: document.title,
|
||||
candidates: allElements()
|
||||
.filter((el) => isVisible(el))
|
||||
.map((el, index) => ({ index, tag: el.tagName.toLowerCase(), text: textOf(el), attrs: attrsOf(el) }))
|
||||
.filter((row) => /推荐报价|featured offer|buy box|百分比|percentage/i.test(`${row.tag} ${row.text} ${Object.values(row.attrs).join(' ')}`))
|
||||
.slice(0, 80),
|
||||
};
|
||||
}
|
||||
|
||||
const selected = ranked[0];
|
||||
const clickable = clickableFor(selected.el);
|
||||
clickable.setAttribute('data-crawler-recommended-offer-entry', 'true');
|
||||
clickable.scrollIntoView({ block: 'center', inline: 'nearest' });
|
||||
['pointerdown', 'mousedown', 'mouseup', 'click'].forEach((eventName) => {
|
||||
clickable.dispatchEvent(new MouseEvent(eventName, { bubbles: true, cancelable: true, composed: true }));
|
||||
});
|
||||
if (typeof clickable.click === 'function') clickable.click();
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
selected: {
|
||||
index: selected.index,
|
||||
score: selected.score,
|
||||
text: selected.text,
|
||||
attrs: attrsText(selected.el),
|
||||
clickableTag: clickable.tagName.toLowerCase(),
|
||||
clickableText: textOf(clickable),
|
||||
outerHTML: (selected.el.outerHTML || '').slice(0, 2000),
|
||||
},
|
||||
candidates: ranked.slice(0, 12).map((row) => ({
|
||||
index: row.index,
|
||||
score: row.score,
|
||||
text: row.text,
|
||||
attrs: attrsText(row.el),
|
||||
outerHTML: (row.el.outerHTML || '').slice(0, 1000),
|
||||
})),
|
||||
};
|
||||
@@ -0,0 +1,332 @@
|
||||
function textOf(node) {
|
||||
return (node && (node.innerText || node.textContent || '') || '').replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function attrsOf(el) {
|
||||
const attrs = {};
|
||||
if (!el || !el.attributes) return attrs;
|
||||
for (const attr of el.attributes) attrs[attr.name] = attr.value;
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function isVisible(el) {
|
||||
if (!el || !el.getBoundingClientRect) return false;
|
||||
const rect = el.getBoundingClientRect();
|
||||
const style = window.getComputedStyle(el);
|
||||
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
|
||||
}
|
||||
|
||||
function walkRoots(root, rows, seen) {
|
||||
if (!root || seen.has(root)) return;
|
||||
seen.add(root);
|
||||
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
||||
let node = walker.currentNode;
|
||||
while (node) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE && node.tagName) rows.push(node);
|
||||
if (node.shadowRoot) walkRoots(node.shadowRoot, rows, seen);
|
||||
node = walker.nextNode();
|
||||
}
|
||||
}
|
||||
|
||||
function allElements() {
|
||||
const rows = [];
|
||||
walkRoots(document, rows, new Set());
|
||||
return rows;
|
||||
}
|
||||
|
||||
function rowOf(el, index) {
|
||||
const attrs = attrsOf(el);
|
||||
const rect = el.getBoundingClientRect();
|
||||
return {
|
||||
index,
|
||||
tag: el.tagName.toLowerCase(),
|
||||
raw_text: textOf(el),
|
||||
label_text: attrs.label || attrs.title || attrs['aria-label'] || '',
|
||||
percentage_text: attrs.value || attrs['data-value'] || '',
|
||||
attrs,
|
||||
visible: isVisible(el),
|
||||
rect_left: rect.left,
|
||||
rect_top: rect.top,
|
||||
rect_width: rect.width,
|
||||
rect_height: rect.height,
|
||||
outerHTML: (el.outerHTML || '').slice(0, 1200),
|
||||
};
|
||||
}
|
||||
|
||||
const COUNTRIES = [
|
||||
{ country: '欧洲', aliases: ['欧洲', 'Europe'] },
|
||||
{ country: '英国', aliases: ['英国', 'UK', 'United Kingdom'] },
|
||||
{ country: '德国', aliases: ['德国', 'Germany'] },
|
||||
{ country: '法国', aliases: ['法国', 'France'] },
|
||||
{ country: '西班牙', aliases: ['西班牙', 'Spain'] },
|
||||
{ country: '意大利', aliases: ['意大利', 'Italy'] },
|
||||
];
|
||||
|
||||
function percentagesOf(text) {
|
||||
return (text.match(/\d+(?:\.\d+)?\s*%/g) || []).map((item) => item.replace(/\s+/g, ''));
|
||||
}
|
||||
|
||||
function parentOrHost(el) {
|
||||
if (!el) return null;
|
||||
if (el.parentElement) return el.parentElement;
|
||||
const root = el.getRootNode && el.getRootNode();
|
||||
return root && root.host ? root.host : null;
|
||||
}
|
||||
|
||||
function countryOfText(text) {
|
||||
const normalized = (text || '').trim().toLowerCase();
|
||||
return COUNTRIES.find(({ aliases }) => aliases.some((alias) => normalized === alias.toLowerCase())) || null;
|
||||
}
|
||||
|
||||
function countCountries(text) {
|
||||
return COUNTRIES.filter(({ aliases }) => aliases.some((alias) => new RegExp(`(^|\\s)${alias.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(\\s|$)`, 'i').test(text))).length;
|
||||
}
|
||||
|
||||
function bestCountryRowText(el) {
|
||||
const visited = new Set();
|
||||
let node = el;
|
||||
for (let depth = 0; node && depth < 7; depth += 1, node = parentOrHost(node)) {
|
||||
if (visited.has(node)) break;
|
||||
visited.add(node);
|
||||
const text = textOf(node);
|
||||
if (!text || text.length > 500) continue;
|
||||
const percentages = percentagesOf(text);
|
||||
if (percentages.length >= 2 && countCountries(text) <= 1) return text;
|
||||
}
|
||||
|
||||
const parent = parentOrHost(el);
|
||||
if (!parent || !parent.children) return '';
|
||||
|
||||
const children = Array.from(parent.children).filter((child) => isVisible(child) && textOf(child));
|
||||
const index = children.indexOf(el);
|
||||
if (index < 0) return '';
|
||||
|
||||
const nearby = children.slice(Math.max(0, index - 1), Math.min(children.length, index + 5)).map((child) => textOf(child));
|
||||
const joined = nearby.join(' ');
|
||||
return percentagesOf(joined).length >= 2 ? joined : '';
|
||||
}
|
||||
|
||||
function directCountryRows(elements) {
|
||||
const results = new Map();
|
||||
elements.forEach((el) => {
|
||||
const match = countryOfText(textOf(el));
|
||||
if (!match || results.has(match.country)) return;
|
||||
const text = bestCountryRowText(el);
|
||||
const percentages = percentagesOf(text);
|
||||
if (percentages.length < 2) return;
|
||||
results.set(match.country, {
|
||||
country: match.country,
|
||||
ratio2DaysAgo: percentages[0],
|
||||
ratio30DaysAgo: percentages[1],
|
||||
raw_text: text,
|
||||
source: 'rendered-country-row',
|
||||
});
|
||||
});
|
||||
return Array.from(results.values());
|
||||
}
|
||||
|
||||
function expandedCardOf(elements) {
|
||||
return elements.find((el) => el.tagName && el.tagName.toLowerCase() === 'casino-card' && /\bcasino-expanded-card\b/.test(el.className || ''));
|
||||
}
|
||||
|
||||
function directRowsFromExpandedCard(expandedCard) {
|
||||
if (!expandedCard) return [];
|
||||
|
||||
const scoped = [];
|
||||
walkRoots(expandedCard, scoped, new Set());
|
||||
|
||||
const countries = [];
|
||||
const percentages = [];
|
||||
const seen = new Set();
|
||||
|
||||
scoped
|
||||
.filter((el) => isVisible(el))
|
||||
.forEach((el) => {
|
||||
const text = textOf(el);
|
||||
const title = (el.getAttribute && el.getAttribute('title')) || '';
|
||||
const aria = (el.getAttribute && (el.getAttribute('aria-label') || el.getAttribute('arialabel'))) || '';
|
||||
const candidate = (title || aria || text || '').replace(/\s+/g, ' ').trim();
|
||||
if (!candidate || candidate.length > 20) return;
|
||||
|
||||
const rect = el.getBoundingClientRect();
|
||||
const area = rect.width * rect.height;
|
||||
if (area <= 0 || area > 20000) return;
|
||||
|
||||
const key = `${candidate}|${Math.round(rect.left)}|${Math.round(rect.top)}|${Math.round(rect.width)}|${Math.round(rect.height)}`;
|
||||
if (seen.has(key)) return;
|
||||
seen.add(key);
|
||||
|
||||
const countryMatch = countryOfText(candidate);
|
||||
if (countryMatch) {
|
||||
countries.push({
|
||||
country: countryMatch.country,
|
||||
x: rect.left,
|
||||
y: rect.top + rect.height / 2,
|
||||
h: rect.height,
|
||||
area,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (/^\d+(?:\.\d+)?\s*%$/.test(candidate)) {
|
||||
percentages.push({
|
||||
value: candidate.replace(/\s+/g, ''),
|
||||
x: rect.left + rect.width / 2,
|
||||
y: rect.top + rect.height / 2,
|
||||
area,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const dedupedCountries = [];
|
||||
countries
|
||||
.sort((a, b) => a.y - b.y || a.x - b.x || a.area - b.area)
|
||||
.forEach((item) => {
|
||||
const exists = dedupedCountries.some((row) => row.country === item.country && Math.abs(row.y - item.y) < 8);
|
||||
if (!exists) dedupedCountries.push(item);
|
||||
});
|
||||
|
||||
const results = dedupedCountries.map((row) => {
|
||||
const sameRow = percentages
|
||||
.filter((item) => item.x > row.x + 20 && Math.abs(item.y - row.y) <= Math.max(14, row.h))
|
||||
.sort((a, b) => a.x - b.x || a.y - b.y || a.area - b.area);
|
||||
|
||||
const unique = [];
|
||||
sameRow.forEach((item) => {
|
||||
if (!unique.some((existing) => existing.value === item.value && Math.abs(existing.x - item.x) < 8)) {
|
||||
unique.push(item);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
country: row.country,
|
||||
ratio2DaysAgo: unique[0] ? unique[0].value : '',
|
||||
ratio30DaysAgo: unique[1] ? unique[1].value : '',
|
||||
source: 'expanded-card-geometry',
|
||||
};
|
||||
});
|
||||
|
||||
return results.filter((item) => item.ratio2DaysAgo || item.ratio30DaysAgo);
|
||||
}
|
||||
|
||||
function scoreRegion(el) {
|
||||
const text = textOf(el);
|
||||
if (!/%/.test(text)) return 0;
|
||||
|
||||
let score = 0;
|
||||
if (/推荐报价百分比|推荐报价|featured offer percentage|featured offer|buy box/i.test(text)) score += 80;
|
||||
if (/2\s*天前|30\s*天前|2\s*日前|30\s*日前|2\s*days?\s*ago|30\s*days?\s*ago/i.test(text)) score += 80;
|
||||
if (/欧洲|英国|德国|法国|西班牙|意大利|Europe|UK|United Kingdom|Germany|France|Spain|Italy/i.test(text)) score += 40;
|
||||
if (/%/.test(text)) score += 20;
|
||||
if (isVisible(el)) score += 10;
|
||||
if (text.length > 2500) score -= 40;
|
||||
return score;
|
||||
}
|
||||
|
||||
function isRelevantRatioRow(row) {
|
||||
const combined = `${row.raw_text} ${row.label_text} ${row.percentage_text}`;
|
||||
const hasDayLabels = /2\s*天前|30\s*天前|2\s*日前|30\s*日前|2\s*days?\s*ago|30\s*days?\s*ago/i.test(combined);
|
||||
const hasCountries = /欧洲|英国|德国|法国|西班牙|意大利|Europe|UK|United Kingdom|Germany|France|Spain|Italy/i.test(combined);
|
||||
if (!/%|2\s*天前|30\s*天前|2\s*日前|30\s*日前|2\s*days?\s*ago|30\s*days?\s*ago/i.test(combined) && !hasCountries) return false;
|
||||
if (row.raw_text.length > 800 && !hasDayLabels && !hasCountries) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function collectRelevantRows(sourceElements) {
|
||||
const byText = new Map();
|
||||
const scoped = [];
|
||||
sourceElements.forEach((root) => walkRoots(root, scoped, new Set()));
|
||||
scoped
|
||||
.filter((el) => isVisible(el))
|
||||
.forEach((el, index) => {
|
||||
const row = rowOf(el, index);
|
||||
if (!isRelevantRatioRow(row)) return;
|
||||
const combined = `${row.raw_text} ${row.label_text} ${row.percentage_text}`;
|
||||
const key = `${combined}|${Math.round(row.rect_left)}|${Math.round(row.rect_top)}|${Math.round(row.rect_width)}|${Math.round(row.rect_height)}`;
|
||||
if (!byText.has(key)) byText.set(key, row);
|
||||
});
|
||||
return Array.from(byText.values());
|
||||
}
|
||||
|
||||
function scrollableElements(elements) {
|
||||
return elements.filter((el) => {
|
||||
if (!el || !el.getBoundingClientRect || !isVisible(el)) return false;
|
||||
const style = window.getComputedStyle(el);
|
||||
if (!/(auto|scroll)/i.test(`${style.overflowY} ${style.overflow}`)) return false;
|
||||
return el.scrollHeight > el.clientHeight + 20;
|
||||
});
|
||||
}
|
||||
|
||||
function collectRowsAcrossScroll(sourceElements, visibleElements) {
|
||||
const targets = scrollableElements(visibleElements);
|
||||
if (document.scrollingElement) targets.push(document.scrollingElement);
|
||||
|
||||
const byKey = new Map();
|
||||
const originals = targets.map((target) => ({ target, top: target.scrollTop }));
|
||||
for (const target of targets) {
|
||||
const maxTop = Math.max(0, target.scrollHeight - target.clientHeight);
|
||||
const positions = [...new Set([0, Math.floor(maxTop / 2), maxTop])];
|
||||
for (const position of positions) {
|
||||
target.scrollTop = position;
|
||||
collectRelevantRows(sourceElements).forEach((row) => {
|
||||
const combined = `${row.raw_text} ${row.label_text} ${row.percentage_text}`;
|
||||
const key = `${combined}|${Math.round(row.rect_left)}|${Math.round(row.rect_top)}|${Math.round(row.rect_width)}|${Math.round(row.rect_height)}`;
|
||||
if (!byKey.has(key)) byKey.set(key, row);
|
||||
});
|
||||
}
|
||||
}
|
||||
originals.forEach(({ target, top }) => {
|
||||
target.scrollTop = top;
|
||||
});
|
||||
return Array.from(byKey.values());
|
||||
}
|
||||
|
||||
const visible = allElements().filter((el) => isVisible(el));
|
||||
const expandedCard = expandedCardOf(visible);
|
||||
const expandedDirectRows = directRowsFromExpandedCard(expandedCard);
|
||||
const directRows = expandedDirectRows.length ? expandedDirectRows : directCountryRows(visible);
|
||||
const regions = visible
|
||||
.map((el, index) => {
|
||||
const rect = el.getBoundingClientRect();
|
||||
return { el, index, score: scoreRegion(el), text: textOf(el), area: rect.width * rect.height };
|
||||
})
|
||||
.filter((row) => row.score > 0)
|
||||
.sort((a, b) => {
|
||||
if (b.score !== a.score) return b.score - a.score;
|
||||
if (a.text.length !== b.text.length) return a.text.length - b.text.length;
|
||||
return a.area - b.area;
|
||||
});
|
||||
|
||||
const sourceElements = expandedCard ? [expandedCard] : (regions.length ? [regions[0].el] : visible);
|
||||
const scoped = [];
|
||||
sourceElements.forEach((root) => walkRoots(root, scoped, new Set()));
|
||||
|
||||
const byText = new Map();
|
||||
collectRelevantRows(sourceElements)
|
||||
.concat(collectRowsAcrossScroll(sourceElements, visible))
|
||||
.forEach((row) => {
|
||||
const combined = `${row.raw_text} ${row.label_text} ${row.percentage_text}`;
|
||||
const key = `${combined}|${Math.round(row.rect_left)}|${Math.round(row.rect_top)}|${Math.round(row.rect_width)}|${Math.round(row.rect_height)}`;
|
||||
if (!byText.has(key)) byText.set(key, row);
|
||||
});
|
||||
|
||||
return {
|
||||
directRows,
|
||||
region: regions.length
|
||||
? {
|
||||
score: regions[0].score,
|
||||
text: regions[0].text.slice(0, 2000),
|
||||
outerHTML: (regions[0].el.outerHTML || '').slice(0, 3000),
|
||||
}
|
||||
: {
|
||||
url: window.location.href,
|
||||
title: document.title,
|
||||
},
|
||||
rows: Array.from(byText.values()),
|
||||
candidates: regions.slice(0, 20).map((row) => ({
|
||||
index: row.index,
|
||||
score: row.score,
|
||||
text: row.text.slice(0, 1000),
|
||||
})),
|
||||
};
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,7 +2,7 @@
|
||||
主页面蓝图:首页、home、图片工作台、品牌页、静态文件、Logo
|
||||
"""
|
||||
import os
|
||||
from flask import Blueprint, send_file
|
||||
from flask import Blueprint, send_file, render_template_string
|
||||
|
||||
from app_common import (
|
||||
get_db,
|
||||
@@ -23,6 +23,48 @@ from config import base_url,version,JAVA_API_BASE
|
||||
main_bp = Blueprint('main', __name__)
|
||||
|
||||
|
||||
def _resolve_asset_filename(filename):
|
||||
"""Resolve hashed Vite assets without maintaining hardcoded alias tables."""
|
||||
safe_name = os.path.basename(filename)
|
||||
asset_dirs = [
|
||||
ASSETS_DIR,
|
||||
os.path.abspath(os.path.join(BASE_DIR, 'new_web_source', 'assets')),
|
||||
os.path.abspath(os.path.join(BASE_DIR, '..', 'new_web_source', 'assets')),
|
||||
]
|
||||
for asset_dir in asset_dirs:
|
||||
exact_path = os.path.join(asset_dir, safe_name)
|
||||
if os.path.isfile(exact_path):
|
||||
return safe_name
|
||||
|
||||
base_name, ext = os.path.splitext(safe_name)
|
||||
if not base_name or not ext:
|
||||
return safe_name
|
||||
|
||||
parts = base_name.split('-')
|
||||
if len(parts) < 2:
|
||||
return safe_name
|
||||
|
||||
asset_names = []
|
||||
for asset_dir in asset_dirs:
|
||||
try:
|
||||
asset_names.extend(os.listdir(asset_dir))
|
||||
except OSError:
|
||||
continue
|
||||
if not asset_names:
|
||||
return safe_name
|
||||
|
||||
for prefix_length in range(len(parts) - 1, 0, -1):
|
||||
prefix = '-'.join(parts[:prefix_length])
|
||||
matches = [
|
||||
asset_name for asset_name in asset_names
|
||||
if asset_name.endswith(ext) and asset_name.startswith(prefix)
|
||||
]
|
||||
if len(matches) == 1:
|
||||
return matches[0]
|
||||
|
||||
return safe_name
|
||||
|
||||
|
||||
@main_bp.route('/')
|
||||
def index():
|
||||
if session.get('user_id') and _is_session_user_valid():
|
||||
@@ -53,9 +95,46 @@ def wb():
|
||||
@main_bp.route('/brand')
|
||||
@login_required
|
||||
def brand_page():
|
||||
candidate_paths = [
|
||||
os.path.join(BASE_DIR, 'web_source', 'brand.html'),
|
||||
os.path.abspath(os.path.join(BASE_DIR, '..', 'web_source', 'brand.html')),
|
||||
]
|
||||
for candidate_path in candidate_paths:
|
||||
if not os.path.isfile(candidate_path):
|
||||
continue
|
||||
try:
|
||||
with open(candidate_path, 'rb') as f:
|
||||
raw = f.read()
|
||||
try:
|
||||
from html_crypto import decrypt
|
||||
content = decrypt(raw).decode('utf-8')
|
||||
except Exception:
|
||||
if raw[:7] == b'gAAAAAB':
|
||||
raise
|
||||
content = raw.decode('utf-8', errors='replace')
|
||||
return render_template_string(content, user_id=session.get('user_id'))
|
||||
except Exception:
|
||||
continue
|
||||
return _render_html('brand.html', user_id=session.get('user_id'))
|
||||
|
||||
|
||||
@main_bp.route('/brand/legacy')
|
||||
@login_required
|
||||
def brand_page_legacy():
|
||||
legacy_path = os.path.join(BASE_DIR, 'web_source', 'templates_backup', 'brand.html')
|
||||
if not os.path.isfile(legacy_path):
|
||||
return '', 404
|
||||
with open(legacy_path, 'rb') as f:
|
||||
content = f.read().decode('utf-8', errors='replace')
|
||||
content = content.replace(
|
||||
'<header class="top-bar">',
|
||||
'<header class="top-bar" style="display:none !important;">',
|
||||
1,
|
||||
)
|
||||
content = content.replace('height: calc(100vh - 56px);', 'height: 100vh;', 1)
|
||||
return render_template_string(content, user_id=session.get('user_id'))
|
||||
|
||||
|
||||
|
||||
|
||||
@main_bp.route('/static/<path:filename>')
|
||||
@@ -72,12 +151,20 @@ def serve_static(filename):
|
||||
@main_bp.route('/assets/<path:filename>')
|
||||
def serve_assets(filename):
|
||||
"""提供 static 目录及子目录下的静态文件访问。"""
|
||||
filepath = os.path.normpath(os.path.join(ASSETS_DIR, filename))
|
||||
static_abs = os.path.abspath(ASSETS_DIR)
|
||||
filename = _resolve_asset_filename(filename)
|
||||
candidate_dirs = [
|
||||
os.path.abspath(ASSETS_DIR),
|
||||
os.path.abspath(os.path.join(BASE_DIR, 'new_web_source', 'assets')),
|
||||
os.path.abspath(os.path.join(BASE_DIR, '..', 'new_web_source', 'assets')),
|
||||
]
|
||||
for static_abs in candidate_dirs:
|
||||
filepath = os.path.normpath(os.path.join(static_abs, filename))
|
||||
file_abs = os.path.abspath(filepath)
|
||||
if not file_abs.startswith(static_abs) or not os.path.isfile(file_abs):
|
||||
return '', 404
|
||||
if not file_abs.startswith(static_abs):
|
||||
continue
|
||||
if os.path.isfile(file_abs):
|
||||
return send_file(file_abs, as_attachment=False)
|
||||
return '', 404
|
||||
|
||||
|
||||
@main_bp.route('/new_web_source/<path:filename>')
|
||||
@@ -90,8 +177,6 @@ def serve_new_web_source(filename):
|
||||
return '', 404
|
||||
return send_file(file_abs, as_attachment=False)
|
||||
|
||||
|
||||
|
||||
@main_bp.route('/logo.jpg', methods=['GET'])
|
||||
def get_logo_image():
|
||||
return send_file(os.path.join(BASE_DIR, "logo.jpg"), mimetype='image/jpeg')
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
102
app/pyproject.toml
Normal file
102
app/pyproject.toml
Normal file
@@ -0,0 +1,102 @@
|
||||
[project]
|
||||
name = "crawler-desktop-app"
|
||||
version = "0.1.0"
|
||||
description = "Desktop crawler app backend and pywebview shell."
|
||||
requires-python = ">=3.9,<3.10"
|
||||
dependencies = [
|
||||
"alibabacloud-oss-v2==1.2.4",
|
||||
"altgraph==0.17.5",
|
||||
"annotated-types==0.7.0",
|
||||
"anyio==4.12.1",
|
||||
"Authlib==1.6.8",
|
||||
"blinker==1.9.0",
|
||||
"bottle==0.13.4",
|
||||
"certifi==2026.1.4",
|
||||
"cffi==2.0.0",
|
||||
"charset-normalizer==3.4.4",
|
||||
"click==8.1.8",
|
||||
"clr_loader==0.2.10",
|
||||
"colorama==0.4.6",
|
||||
"cozepy==0.20.0",
|
||||
"crcmod-plus==2.3.1",
|
||||
"cryptography==41.0.0",
|
||||
"cssselect==1.3.0",
|
||||
"DataRecorder==3.6.2",
|
||||
"distro==1.9.0",
|
||||
"DownloadKit==2.0.7",
|
||||
"DrissionPage==4.1.1.2",
|
||||
"et_xmlfile==2.0.0",
|
||||
"exceptiongroup==1.3.1",
|
||||
"filelock==3.19.1",
|
||||
"Flask==3.1.3",
|
||||
"flask-cors==6.0.2",
|
||||
"h11==0.16.0",
|
||||
"httpcore==1.0.9",
|
||||
"httpx==0.28.1",
|
||||
"idna==3.11",
|
||||
"importlib_metadata==8.7.1",
|
||||
"itsdangerous==2.2.0",
|
||||
"Jinja2==3.1.6",
|
||||
"loguru>=0.7.3",
|
||||
"lxml==6.0.2",
|
||||
"MarkupSafe==3.0.3",
|
||||
"MouseInfo==0.1.3",
|
||||
"Nuitka==2.8.6",
|
||||
"numpy==1.25.2",
|
||||
"openpyxl==3.1.5",
|
||||
"ordered-set==4.1.0",
|
||||
"packaging==26.0",
|
||||
"pandas==2.3.3",
|
||||
"pefile==2024.8.26",
|
||||
"pillow==11.3.0",
|
||||
"proxy_tools==0.1.0",
|
||||
"psutil==7.2.2",
|
||||
"PyAutoGUI==0.9.54",
|
||||
"pycparser==2.23",
|
||||
"pycryptodome==3.23.0",
|
||||
"pydantic==2.12.5",
|
||||
"pydantic_core==2.41.5",
|
||||
"PyGetWindow==0.0.9",
|
||||
"pyinstaller==6.19.0",
|
||||
"pyinstaller-hooks-contrib==2026.3",
|
||||
"PyMsgBox==2.0.1",
|
||||
"PyMySQL==1.1.2",
|
||||
"pyperclip==1.11.0",
|
||||
"PyQt5==5.15.11",
|
||||
"PyQt5-Qt5==5.15.2",
|
||||
"PyQt5_sip==12.17.1",
|
||||
"PyRect==0.2.0",
|
||||
"PyScreeze==1.0.1",
|
||||
"python-dateutil==2.9.0.post0",
|
||||
"python-dotenv==1.2.1",
|
||||
"pythonnet==3.0.5",
|
||||
"pytweening==1.2.0",
|
||||
"pytz==2026.1.post1",
|
||||
"pywebview==6.1",
|
||||
"pywin32-ctypes==0.2.3",
|
||||
"requests==2.32.5",
|
||||
"requests-file==3.0.1",
|
||||
"six==1.17.0",
|
||||
"tldextract==5.3.0",
|
||||
"typing-inspection==0.4.2",
|
||||
"typing_extensions==4.15.0",
|
||||
"tzdata==2025.3",
|
||||
"urllib3==2.6.3",
|
||||
"websocket-client==1.9.0",
|
||||
"websockets==14.2",
|
||||
"Werkzeug==3.1.6",
|
||||
"zipp==3.23.0",
|
||||
"zstandard==0.25.0",
|
||||
]
|
||||
|
||||
[tool.uv]
|
||||
package = false
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
pythonpath = ["."]
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"pytest>=8.4.2",
|
||||
"ruff>=0.14.0",
|
||||
]
|
||||
0
app/tests/conftest.py
Normal file
0
app/tests/conftest.py
Normal file
385
app/tests/test_amazon_base.py
Normal file
385
app/tests/test_amazon_base.py
Normal file
@@ -0,0 +1,385 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from amazon import base
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, payload):
|
||||
self.payload = payload
|
||||
|
||||
def json(self):
|
||||
return self.payload
|
||||
|
||||
|
||||
class FakeWait:
|
||||
def __init__(self, *, raise_on_doc_loaded=False):
|
||||
self.doc_loaded_calls = []
|
||||
self.displayed_calls = []
|
||||
self.raise_on_doc_loaded = raise_on_doc_loaded
|
||||
|
||||
def doc_loaded(self, **kwargs):
|
||||
if self.raise_on_doc_loaded:
|
||||
raise RuntimeError("doc load failed")
|
||||
self.doc_loaded_calls.append(kwargs)
|
||||
|
||||
def displayed(self, **kwargs):
|
||||
self.displayed_calls.append(kwargs)
|
||||
return True
|
||||
|
||||
|
||||
class FakeElement:
|
||||
def __init__(self, *, text="", value=None):
|
||||
self.text = text
|
||||
self.value = value
|
||||
self.clicks = 0
|
||||
self.inputs = []
|
||||
self.wait = FakeWait()
|
||||
|
||||
def click(self, *args, **kwargs):
|
||||
self.clicks += 1
|
||||
|
||||
def input(self, value, clear=False):
|
||||
self.inputs.append((value, clear))
|
||||
|
||||
|
||||
class FakeTab:
|
||||
def __init__(self, *, ele_returns=None, eles_returns=None, raise_on_doc_loaded=False):
|
||||
self.ele_returns = {key: list(values) for key, values in (ele_returns or {}).items()}
|
||||
self.eles_returns = {key: list(values) for key, values in (eles_returns or {}).items()}
|
||||
self.wait = FakeWait(raise_on_doc_loaded=raise_on_doc_loaded)
|
||||
self.refreshes = 0
|
||||
|
||||
def ele(self, locator, timeout=None):
|
||||
values = self.ele_returns.get(locator)
|
||||
if not values:
|
||||
return None
|
||||
return values.pop(0)
|
||||
|
||||
def eles(self, locator, timeout=None):
|
||||
values = self.eles_returns.get(locator)
|
||||
if not values:
|
||||
return []
|
||||
return values.pop(0)
|
||||
|
||||
def refresh(self):
|
||||
self.refreshes += 1
|
||||
|
||||
|
||||
def test_ziniao_driver_initializes_browser_state():
|
||||
user_info = {
|
||||
"company": "test-company",
|
||||
"username": "test-user",
|
||||
"password": "test-password",
|
||||
}
|
||||
|
||||
driver = base.ZiniaoDriver(user_info, socket_port=19999)
|
||||
|
||||
assert driver.user_info is user_info
|
||||
assert driver.socket_port == 19999
|
||||
assert driver.client_path is None
|
||||
assert driver.browser is None
|
||||
assert driver.tab is None
|
||||
assert driver.store_id is None
|
||||
|
||||
|
||||
def test_amazon_base_inherits_ziniao_driver():
|
||||
driver = base.AmamzonBase({})
|
||||
|
||||
assert isinstance(driver, base.ZiniaoDriver)
|
||||
|
||||
|
||||
def test_kill_process_uses_ziniao_driver(monkeypatch):
|
||||
calls = []
|
||||
|
||||
class FakeDriver:
|
||||
def __init__(self, user_info):
|
||||
calls.append(("init", user_info))
|
||||
|
||||
def kill_process(self, version):
|
||||
calls.append(("kill_process", version))
|
||||
|
||||
monkeypatch.setattr(base, "ZiniaoDriver", FakeDriver)
|
||||
|
||||
base.kill_process("v6")
|
||||
|
||||
assert calls == [("init", {}), ("kill_process", "v6")]
|
||||
|
||||
|
||||
def test_get_zinaio_exe_returns_none_when_winreg_unavailable(monkeypatch):
|
||||
driver = base.ZiniaoDriver({})
|
||||
monkeypatch.setattr(base, "winreg", None)
|
||||
messages = []
|
||||
sink_id = base.logger.add(messages.append, level="ERROR", format="{message}")
|
||||
|
||||
try:
|
||||
result = driver.get_zinaio_exe()
|
||||
finally:
|
||||
base.logger.remove(sink_id)
|
||||
|
||||
assert result is None
|
||||
assert any("winreg" in message for message in messages)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("store_info", "expected_key"),
|
||||
[
|
||||
("12345", "browserId"),
|
||||
("oauth-token", "browserOauth"),
|
||||
],
|
||||
)
|
||||
def test_open_store_payload_uses_id_or_oauth(monkeypatch, store_info, expected_key):
|
||||
payloads = []
|
||||
|
||||
def fake_post(url, data, timeout):
|
||||
payloads.append(json.loads(data.decode("utf-8")))
|
||||
return FakeResponse({"statusCode": 0, "debuggingPort": 9222})
|
||||
|
||||
monkeypatch.setattr(base.requests, "post", fake_post)
|
||||
|
||||
result = base.ZiniaoDriver({"company": "c"}).open_store(store_info)
|
||||
|
||||
assert result["debuggingPort"] == 9222
|
||||
assert payloads[0]["action"] == "startBrowser"
|
||||
assert payloads[0][expected_key] == store_info
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("payload", "expected"),
|
||||
[
|
||||
({"statusCode": 0, "browserList": [{"browserName": "shop"}]}, [{"browserName": "shop"}]),
|
||||
({"statusCode": -10003}, None),
|
||||
({"statusCode": 500, "message": "failed"}, None),
|
||||
],
|
||||
)
|
||||
def test_get_browser_list_status_handling(monkeypatch, payload, expected):
|
||||
monkeypatch.setattr(base.requests, "post", lambda *args, **kwargs: FakeResponse(payload))
|
||||
|
||||
assert base.ZiniaoDriver({}).get_browser_list() == expected
|
||||
|
||||
|
||||
def test_update_core_retries_none_then_returns_on_success(monkeypatch):
|
||||
calls = []
|
||||
responses = [FakeResponse(None), FakeResponse({"statusCode": 0})]
|
||||
|
||||
def fake_post(url, data, timeout):
|
||||
calls.append(json.loads(data.decode("utf-8")))
|
||||
return responses.pop(0)
|
||||
|
||||
monkeypatch.setattr(base.requests, "post", fake_post)
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
|
||||
base.ZiniaoDriver({}).update_core()
|
||||
|
||||
assert [payload["action"] for payload in calls] == ["updateCore", "updateCore"]
|
||||
|
||||
|
||||
def test_wait_until_client_ready_polls_until_available(monkeypatch):
|
||||
readiness = [False, False, True]
|
||||
sleeps = []
|
||||
driver = base.ZiniaoDriver({})
|
||||
monkeypatch.setattr(driver, "_is_client_ready", lambda: readiness.pop(0))
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: sleeps.append(seconds))
|
||||
|
||||
assert driver._wait_until_client_ready(timeout=10) is True
|
||||
assert sleeps == [base.CLIENT_READY_INTERVAL, base.CLIENT_READY_INTERVAL]
|
||||
|
||||
|
||||
def test_close_store_success_uses_current_store(monkeypatch):
|
||||
payloads = []
|
||||
|
||||
def fake_post(url, data, timeout):
|
||||
payloads.append(json.loads(data.decode("utf-8")))
|
||||
return FakeResponse({"statusCode": 0, "ok": True})
|
||||
|
||||
monkeypatch.setattr(base.requests, "post", fake_post)
|
||||
driver = base.ZiniaoDriver({})
|
||||
driver.store_id = "oauth-1"
|
||||
|
||||
result = driver.close_store()
|
||||
|
||||
assert result["ok"] is True
|
||||
assert payloads[0]["action"] == "stopBrowser"
|
||||
assert payloads[0]["browserOauth"] == "oauth-1"
|
||||
|
||||
|
||||
def test_close_store_login_failure_raises(monkeypatch):
|
||||
monkeypatch.setattr(base.requests, "post", lambda *args, **kwargs: FakeResponse({"statusCode": -10003}))
|
||||
|
||||
with pytest.raises(RuntimeError, match="【close_store】登录失败"):
|
||||
base.ZiniaoDriver({}).close_store("oauth-1")
|
||||
|
||||
|
||||
def test_open_shop_returns_message_when_shop_missing(monkeypatch):
|
||||
driver = base.ZiniaoDriver({})
|
||||
monkeypatch.setattr(driver, "start_client", lambda: None)
|
||||
monkeypatch.setattr(driver, "get_browser_list", lambda: [{"browserName": "other"}])
|
||||
|
||||
assert driver.open_shop("target") == "店铺不存在"
|
||||
assert driver.store_id is None
|
||||
|
||||
|
||||
def test_open_shop_opens_matching_store(monkeypatch):
|
||||
opened_pages = []
|
||||
fake_browser = object()
|
||||
driver = base.ZiniaoDriver({})
|
||||
monkeypatch.setattr(driver, "start_client", lambda: None)
|
||||
monkeypatch.setattr(driver, "get_browser_list", lambda: [{"browserName": "target", "browserOauth": "oauth-1"}])
|
||||
monkeypatch.setattr(
|
||||
driver,
|
||||
"open_store",
|
||||
lambda store_id: {
|
||||
"browserOauth": "oauth-2",
|
||||
"debuggingPort": 9222,
|
||||
"ipDetectionPage": "https://ip-check",
|
||||
"launcherPage": "https://launcher",
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(driver, "get_browser", lambda port: fake_browser)
|
||||
monkeypatch.setattr(driver, "open_ip_check", lambda browser, url: True)
|
||||
monkeypatch.setattr(driver, "open_launcher_page", lambda page, browser: opened_pages.append((page, browser)))
|
||||
|
||||
assert driver.open_shop("target") is fake_browser
|
||||
assert driver.store_id == "oauth-2"
|
||||
assert opened_pages == [("https://launcher", fake_browser)]
|
||||
|
||||
|
||||
def test_switching_countries_returns_true_when_already_current():
|
||||
current_country = FakeElement(text="西班牙")
|
||||
driver = base.AmamzonBase({})
|
||||
driver.browser = object()
|
||||
driver.tab = FakeTab(
|
||||
ele_returns={
|
||||
'xpath://div[@class="dropdown-account-switcher-header-label"]/span[last()]': [current_country],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver.switch_to_country("西班牙") is True
|
||||
|
||||
|
||||
def test_switching_countries_returns_false_when_target_missing(monkeypatch):
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
driver = base.AmamzonBase({})
|
||||
driver.browser = object()
|
||||
driver.tab = FakeTab(
|
||||
ele_returns={
|
||||
'xpath://div[@class="dropdown-account-switcher-header-label"]/span[last()]': [FakeElement(text="英国")],
|
||||
'xpath://div[@class="dropdown-account-switcher-header-label"]': [FakeElement()],
|
||||
'xpath://div[@class="dropdown-account-switcher-list-item"]': [FakeElement()],
|
||||
'xpath://div[@class="dropdown-account-switcher-list-item dropdown-account-switcher-list-item-indented" and @title="西班牙"]': [
|
||||
None
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver.switch_to_country("西班牙") is False
|
||||
|
||||
|
||||
def test_switching_countries_clicks_and_verifies_target(monkeypatch):
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
dropdown = FakeElement()
|
||||
first_item = FakeElement()
|
||||
target = FakeElement()
|
||||
driver = base.AmamzonBase({})
|
||||
driver.browser = object()
|
||||
driver.tab = FakeTab(
|
||||
ele_returns={
|
||||
'xpath://div[@class="dropdown-account-switcher-header-label"]/span[last()]': [
|
||||
FakeElement(text="英国"),
|
||||
FakeElement(text="西班牙"),
|
||||
],
|
||||
'xpath://div[@class="dropdown-account-switcher-header-label"]': [dropdown],
|
||||
'xpath://div[@class="dropdown-account-switcher-list-item"]': [first_item],
|
||||
'xpath://div[@class="dropdown-account-switcher-list-item dropdown-account-switcher-list-item-indented" and @title="西班牙"]': [
|
||||
target
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver.switch_to_country("西班牙") is True
|
||||
assert dropdown.clicks == 1
|
||||
assert first_item.clicks == 1
|
||||
assert target.clicks == 1
|
||||
|
||||
|
||||
def test_open_country_dropdown_waits_for_delayed_country_list(monkeypatch):
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
dropdown = FakeElement()
|
||||
first_item = FakeElement()
|
||||
driver = base.AmamzonBase({})
|
||||
driver.tab = FakeTab(
|
||||
ele_returns={
|
||||
'xpath://div[@class="dropdown-account-switcher-header-label"]': [dropdown],
|
||||
'xpath://div[@class="dropdown-account-switcher-list-item"]': [None, first_item],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver._open_country_dropdown() is True
|
||||
assert dropdown.clicks == 2
|
||||
assert first_item.clicks == 1
|
||||
|
||||
|
||||
def test_login_returns_true_when_otp_submit_succeeds(monkeypatch):
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
password_input = FakeElement()
|
||||
password_submit = FakeElement()
|
||||
otp_input = FakeElement(value="123456")
|
||||
otp_submit = FakeElement()
|
||||
driver = base.AmamzonBase({})
|
||||
driver.tab = FakeTab(
|
||||
eles_returns={
|
||||
'xpath://input[@type="password"]': [[password_input]],
|
||||
'xpath://input[@id="signInSubmit"]': [[password_submit]],
|
||||
'xpath://span[@id="auth-send-code" and contains(string(.),"发送一次性密码")]': [[]],
|
||||
'xpath://input[@name="otpCode"]': [[otp_input]],
|
||||
'xpath://input[@id="auth-signin-button"]': [[otp_submit]],
|
||||
'xpath://div[@id="auth-error-message-box"]': [[]],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver.login("secret") is True
|
||||
assert password_input.inputs == [("secret", True)]
|
||||
assert password_submit.clicks == 1
|
||||
assert otp_submit.clicks == 1
|
||||
|
||||
|
||||
def test_login_refreshes_on_otp_error(monkeypatch):
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
otp_input = FakeElement(value="123456")
|
||||
driver = base.AmamzonBase({})
|
||||
driver.tab = FakeTab(
|
||||
eles_returns={
|
||||
'xpath://input[@type="password"]': [[]],
|
||||
'xpath://input[@id="signInSubmit"]': [[]],
|
||||
'xpath://span[@id="auth-send-code" and contains(string(.),"发送一次性密码")]': [[]],
|
||||
'xpath://input[@name="otpCode"]': [[otp_input]],
|
||||
'xpath://input[@id="auth-signin-button"]': [[FakeElement()]],
|
||||
'xpath://div[@id="auth-error-message-box"]': [[FakeElement(text="验证码错误")]],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver.login("secret") is False
|
||||
assert driver.tab.refreshes == 1
|
||||
|
||||
|
||||
def test_login_returns_false_when_no_otp_input(monkeypatch):
|
||||
monkeypatch.setattr(base.time, "sleep", lambda seconds: None)
|
||||
driver = base.AmamzonBase({})
|
||||
driver.tab = FakeTab(
|
||||
eles_returns={
|
||||
'xpath://input[@type="password"]': [[]],
|
||||
'xpath://input[@id="signInSubmit"]': [[]],
|
||||
'xpath://span[@id="auth-send-code" and contains(string(.),"发送一次性密码")]': [[]],
|
||||
'xpath://input[@name="otpCode"]': [[]],
|
||||
'xpath://input[@id="auth-signin-button"]': [[]],
|
||||
}
|
||||
)
|
||||
|
||||
assert driver.login("secret") is False
|
||||
|
||||
|
||||
def test_login_returns_false_on_exception():
|
||||
driver = base.AmamzonBase({})
|
||||
driver.tab = FakeTab(raise_on_doc_loaded=True)
|
||||
|
||||
assert driver.login("secret") is False
|
||||
1028
app/tests/test_patrol_delete.py
Normal file
1028
app/tests/test_patrol_delete.py
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
1501
app/uv.lock
generated
Normal file
1501
app/uv.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user