|
|
@@ -8,9 +8,16 @@
|
|
|
*/
|
|
|
|
|
|
|
|
|
-//C:\Program Files\Google\Chrome\Application\chrome.exe --auto-open-devtools-for-tabs --load-extension=D:\mayun\LR04.02_RVCTerminalPlus\addin\res\VTMModifyHeaders "file:///D:/redirect.html?url=http://oa.cmbchina.com"
|
|
|
+//C:\Program Files\Google\Chrome\Application\chrome.exe --auto-open-devtools-for-tabs --load-extension=D:\mayun\LR04.02_RVCTerminalPlus_uos\addin\res\VTMModifyHeaders "file:///D:/redirect.html?url=http://oa.cmbchina.com"
|
|
|
+
|
|
|
+//"C:\Program Files\Google\Chrome\Application\chrome.exe" --auto-open-devtools-for-tabs --load-extension=D:\mayun\LR04.02_RVCTerminalPlus_uos\addin\res\VTMModifyHeaders "file:///D:/mayun/LR04.02_RVCTerminalPlus_uos/addin/res/ManagerDesktop/redirect.html?redirect_open=http://oa.cmbchina.com&redirect_type=ad"
|
|
|
|
|
|
"use strict";
|
|
|
+// 创建自定义事件发射器
|
|
|
+const eventBus = new EventTarget();
|
|
|
+
|
|
|
+// 定义事件名
|
|
|
+const WS_CALLBACK_COMPLETE = 'wsCallbackComplete';
|
|
|
|
|
|
let config;
|
|
|
let started = 'on';
|
|
|
@@ -18,6 +25,7 @@ let debug_mode = true;
|
|
|
const isChrome = true;
|
|
|
let config_read_type = 'websocket';//local,file,websocket,http
|
|
|
const wsUrl = 'ws://127.0.0.1:9002?name=vtm_modify_header';
|
|
|
+const wsLoggerUrl = 'ws://127.0.0.1:9002?name=header_';
|
|
|
let socket = null;
|
|
|
let isExtensionReady = false;
|
|
|
|
|
|
@@ -25,7 +33,113 @@ function delay(ms) {
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
}
|
|
|
|
|
|
+class WebSocketLogger {
|
|
|
+ constructor(options) {
|
|
|
+ // 默认配置
|
|
|
+ const defaults = {
|
|
|
+ url: wsLoggerUrl,
|
|
|
+ reconnectInterval: 5000,
|
|
|
+ maxReconnectAttempts: 3,
|
|
|
+ logging: true
|
|
|
+ };
|
|
|
+
|
|
|
+ this.config = { ...defaults, ...options };
|
|
|
+ this.ws = null;
|
|
|
+ this.reconnectAttempts = 0;
|
|
|
+ this.isConnected = false;
|
|
|
+ this.messageQueue = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化WebSocket连接
|
|
|
+ connect() {
|
|
|
+ this.ws = new WebSocket(this.config.url);
|
|
|
+
|
|
|
+ this.ws.onopen = () => {
|
|
|
+ this.isConnected = true;
|
|
|
+ this.reconnectAttempts = 0;
|
|
|
+ console.log('WebSocket connected');
|
|
|
+ this._flushMessageQueue(); // 发送积压的消息
|
|
|
+ };
|
|
|
+
|
|
|
+ this.ws.onclose = () => {
|
|
|
+ this.isConnected = false;
|
|
|
+ console.log('WebSocket disconnected');
|
|
|
+ this._attemptReconnect();
|
|
|
+ };
|
|
|
+
|
|
|
+ this.ws.onerror = (error) => {
|
|
|
+ console.error('WebSocket error:', error);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试重新连接
|
|
|
+ _attemptReconnect() {
|
|
|
+ if (this.reconnectAttempts < this.config.maxReconnectAttempts) {
|
|
|
+ this.reconnectAttempts++;
|
|
|
+ console.log(`Reconnecting attempt ${this.reconnectAttempts}...`);
|
|
|
+ setTimeout(() => this.connect(), this.config.reconnectInterval);
|
|
|
+ } else {
|
|
|
+ console.error('Max reconnection attempts reached');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送日志消息
|
|
|
+ log(payload) {
|
|
|
+ const defaultPayload = {
|
|
|
+ messageType: 3080198,
|
|
|
+ EntityName: 'header_log',
|
|
|
+ ResultMsg: '',
|
|
|
+ LogType: 'Sys',
|
|
|
+ CostTime: 0,
|
|
|
+ ResultCode: '',
|
|
|
+ LogCode: '',
|
|
|
+ API: '',
|
|
|
+ SourceType: '',
|
|
|
+ BussID: '',
|
|
|
+ TipMsg: ''
|
|
|
+ };
|
|
|
+
|
|
|
+ const message = { ...defaultPayload, ...payload };
|
|
|
+
|
|
|
+ if (this.isConnected) {
|
|
|
+ this._sendMessage(message);
|
|
|
+ } else {
|
|
|
+ this.messageQueue.push(message); // 先存入队列
|
|
|
+ if (this.messageQueue.length === 1) {
|
|
|
+ this.connect(); // 首次断连时尝试重连
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 实际发送消息
|
|
|
+ _sendMessage(message) {
|
|
|
+ try {
|
|
|
+ this.ws.send(JSON.stringify(message));
|
|
|
+ if (this.config.logging) {
|
|
|
+ console.log('Log sent:', message);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Failed to send log:', error);
|
|
|
+ this.messageQueue.push(message); // 失败后重新入队
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ // 发送队列中的积压消息
|
|
|
+ _flushMessageQueue() {
|
|
|
+ while (this.messageQueue.length > 0 && this.isConnected) {
|
|
|
+ const message = this.messageQueue.shift();
|
|
|
+ this._sendMessage(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭连接
|
|
|
+ close() {
|
|
|
+ if (this.ws) {
|
|
|
+ this.ws.close();
|
|
|
+ this.isConnected = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
loadConfigurationFromLocalStorage();
|
|
|
|
|
|
@@ -42,14 +156,14 @@ function connectWebSocket(callback_function) {
|
|
|
try {
|
|
|
socket = new WebSocket(wsUrl);
|
|
|
} catch (error) {
|
|
|
- log('WebSocket connection creation error: ' + error);
|
|
|
+ console.log('WebSocket connection creation error: ' + error);
|
|
|
callback_function(null); // 连接创建失败,回调 null
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
socket.onmessage = function (event) {
|
|
|
- log('Received message:' + event.data);
|
|
|
+ console.log('Received message:' + event.data);
|
|
|
clearTimeout(timeoutId); // 清除定时器
|
|
|
if (socket && socket.readyState === WebSocket.OPEN) { // 检查连接状态
|
|
|
socket.close();
|
|
|
@@ -59,16 +173,16 @@ function connectWebSocket(callback_function) {
|
|
|
};
|
|
|
|
|
|
socket.onopen = function () {
|
|
|
- log('WebSocket connection established');
|
|
|
+ console.log('WebSocket connection established');
|
|
|
connectionOpened = true; // 设置连接已打开标志
|
|
|
// 发送配置请求
|
|
|
let requestData = '{"messageType":131073}';
|
|
|
- log('Send message:' + requestData);
|
|
|
+ console.log('Send message:' + requestData);
|
|
|
socket.send(requestData);
|
|
|
};
|
|
|
|
|
|
socket.onerror = function (error) {
|
|
|
- log('WebSocket connection error:' + error);
|
|
|
+ console.log('WebSocket connection error:' + error);
|
|
|
clearTimeout(timeoutId); // 清除定时器
|
|
|
if (socket && socket.readyState === WebSocket.OPEN) { // 检查连接状态
|
|
|
socket.close();
|
|
|
@@ -77,7 +191,7 @@ function connectWebSocket(callback_function) {
|
|
|
};
|
|
|
|
|
|
socket.onclose = function (event) {
|
|
|
- log('WebSocket connection closed:' + event.reason);
|
|
|
+ console.log('WebSocket connection closed:' + event.reason);
|
|
|
clearTimeout(timeoutId); // 清除定时器
|
|
|
connectionOpened = false; // 重置连接已打开标志
|
|
|
if (event.reason !== 'Connection timed out') { // 如果不是超时关闭
|
|
|
@@ -102,67 +216,97 @@ function extractUrlFromFileProtocol(fileUrl) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+function extractTypeFromFileProtocol(fileUrl) {
|
|
|
+ try {
|
|
|
+ const urlObj = new URL(fileUrl);
|
|
|
+ const params = new URLSearchParams(urlObj.search);
|
|
|
+ const targetUrl = params.get('redirect_type');
|
|
|
+
|
|
|
+ // 验证提取的 URL 是否合法
|
|
|
+ if (targetUrl) {
|
|
|
+ return targetUrl;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ return "unknown";
|
|
|
+ } catch (e) {
|
|
|
+ console.error('Failed to parse file:// URL:', e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+}
|
|
|
|
|
|
|
|
|
function loadConfigurationFromLocalStorage() {
|
|
|
- if (config_read_type == 'local') {
|
|
|
- let headers = [];
|
|
|
- headers.push({ url_contains: "", action: "add", header_name: "terminalno", header_value: "7555980178", comment: "test", apply_on: "req", status: "on" });
|
|
|
- config = { format_version: "1.1", target_page: "", headers: headers, debug_mode: true, use_url_excepts: false };
|
|
|
- log("current new config" + JSON.stringify(config));
|
|
|
- storeInBrowserStorage({ config: JSON.stringify(config) });
|
|
|
- started = 'on';
|
|
|
- }
|
|
|
- else if (config_read_type == 'file') {
|
|
|
- log('loadConfigurationFromLocalStorage');
|
|
|
- const filePath = 'file:///D:/Run/runinfo/runcfg/config.json'; // 指定本地文件路径
|
|
|
- fetch(filePath)
|
|
|
- .then(response => response.json())
|
|
|
- .then(data => {
|
|
|
- console.log('Current new config:', JSON.stringify(data));
|
|
|
- config = data;
|
|
|
- storeInBrowserStorage({ config: JSON.stringify(data) }); // 如果不需要存储到浏览器存储中,可以注释掉这行
|
|
|
- started = 'on';
|
|
|
- })
|
|
|
- .catch(error => {
|
|
|
- console.error('Error reading file:', error);
|
|
|
- });
|
|
|
- }
|
|
|
- else if (config_read_type == 'websocket') {
|
|
|
+ let isSuc = false;
|
|
|
+ let dstWs_logger = wsLoggerUrl;
|
|
|
+ let currentUrl = "";
|
|
|
+ if (config_read_type == 'websocket') {
|
|
|
connectWebSocket(function (dataStr) {
|
|
|
if (dataStr == null)
|
|
|
return;
|
|
|
let data = JSON.parse(dataStr);
|
|
|
log("current new config:" + JSON.stringify(data));
|
|
|
- config = data
|
|
|
- storeInBrowserStorage({ config: JSON.stringify(data) });
|
|
|
- started = 'on';
|
|
|
-
|
|
|
+
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
|
if (tabs[0]) {
|
|
|
- const currentUrl = tabs[0].url; // 当前标签页的 URL
|
|
|
+ currentUrl = tabs[0].url; // 当前标签页的 URL
|
|
|
console.log("当前 URL:", currentUrl);
|
|
|
+ const urlType = extractTypeFromFileProtocol(currentUrl);
|
|
|
+ dstWs_logger = dstWs_logger + urlType;
|
|
|
+
|
|
|
// 规则1: 如果是 file:// 开头,提取 url= 参数并跳转
|
|
|
if (currentUrl.startsWith('file:///')) {
|
|
|
console.log("begin with file:///");
|
|
|
- const urlParam = extractUrlFromFileProtocol(currentUrl);
|
|
|
+ const urlParam = extractUrlFromFileProtocol(currentUrl);
|
|
|
+ console.log("解析的url为:" + urlParam);
|
|
|
if (urlParam) {
|
|
|
+ // 使插件生效
|
|
|
+ config = data // modify config which make
|
|
|
+ storeInBrowserStorage({ config: JSON.stringify(data) });
|
|
|
+ started = 'on';
|
|
|
+
|
|
|
console.log("重载为:", urlParam);
|
|
|
chrome.tabs.update(tabs[0].id, { url: urlParam });
|
|
|
-
|
|
|
+
|
|
|
+ isSuc = true;
|
|
|
+ eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
|
|
|
+ detail: { success: true, data: "操作完成" }
|
|
|
+ }));
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ else {
|
|
|
+ isSuc = false
|
|
|
+ started = 'off';
|
|
|
+ eventBus.dispatchEvent(new CustomEvent(WS_CALLBACK_COMPLETE, {
|
|
|
+ detail: { success: false, data: "操作失败" }
|
|
|
+ }));
|
|
|
+ }
|
|
|
}
|
|
|
- });
|
|
|
-
|
|
|
-
|
|
|
+ });
|
|
|
/*
|
|
|
chrome.tabs.query({ active: true }, (tabs) => {
|
|
|
chrome.tabs.update(tabs[0].id, { url: "https://oa.cmbchina.com" });
|
|
|
});
|
|
|
*/
|
|
|
});
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ eventBus.addEventListener(WS_CALLBACK_COMPLETE, async (e) => {
|
|
|
+ const logger = new WebSocketLogger({
|
|
|
+ url: dstWs_logger,
|
|
|
+ logging: true // 开启调试日志
|
|
|
+ });
|
|
|
+ if (e.success) {
|
|
|
+ logger.log({
|
|
|
+ ResultMsg: "重载为:" + urlParam
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ logger.log({
|
|
|
+ ResultMsg: "不运行插件, 当前url地址为:" + currentUrl
|
|
|
+ });
|
|
|
+ }
|
|
|
+ logger.close();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
|