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