Skip to content

Commit bae9e0b

Browse files
committed
feat: Enhance browser API and debugger functionality
- Add missing debugger API methods to browser API map - Refactor debugger attachment and command sending in workflow engine - Remove debug console logs - Enable debug mode in settings - Improve content script injection check
1 parent ecbad82 commit bae9e0b

File tree

7 files changed

+55
-20
lines changed

7 files changed

+55
-20
lines changed

src/components/newtab/shared/SharedCodemirror.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import { oneDark } from '@codemirror/theme-one-dark';
2626
import { keymap } from '@codemirror/view';
2727
import { EditorView, basicSetup } from 'codemirror';
2828
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
29+
// don't remove this unused import, the css is used in dynamic style
2930
import { store } from '../settings/jsBlockWrap';
3031
31-
console.log('🚀 ~ store:', store);
32-
3332
const props = defineProps({
3433
lang: {
3534
type: String,

src/components/newtab/workflow/settings/SettingsGeneral.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ const settingItems = [
189189
notSupport: ['firefox'],
190190
name: t('workflow.settings.debugMode.title'),
191191
description: t('workflow.settings.debugMode.description'),
192-
// FIXME: temporarily disable this function
193-
disabled: true,
194192
},
195193
{
196194
id: 'inputAutocomplete',

src/service/browser-api/BrowserAPIService.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ function sendBrowserApiMessage(name, ...args) {
3434
}
3535

3636
class BrowserContentScript {
37+
/**
38+
* Check if content script injected
39+
* @param {ScriptInjectTarget} target
40+
* @param {string=} messageId
41+
*/
42+
static async isContentScriptInjected(target, messageId) {
43+
if (!IS_BROWSER_API_AVAILABLE) {
44+
return sendBrowserApiMessage(
45+
'contentScript.isContentScriptInjected',
46+
...arguments
47+
);
48+
}
49+
50+
try {
51+
// 发送测试消息到目标标签页
52+
await Browser.tabs.sendMessage(
53+
target.tabId,
54+
{ type: messageId || 'content-script-exists' },
55+
{
56+
frameId: target.allFrames ? undefined : target.frameId,
57+
}
58+
);
59+
60+
return true;
61+
} catch (error) {
62+
return false;
63+
}
64+
}
65+
3766
/**
3867
* Inject content script into targeted tab
3968
* @param {Object} script
@@ -89,8 +118,8 @@ class BrowserContentScript {
89118
}
90119

91120
tryCount += 1;
92-
// isContentScriptInJected is not found
93-
const isInjected = await this.isContentScriptInjected(
121+
122+
const isInjected = await BrowserContentScript.isContentScriptInjected(
94123
target,
95124
waitUntilInjected.messageId
96125
);
@@ -187,6 +216,7 @@ class BrowserAPIService {
187216
(() => {
188217
browserAPIMap.forEach((item) => {
189218
let value;
219+
console.log('IS_BROWSER_API_AVAILABLE', IS_BROWSER_API_AVAILABLE);
190220
if (IS_BROWSER_API_AVAILABLE) {
191221
value = item.api();
192222
} else {

src/service/browser-api/browser-api-map.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export const browserAPIMap = [
5757
api: () => chrome.debugger.onEvent,
5858
},
5959
{ path: 'debugger.detach', api: () => chrome.debugger.detach },
60+
{ path: 'debugger.attach', api: () => chrome.debugger.attach },
61+
{ path: 'debugger.sendCommand', api: () => chrome.debugger.sendCommand },
6062
{ path: 'permissions.contains', api: () => Browser.permissions.contains },
6163
{ path: 'cookies.get', api: () => Browser.cookies?.get },
6264
{ path: 'cookies.getAll', api: () => Browser.cookies?.getAll },

src/workflowEngine/WorkflowEngine.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class WorkflowEngine {
127127
return;
128128
}
129129

130+
console.log('before execute', this.state, '\n', this.workflow);
131+
130132
const { nodes, edges } = this.workflow.drawflow;
131133
if (!nodes || nodes.length === 0) {
132134
console.error(`${this.workflow.name} doesn't have blocks`);
@@ -452,7 +454,7 @@ class WorkflowEngine {
452454
this.workers.forEach((worker) => {
453455
if (!worker.debugAttached) return;
454456

455-
chrome.debugger.detach({ tabId: worker.activeTab.id });
457+
BrowserAPIService.debugger.detach({ tabId: worker.activeTab.id });
456458
});
457459
}
458460

src/workflowEngine/WorkflowManager.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ class WorkflowManager {
5555
}
5656

5757
const convertedWorkflow = convertWorkflowData(workflowData);
58-
console.log(
59-
'🚀 ~ WorkflowManager ~ execute ~ convertedWorkflow:',
60-
convertedWorkflow
61-
);
6258
const engine = new WorkflowEngine(convertedWorkflow, {
6359
options,
6460
states: this.#state,

src/workflowEngine/helper.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,27 @@ export async function getFrames(tabId) {
6060

6161
export function sendDebugCommand(tabId, method, params = {}) {
6262
return new Promise((resolve) => {
63-
chrome.debugger.sendCommand({ tabId }, method, params, resolve);
63+
BrowserAPIService.debugger.sendCommand({ tabId }, method, params, resolve);
6464
});
6565
}
6666

67-
export function attachDebugger(tabId, prevTab) {
68-
return new Promise((resolve) => {
69-
if (prevTab && tabId !== prevTab)
70-
chrome.debugger.detach({ tabId: prevTab });
67+
export async function attachDebugger(tabId, prevTab) {
68+
try {
69+
if (prevTab && tabId !== prevTab) {
70+
await BrowserAPIService.debugger.detach({ tabId: prevTab });
71+
}
7172

72-
chrome.debugger.attach({ tabId }, '1.3', () => {
73-
chrome.debugger.sendCommand({ tabId }, 'Page.enable', resolve);
74-
});
75-
});
73+
// first attach
74+
await BrowserAPIService.debugger.attach({ tabId }, '1.3');
75+
76+
// and then Page.enable
77+
await BrowserAPIService.debugger.sendCommand({ tabId }, 'Page.enable');
78+
79+
return true;
80+
} catch (error) {
81+
console.error('Failed to attach debugger:', error);
82+
return false;
83+
}
7684
}
7785

7886
export function waitTabLoaded({ tabId, listenError = false, ms = 10000 }) {

0 commit comments

Comments
 (0)