correct tab detect method

This commit is contained in:
2025-10-27 16:20:21 +07:00
parent 8a44b67d0c
commit d770780a9d
2 changed files with 53 additions and 42 deletions

View File

@@ -1 +1,3 @@
console.log("Hello World 555"); console.log("Hello World 555");
// example debugger statement to pause the execution of the script
debugger

View File

@@ -1,18 +1,22 @@
const axios = require('axios'); const axios = require("axios");
const fs = require('fs'); const fs = require("fs");
const path = require('path'); const path = require("path");
async function executeScriptOnCurrentTab(scriptFile) { async function executeScriptOnCurrentTab(scriptFile) {
try { try {
// Get list of tabs from Chrome DevTools Protocol // Get list of tabs from Chrome DevTools Protocol
const response = await axios.get('http://localhost:9222/json'); const response = await axios.get("http://localhost:9222/json");
const tabs = response.data; const tabs = response.data;
// Find the first page tab (not extensions or devtools) // Find the first page tab (not extensions or devtools)
const targetTab = tabs.find(tab => tab.type === 'page'); const targetTab = tabs.find(
(tab) => tab.type === "page" && !tab.title.includes("DevTools")
);
if (!targetTab) { if (!targetTab) {
console.error('No active page tab found. Make sure Chrome is running with --remote-debugging-port=9222'); console.error(
"No active page tab found. Make sure Chrome is running with --remote-debugging-port=9222"
);
process.exit(1); process.exit(1);
} }
@@ -20,36 +24,41 @@ async function executeScriptOnCurrentTab(scriptFile) {
console.log(`URL: ${targetTab.url}`); console.log(`URL: ${targetTab.url}`);
// Read the script content // Read the script content
const scriptContent = fs.readFileSync(scriptFile, 'utf-8'); const scriptContent = fs.readFileSync(scriptFile, "utf-8");
// Connect to the WebSocket debugger URL // Connect to the WebSocket debugger URL
const WebSocket = require('ws'); const WebSocket = require("ws");
const ws = new WebSocket(targetTab.webSocketDebuggerUrl); const ws = new WebSocket(targetTab.webSocketDebuggerUrl);
ws.on('open', () => { ws.on("open", () => {
console.log('Connected to Chrome DevTools'); console.log("Connected to Chrome DevTools");
// Send Runtime.evaluate command to execute the script // Send Runtime.evaluate command to execute the script
ws.send(JSON.stringify({ ws.send(
id: 1, JSON.stringify({
method: 'Runtime.evaluate', id: 1,
params: { method: "Runtime.evaluate",
expression: scriptContent, params: {
returnByValue: true expression: scriptContent,
} returnByValue: true,
})); },
})
);
}); });
ws.on('message', (data) => { ws.on("message", (data) => {
const message = JSON.parse(data); const message = JSON.parse(data);
if (message.id === 1) { if (message.id === 1) {
if (message.result.exceptionDetails) { if (message.result.exceptionDetails) {
console.error('Script execution error:', message.result.exceptionDetails); console.error(
"Script execution error:",
message.result.exceptionDetails
);
} else { } else {
console.log('Script executed successfully'); console.log("Script executed successfully");
if (message.result.result.value !== undefined) { if (message.result.result.value !== undefined) {
console.log('Result:', message.result.result.value); console.log("Result:", message.result.result.value);
} }
} }
ws.close(); ws.close();
@@ -57,17 +66,18 @@ async function executeScriptOnCurrentTab(scriptFile) {
} }
}); });
ws.on('error', (error) => { ws.on("error", (error) => {
console.error('WebSocket error:', error); console.error("WebSocket error:", error);
process.exit(1); process.exit(1);
}); });
} catch (error) { } catch (error) {
if (error.code === 'ECONNREFUSED') { if (error.code === "ECONNREFUSED") {
console.error('Could not connect to Chrome. Make sure Chrome is running with:'); console.error(
console.error('chrome.exe --remote-debugging-port=9222'); "Could not connect to Chrome. Make sure Chrome is running with:"
);
console.error("chrome.exe --remote-debugging-port=9222");
} else { } else {
console.error('Error:', error.message); console.error("Error:", error.message);
} }
process.exit(1); process.exit(1);
} }
@@ -76,7 +86,7 @@ async function executeScriptOnCurrentTab(scriptFile) {
const scriptFile = process.argv[2]; const scriptFile = process.argv[2];
if (!scriptFile) { if (!scriptFile) {
console.error('Usage: node run-script.js <script-file>'); console.error("Usage: node run-script.js <script-file>");
process.exit(1); process.exit(1);
} }
@@ -86,4 +96,3 @@ if (!fs.existsSync(scriptFile)) {
} }
executeScriptOnCurrentTab(scriptFile); executeScriptOnCurrentTab(scriptFile);