TidGi-Desktop/features/supports/webContentsViewHelper.ts
lin onetwo 7f5e1aa0cc
Feat/allow watch fs change on git sync
* feat: Skip restart if file system watch is enabled - the watcher will handle file changes automatically

* fix: sometimes change sync interval not working

fixes #310

* fix: Return false on sync failure - no successful changes were made

fixes #558

* fix: step that is wrong

* feat: monitoring subwiki

* AI added waitForSSEReady

* Revert "AI added waitForSSEReady"

This reverts commit 983b1c623c.

* fix: error on frontend loading worker thread

* fix

* Update wiki.ts

* auto reload view and click subwiki icon

* Refactor sync echo prevention and improve logging

Removed frontend-side echo prevention logic in ipcSyncAdaptor, relying solely on backend file exclusion for echo prevention. Improved console log wrappers to preserve native behavior and added a log statement to setupSSE. Updated test steps and file modification logic to better simulate external edits without modifying timestamps. Added internal documentation on sync architecture.

* feat: deboucne and prevent data race when write file

* Update watch-filesystem-adaptor.ts

* rename camelcase

* Update filesystemPlugin.feature

* Fix sync interval timezone handling and add tests

Refactored syncDebounceInterval logic in Sync.tsx to be timezone-independent, ensuring correct interval storage and display across all timezones. Added comprehensive tests in Sync.timezone.test.ts to verify correct behavior and document previous timezone-related bugs. fixes #310

* i18n for notification

* Update index.tsx

* fix: potential symlinks problem of subwiki

* Update Sync.timezone.test.ts

* lint

* Implement backoff for file existence check

Refactor file existence check to use backoff strategy and add directory tree retrieval for error reporting.

* Update BACKOFF_OPTIONS with new configuration

* Update wiki.ts

* remove log

* Update wiki.ts

* fix: draft not move to sub

* Update filesystemPlugin.feature

* fix: routing tw logger to file

* Update filesystemPlugin.feature

* test: use id to check view load and sse load

* Optimize test steps and screenshot logic

Removed unnecessary short waits in filesystemPlugin.feature and increased wait time for tiddler state to settle. Updated application.ts to skip screenshots for wait steps, reducing redundant screenshots during test execution.

* Check if the WebContents is actually loaded and remove fake webContentsViewHelper.new.ts created by AI

* Update view.ts

* fix: prevent echo by exclude title

* test: Then file "Draft of '新条目'.tid" should not exist in "{tmpDir}/wiki/tiddlers"

* Revert "fix: prevent echo by exclude title"

This reverts commit 86aa838d24.

* fix: when move file to subwiki, delete old file

* fix: prevent ipc echo change back to frontend

* test: view might take longer to load

* fix: minor issues

* test: fix cleanup timeout

* Update cleanup.ts

* feat: capture webview screenshot

* Update filesystemPlugin.feature

* Update SyncArchitecture.md

* rename

* test: add some time to easy failed steps

* Separate logs by test scenario for easier debugging

* Update selectors for add and confirm buttons in tests

Changed the CSS selectors for the add tiddler and confirm buttons in the filesystem plugin feature tests to use :has() with icon classes. This improves selector robustness and aligns with UI changes.

* Ensure window has focus and is ready

* Update window.ts

* fix: webview screenshot capture prevent mini window to close

* fix: Failed to take screenshot: Error: ENAMETOOLONG: name too long, open '/home/runner/work/TidGi-Desktop/TidGi-Desktop/userData-test/logs/screenshots/Agent workflow - Create notes- update embeddings- then search/2025-10-30T11-46-28-891Z-I type -在 wiki 工作区创建一个名为 AI Agent Guide 的笔记-内容是-智能体是一种可以执行任务的AI系统-它可以使用工具-搜索信息并与用户交互- in -chat input- element with selec-PASSED-page.png'

* Update window.ts

* feat: remove deprecated symlink subwiki approach

* Update wiki.ts

* fix: remove AI buggy bring window to front cause mini window test to fail

* lint

* Adjust wait time for draft saving in filesystemPlugin

Increased wait time for file system plugin to save draft.

* Adjust wait time for tiddler state stabilization

Increased wait time to ensure tiddler state settles properly.

* Refactor release workflow to simplify dependency installation

Removed installation steps for x64 and arm64 dependencies, and adjusted the build process for plugins and native modules.

* Enhance wait for IPC in filesystemPlugin feature

Added a wait time to improve reliability of content update verification in CI.
2025-10-31 02:00:40 +08:00

348 lines
9.9 KiB
TypeScript

import { WebContentsView } from 'electron';
import fs from 'fs-extra';
import type { ElectronApplication } from 'playwright';
/**
* Get the first WebContentsView from any window
* Prioritizes main window, but will check all windows if needed
*/
async function getFirstWebContentsView(app: ElectronApplication) {
return await app.evaluate(async ({ BrowserWindow }) => {
const allWindows = BrowserWindow.getAllWindows();
// First try to find main window
const mainWindow = allWindows.find(w => !w.isDestroyed() && w.webContents?.getType() === 'window');
if (mainWindow?.contentView && 'children' in mainWindow.contentView) {
const children = (mainWindow.contentView as WebContentsView).children as WebContentsView[];
if (Array.isArray(children) && children.length > 0) {
const webContentsId = children[0]?.webContents?.id;
if (webContentsId) return webContentsId;
}
}
// If main window doesn't have a WebContentsView, check all windows
for (const window of allWindows) {
if (!window.isDestroyed() && window.contentView && 'children' in window.contentView) {
const children = (window.contentView as WebContentsView).children as WebContentsView[];
if (Array.isArray(children) && children.length > 0) {
const webContentsId = children[0]?.webContents?.id;
if (webContentsId) return webContentsId;
}
}
}
return null;
});
}
/**
* Execute JavaScript in the browser view
*/
async function executeInBrowserView<T>(
app: ElectronApplication,
script: string,
): Promise<T> {
const webContentsId = await getFirstWebContentsView(app);
if (!webContentsId) {
throw new Error('No WebContentsView found in main window');
}
return await app.evaluate(
async ({ webContents }, [id, scriptContent]) => {
const targetWebContents = webContents.fromId(id as number);
if (!targetWebContents) {
throw new Error('WebContents not found');
}
const result: T = await targetWebContents.executeJavaScript(scriptContent as string, true) as T;
return result;
},
[webContentsId, script],
);
}
/**
* Get text content from WebContentsView
*/
export async function getTextContent(app: ElectronApplication): Promise<string | null> {
try {
return await executeInBrowserView<string>(
app,
'document.body.textContent || document.body.innerText || ""',
);
} catch {
return null;
}
}
/**
* Get DOM content from WebContentsView
*/
export async function getDOMContent(app: ElectronApplication): Promise<string | null> {
try {
return await executeInBrowserView<string>(
app,
'document.documentElement.outerHTML || ""',
);
} catch {
return null;
}
}
/**
* Check if WebContentsView exists and is loaded
*/
export async function isLoaded(app: ElectronApplication): Promise<boolean> {
const webContentsId = await getFirstWebContentsView(app);
if (webContentsId === null) {
return false;
}
// Check if the WebContents is actually loaded
return await app.evaluate(
async ({ webContents }, id: number) => {
const targetWebContents = webContents.fromId(id);
if (!targetWebContents) {
return false;
}
// Check if the page has finished loading
return !targetWebContents.isLoading() && targetWebContents.getURL() !== '' && targetWebContents.getURL() !== 'about:blank';
},
webContentsId,
);
}
/**
* Click element containing specific text in browser view
*/
export async function clickElementWithText(
app: ElectronApplication,
selector: string,
text: string,
): Promise<void> {
const script = `
(function() {
try {
const selector = ${JSON.stringify(selector)};
const text = ${JSON.stringify(text)};
const elements = document.querySelectorAll(selector);
let found = null;
for (let i = 0; i < elements.length; i++) {
const elem = elements[i];
const elemText = elem.textContent || elem.innerText || '';
if (elemText.trim() === text.trim() || elemText.includes(text)) {
found = elem;
break;
}
}
if (!found) {
return { error: 'Element with text "' + text + '" not found in selector: ' + selector };
}
found.click();
return { success: true };
} catch (error) {
return { error: error.message || String(error) };
}
})()
`;
const result = await executeInBrowserView(app, script);
if (result && typeof result === 'object' && 'error' in result) {
throw new Error(String(result.error));
}
}
/**
* Click element in browser view
*/
export async function clickElement(app: ElectronApplication, selector: string): Promise<void> {
const script = `
(function() {
try {
const selector = ${JSON.stringify(selector)};
const elem = document.querySelector(selector);
if (!elem) {
return { error: 'Element not found: ' + selector };
}
elem.click();
return { success: true };
} catch (error) {
return { error: error.message || String(error) };
}
})()
`;
const result = await executeInBrowserView(app, script);
if (result && typeof result === 'object' && 'error' in result) {
throw new Error(String(result.error));
}
}
/**
* Type text in element in browser view
*/
export async function typeText(app: ElectronApplication, selector: string, text: string): Promise<void> {
const escapedSelector = selector.replace(/'/g, "\\'");
const escapedText = text.replace(/'/g, "\\'").replace(/\n/g, '\\n');
const script = `
(function() {
try {
const selector = '${escapedSelector}';
const text = '${escapedText}';
const elem = document.querySelector(selector);
if (!elem) {
return { error: 'Element not found: ' + selector };
}
elem.focus();
if (elem.tagName === 'TEXTAREA' || elem.tagName === 'INPUT') {
elem.value = text;
} else {
elem.textContent = text;
}
elem.dispatchEvent(new Event('input', { bubbles: true }));
elem.dispatchEvent(new Event('change', { bubbles: true }));
return { success: true };
} catch (error) {
return { error: error.message || String(error) };
}
})()
`;
const result = await executeInBrowserView(app, script);
if (result && typeof result === 'object' && 'error' in result) {
throw new Error(String(result.error));
}
}
/**
* Press key in browser view
*/
export async function pressKey(app: ElectronApplication, key: string): Promise<void> {
const escapedKey = key.replace(/'/g, "\\'");
const script = `
(function() {
const key = '${escapedKey}';
const keydownEvent = new KeyboardEvent('keydown', {
key: key,
code: key,
bubbles: true,
cancelable: true
});
document.activeElement?.dispatchEvent(keydownEvent);
const keyupEvent = new KeyboardEvent('keyup', {
key: key,
code: key,
bubbles: true,
cancelable: true
});
document.activeElement?.dispatchEvent(keyupEvent);
return true;
})()
`;
await executeInBrowserView(app, script);
}
/**
* Check if element exists in browser view
*/
export async function elementExists(app: ElectronApplication, selector: string): Promise<boolean> {
try {
// Check if selector contains :has-text() pseudo-selector
const hasTextMatch = selector.match(/^(.+):has-text\(['"](.+)['"]\)$/);
if (hasTextMatch) {
const baseSelector = hasTextMatch[1];
const textContent = hasTextMatch[2];
const script = `
(function() {
const elements = document.querySelectorAll('${baseSelector.replace(/'/g, "\\'")}');
for (const el of elements) {
if (el.textContent && el.textContent.includes('${textContent.replace(/'/g, "\\'")}')) {
return true;
}
}
return false;
})()
`;
return await executeInBrowserView<boolean>(app, script);
} else {
const script = `document.querySelector('${selector.replace(/'/g, "\\'")}') !== null`;
return await executeInBrowserView<boolean>(app, script);
}
} catch {
return false;
}
}
/**
* Capture screenshot of WebContentsView with timeout
* Returns true if screenshot capture started successfully, false if failed or timeout
* File writing continues asynchronously in background if capture succeeds
*/
export async function captureScreenshot(app: ElectronApplication, screenshotPath: string): Promise<boolean> {
try {
// Add timeout to prevent screenshot from blocking test execution
const timeoutPromise = new Promise<null>((resolve) => {
setTimeout(() => {
resolve(null);
}, 500);
});
const capturePromise = (async () => {
const webContentsId = await getFirstWebContentsView(app);
if (!webContentsId) {
return null;
}
const pngBufferData = await app.evaluate(
async ({ webContents }, id: number) => {
const targetWebContents = webContents.fromId(id);
if (!targetWebContents || targetWebContents.isDestroyed()) {
return null;
}
try {
const image = await targetWebContents.capturePage();
const pngBuffer = image.toPNG();
return Array.from(pngBuffer);
} catch {
return null;
}
},
webContentsId,
);
return pngBufferData;
})();
const result = await Promise.race([capturePromise, timeoutPromise]);
// If we got the screenshot data, write it to file asynchronously (fire and forget)
if (result && Array.isArray(result)) {
fs.writeFile(screenshotPath, Buffer.from(result)).catch(() => {
// Silently ignore write errors
});
return true;
}
return false;
} catch {
return false;
}
}