1
This commit is contained in:
@@ -1,13 +1,6 @@
|
||||
// Amazon:action 壳(编排逻辑移至 amazon_tool.js)
|
||||
|
||||
import {
|
||||
injected_amazon_product_detail,
|
||||
injected_amazon_product_reviews,
|
||||
run_amazon_pdp_action,
|
||||
run_amazon_pdp_action_multi,
|
||||
run_amazon_search_list_action,
|
||||
run_amazon_set_language_action,
|
||||
} from './amazon_tool.js';
|
||||
import { injected_amazon_product_detail, injected_amazon_product_reviews, run_amazon_pdp_action, run_amazon_pdp_action_multi, run_amazon_search_list_action, run_amazon_set_language_action } from './amazon_tool.js';
|
||||
|
||||
const amazon_search_list_action = {
|
||||
desc: 'Amazon 搜索列表:先打开中文首页,搜索框输入类目并搜索,再分页抓取',
|
||||
|
||||
@@ -4,19 +4,12 @@
|
||||
*/
|
||||
|
||||
// Amazon 相关动作
|
||||
export { amazon_actions } from './amazon.js';
|
||||
import { amazon_actions } from './amazon.js';
|
||||
export { amazon_actions };
|
||||
|
||||
// Amazon 工具函数
|
||||
export {
|
||||
injected_amazon_validate_captcha_continue,
|
||||
is_amazon_validate_captcha_url,
|
||||
injected_amazon_product_detail,
|
||||
injected_amazon_product_reviews,
|
||||
run_amazon_pdp_action,
|
||||
run_amazon_pdp_action_multi,
|
||||
run_amazon_search_list_action,
|
||||
run_amazon_set_language_action
|
||||
} from './amazon_tool.js';
|
||||
import { injected_amazon_validate_captcha_continue, is_amazon_validate_captcha_url, injected_amazon_product_detail, injected_amazon_product_reviews, run_amazon_pdp_action, run_amazon_pdp_action_multi, run_amazon_search_list_action, run_amazon_set_language_action } from './amazon_tool.js';
|
||||
export { injected_amazon_validate_captcha_continue, is_amazon_validate_captcha_url, injected_amazon_product_detail, injected_amazon_product_reviews, run_amazon_pdp_action, run_amazon_pdp_action_multi, run_amazon_search_list_action, run_amazon_set_language_action };
|
||||
|
||||
// 便捷的统一导出对象
|
||||
export const Actions = {
|
||||
|
||||
@@ -2,31 +2,37 @@
|
||||
* 服务端 Puppeteer 通过此页与 background 通讯(等同 UI 发 chrome.runtime.sendMessage)
|
||||
* 页面内若需 Web Worker 做重计算,可在此 postMessage;当前直连 background 即可满足指令/结果
|
||||
*/
|
||||
(function () {
|
||||
function server_bridge_invoke(action, data) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
(() => {
|
||||
const server_bridge_invoke = (action, data) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!action) {
|
||||
reject(new Error('缺少 action'));
|
||||
return;
|
||||
}
|
||||
chrome.runtime.sendMessage({ action: action, data: data || {} }, function (res) {
|
||||
var err = chrome.runtime.lastError;
|
||||
if (err) {
|
||||
reject(new Error(err.message));
|
||||
return;
|
||||
|
||||
chrome.runtime.sendMessage(
|
||||
{ action, data: data || {} },
|
||||
(res) => {
|
||||
const err = chrome.runtime.lastError;
|
||||
if (err) {
|
||||
reject(new Error(err.message));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
reject(new Error('background 无响应'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.ok) {
|
||||
resolve(res.data);
|
||||
} else {
|
||||
reject(new Error(res.error || 'action 失败'));
|
||||
}
|
||||
}
|
||||
if (!res) {
|
||||
reject(new Error('background 无响应'));
|
||||
return;
|
||||
}
|
||||
if (res.ok) {
|
||||
resolve(res.data);
|
||||
} else {
|
||||
reject(new Error(res.error || 'action 失败'));
|
||||
}
|
||||
});
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
window.server_bridge_invoke = server_bridge_invoke;
|
||||
})();
|
||||
|
||||
@@ -4,14 +4,7 @@
|
||||
*/
|
||||
|
||||
// 方式1: 命名导入(推荐)
|
||||
import {
|
||||
ok_response,
|
||||
fail_response,
|
||||
create_tab_task,
|
||||
amazon_actions,
|
||||
getAllActionsMeta,
|
||||
getActionByName
|
||||
} from '../libs/index.js';
|
||||
import { ok_response, fail_response, create_tab_task, amazon_actions, getAllActionsMeta, getActionByName } from '../libs/index.js';
|
||||
|
||||
// 方式2: 默认导入使用对象
|
||||
import Libs from '../libs/index.js';
|
||||
|
||||
@@ -4,28 +4,16 @@
|
||||
*/
|
||||
|
||||
// 响应处理相关
|
||||
export {
|
||||
ok_response,
|
||||
fail_response,
|
||||
response_code,
|
||||
guard_sync
|
||||
} from './action_response.js';
|
||||
import { ok_response, fail_response, response_code, guard_sync } from './action_response.js';
|
||||
export { ok_response, fail_response, response_code, guard_sync };
|
||||
|
||||
// Tab 操作相关
|
||||
export {
|
||||
raw_execute_script,
|
||||
inject_file,
|
||||
ensure_injected,
|
||||
execute_script,
|
||||
open_tab,
|
||||
close_tab,
|
||||
create_tab_task
|
||||
} from './tabs.js';
|
||||
import { raw_execute_script, inject_file, ensure_injected, execute_script, open_tab, close_tab, create_tab_task } from './tabs.js';
|
||||
export { raw_execute_script, inject_file, ensure_injected, execute_script, open_tab, close_tab, create_tab_task };
|
||||
|
||||
// Action 元数据相关
|
||||
export {
|
||||
bind_action_meta
|
||||
} from './action_meta.js';
|
||||
import { bind_action_meta } from './action_meta.js';
|
||||
export { bind_action_meta };
|
||||
|
||||
// 便捷的统一导出对象(可选使用)
|
||||
export const Libs = {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
const out = document.getElementById('out');
|
||||
const btn = document.getElementById('btn');
|
||||
|
||||
function set_out(obj) {
|
||||
const set_out = (obj) => {
|
||||
out.textContent = typeof obj === 'string' ? obj : JSON.stringify(obj, null, 2);
|
||||
}
|
||||
};
|
||||
|
||||
btn.addEventListener('click', () => {
|
||||
set_out('执行中...');
|
||||
|
||||
@@ -9,21 +9,21 @@ const action_log_el = document.getElementById('action_log');
|
||||
let actions_meta = {};
|
||||
const ui_state = { last_result: null, actions: [] };
|
||||
|
||||
function now_time() {
|
||||
const now_time = () => {
|
||||
const d = new Date();
|
||||
const pad = (n) => String(n).padStart(2, '0');
|
||||
return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
||||
}
|
||||
};
|
||||
|
||||
function safe_json_parse(text) {
|
||||
const safe_json_parse = (text) => {
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (e) {
|
||||
return { __parse_error: e.message, __raw: text };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function pick_main_result(res) {
|
||||
const pick_main_result = (res) => {
|
||||
// 右侧只展示最核心的数据,避免被 ok/request_id 包裹层干扰
|
||||
if (res && res.ok && res.data) {
|
||||
// 约定:action 返回的核心结果放在 data.result(例如 amazon_search_list 的 stage=list)
|
||||
@@ -31,14 +31,14 @@ function pick_main_result(res) {
|
||||
return res.data;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
function render_state() {
|
||||
const render_state = () => {
|
||||
last_response_el.textContent = JSON.stringify(ui_state.last_result, null, 2);
|
||||
action_log_el.textContent = ui_state.actions.join('\n');
|
||||
}
|
||||
};
|
||||
|
||||
function push_action(obj) {
|
||||
const push_action = (obj) => {
|
||||
// 动作日志只保留单行文本,避免 JSON 换行太长
|
||||
const ts = now_time();
|
||||
const type = obj && obj.type ? String(obj.type) : 'action';
|
||||
@@ -64,7 +64,7 @@ function push_action(obj) {
|
||||
ui_state.actions.splice(0, ui_state.actions.length - 200);
|
||||
}
|
||||
render_state();
|
||||
}
|
||||
};
|
||||
|
||||
btn_run_el.addEventListener('click', () => {
|
||||
const action = action_name_el.value;
|
||||
|
||||
Reference in New Issue
Block a user