unicorn-rpa
Version:
🦄 Unicorn RPA - 强大的跨域iframe自动化控制工具
93 lines (74 loc) • 3.18 kB
JavaScript
const { log, errorLog, delay, safeQuerySelector, safeQuerySelectorAll } = require('../utils');
const {
performSecurityValidation,
createSecureResponse,
getSecurityStats
} = require('../utils/security');
const actionHandlers = require('../actions');
function injectRPA() {
window.addEventListener('message', async (event) => {
// 基本消息格式检查
if (!event.data || !event.data.actions) {
return;
}
const messageData = event.data;
const actions = messageData.actions;
const sessionId = messageData.sessionId;
log('收到自动化指令,会话ID:', sessionId, '动作数量:', (actions && actions.length) || 0);
try {
// 执行完整的安全验证(包括来源验证、频率限制和动作类型验证)
const securityResult = performSecurityValidation(event, actions);
if (!securityResult.valid) {
const errorResponse = createSecureResponse(false, sessionId, null, securityResult.error);
window.postMessage(errorResponse, window.location.origin);
errorLog('安全验证失败:', securityResult.error);
if (securityResult.unsupportedType) {
errorLog('不支持的动作类型:', securityResult.unsupportedType);
}
return;
}
log('安全验证通过,开始执行自动化动作');
// 执行自动化动作
const frameDoc = document;
let results = [];
for (let i = 0; i < actions.length; i++) {
const action = actions[i];
try {
log(`执行第${i + 1}/${actions.length}个动作:`, action.type);
const handler = actionHandlers[action.type];
if (!handler) {
throw new Error(`不支持的动作类型: ${action.type}`);
}
await handler(action, frameDoc, results);
log(`第${i + 1}个动作执行成功`);
} catch (actionError) {
errorLog(`第${i + 1}个动作执行出错:`, actionError);
throw actionError;
}
}
// 发送成功响应
const successResponse = createSecureResponse(true, sessionId, results);
window.postMessage(successResponse, window.location.origin);
log('自动化流程执行完成,会话ID:', sessionId);
} catch (err) {
errorLog('自动化流程异常:', err);
// 发送错误响应
const errorResponse = createSecureResponse(false, sessionId, null, err.message);
window.postMessage(errorResponse, window.location.origin);
}
});
// 添加安全统计信息获取接口
window.addEventListener('message', (event) => {
if (event.data && event.data.type === 'getSecurityStats') {
const stats = getSecurityStats();
const response = {
type: 'securityStatsResult',
stats,
timestamp: new Date().toISOString()
};
window.postMessage(response, window.location.origin);
}
});
log('Unicorn RPA 初始化完成,版本: 1.0.22,安全策略已启用');
}
module.exports = injectRPA;