好的,我明白你的意思了,我的插件也是通过点击来获取文件中diff,但是这样会导致diff页面跳转到对应点击文件。所有是否有办法拿到对应文件的diff而不进行跳转呢
下面是插件异步处理方法
// 温和的异步点击获取diff(进一步避免页面跳动)
async function gentleAsyncClickToLoadDiff(file, maxWaitMs = 8000) {
console.log(`[温和异步点击] 开始温和异步点击获取文件 ${file.fileName} 的diff`);
// 保存当前页面状态
const currentScrollPosition = window.scrollY;
const currentActiveElement = document.activeElement;
const currentSelection = window.getSelection().toString();
console.log(`[温和异步点击] 保存当前页面状态: scrollY=${currentScrollPosition}, activeElement=${currentActiveElement?.tagName}`);
// 检查是否有匹配的diff(文件名必须完全匹配)
const exactMatch = fileDiffMap[file.fileName];
if (exactMatch) {
console.log(`[温和异步点击] 文件 ${file.fileName} 已有完全匹配的diff缓存`);
return exactMatch;
}
// 检查是否有智能匹配的diff
const availableFiles = Object.keys(fileDiffMap);
const matchingDiffFile = availableFiles.find(availableFile => {
if (availableFile === '__single__') return false;
const pageFileName = file.fileName;
const apiFileName = availableFile;
const getFileName = (path) => path.split('/').pop().split('\\').pop();
const pageFileBase = getFileName(pageFileName);
const apiFileBase = getFileName(apiFileName);
const matches = [
pageFileName === apiFileName,
pageFileBase === apiFileBase,
apiFileName.includes(pageFileBase),
pageFileName.includes(apiFileBase),
pageFileName.includes(apiFileName) || apiFileName.includes(pageFileName)
];
return matches.some(match => match);
});
if (matchingDiffFile) {
console.log(`[温和异步点击] 文件 ${file.fileName} 找到智能匹配的diff: ${matchingDiffFile}`);
return fileDiffMap[matchingDiffFile];
}
// 清空之前的diff缓存,确保获取到当前文件的diff
const previousDiffMap = { ...fileDiffMap };
fileDiffMap = {};
console.log(`[温和异步点击] 清空diff缓存,准备加载 ${file.fileName} 的diff`);
try {
// 使用更温和的异步点击方式,进一步避免页面跳动
console.log(`[温和异步点击] 使用更温和的异步点击方式: ${file.fileName}`);
// 方法1: 使用更长的延迟和更温和的方式
const titleElement = file.element.querySelector('.teamix-title');
if (titleElement) {
console.log(`[温和异步点击] 找到文件标题元素:`, titleElement.tagName, titleElement.className);
// 使用更长的延迟和更温和的方式
return new Promise((resolve) => {
const gentleCallback = () => {
// 使用更温和的点击方式
try {
// 方法1: 使用dispatchEvent而不是click()
const clickEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
detail: 1,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0
});
titleElement.dispatchEvent(clickEvent);
console.log(`[温和异步点击] 温和点击完成: ${file.fileName}`);
} catch (error) {
console.warn(`[温和异步点击] 温和点击失败,尝试直接点击:`, error);
titleElement.click();
}
// 等待更长时间,确保diff加载完成
setTimeout(() => {
// 检查是否成功加载到匹配的diff
const newExactMatch = fileDiffMap[file.fileName];
if (newExactMatch) {
console.log(`[温和异步点击] 成功: 已获取到 ${file.fileName} 的diff`);
resolve(newExactMatch);
return;
}
// 检查智能匹配
const newMatchingDiffFile = Object.keys(fileDiffMap).find(availableFile => {
if (availableFile === '__single__') return false;
const pageFileName = file.fileName;
const apiFileName = availableFile;
const getFileName = (path) => path.split('/').pop().split('\\').pop();
const pageFileBase = getFileName(pageFileName);
const apiFileBase = getFileName(apiFileName);
const matches = [
pageFileName === apiFileName,
pageFileBase === apiFileBase,
apiFileName.includes(pageFileBase),
pageFileName.includes(apiFileBase),
pageFileName.includes(apiFileName) || apiFileName.includes(pageFileName)
];
return matches.some(match => match);
});
if (newMatchingDiffFile) {
console.log(`[温和异步点击] 成功: 找到智能匹配的diff: ${newMatchingDiffFile}`);
resolve(fileDiffMap[newMatchingDiffFile]);
return;
}
// 如果没有找到匹配的diff,返回null
console.log(`[温和异步点击] 未找到匹配的diff: ${file.fileName}`);
resolve(null);
}, 3000); // 增加等待时间到3秒,确保diff加载完成
};
// 使用更长的延迟,确保页面完全稳定
setTimeout(() => {
if (window.requestIdleCallback) {
window.requestIdleCallback(gentleCallback, { timeout: 5000 });
} else {
setTimeout(gentleCallback, 500);
}
}, 1000); // 额外等待1秒,确保页面稳定
});
}
// 方法2: 如果找不到标题元素,尝试其他可点击元素
console.log(`[温和异步点击] 方法2: 尝试其他可点击元素 ${file.fileName}`);
const clickableElement = file.element.querySelector('a, button, [role="button"], span');
if (clickableElement && !clickableElement.classList.contains('next-tree-node-indent-unit')) {
console.log(`[温和异步点击] 找到可点击元素:`, clickableElement.tagName, clickableElement.className);
return new Promise((resolve) => {
const gentleCallback = () => {
try {
const clickEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
detail: 1,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0
});
clickableElement.dispatchEvent(clickEvent);
console.log(`[温和异步点击] 温和点击可点击元素完成: ${file.fileName}`);
} catch (error) {
console.warn(`[温和异步点击] 温和点击失败,尝试直接点击:`, error);
clickableElement.click();
}
setTimeout(() => {
// 检查结果
const newExactMatch = fileDiffMap[file.fileName];
if (newExactMatch) {
console.log(`[温和异步点击] 方法2成功: 已获取到 ${file.fileName} 的diff`);
resolve(newExactMatch);
return;
}
const newMatchingDiffFile = Object.keys(fileDiffMap).find(availableFile => {
if (availableFile === '__single__') return false;
const pageFileName = file.fileName;
const apiFileName = availableFile;
const getFileName = (path) => path.split('/').pop().split('\\').pop();
const pageFileBase = getFileName(pageFileName);
const apiFileBase = getFileName(apiFileName);
const matches = [
pageFileName === apiFileName,
pageFileBase === apiFileBase,
apiFileName.includes(pageFileBase),
pageFileName.includes(apiFileBase),
pageFileName.includes(apiFileName) || apiFileName.includes(pageFileName)
];
return matches.some(match => match);
});
if (newMatchingDiffFile) {
console.log(`[温和异步点击] 方法2成功: 找到智能匹配的diff: ${newMatchingDiffFile}`);
resolve(fileDiffMap[newMatchingDiffFile]);
return;
}
console.log(`[温和异步点击] 方法2失败: 未找到匹配的diff: ${file.fileName}`);
resolve(null);
}, 3000);
};
setTimeout(() => {
if (window.requestIdleCallback) {
window.requestIdleCallback(gentleCallback, { timeout: 5000 });
} else {
setTimeout(gentleCallback, 500);
}
}, 1000);
});
}
// 方法3: 如果都找不到可点击元素,尝试直接点击文件元素
console.log(`[温和异步点击] 方法3: 尝试直接点击文件元素 ${file.fileName}`);
if (file.element && typeof file.element.click === 'function') {
return new Promise((resolve) => {
const gentleCallback = () => {
try {
const clickEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
detail: 1,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0
});
file.element.dispatchEvent(clickEvent);
console.log(`[温和异步点击] 温和点击文件元素完成: ${file.fileName}`);
} catch (error) {
console.warn(`[温和异步点击] 温和点击失败,尝试直接点击:`, error);
file.element.click();
}
setTimeout(() => {
// 检查结果
const newExactMatch = fileDiffMap[file.fileName];
if (newExactMatch) {
console.log(`[温和异步点击] 方法3成功: 已获取到 ${file.fileName} 的diff`);
resolve(newExactMatch);
return;
}
const newMatchingDiffFile = Object.keys(fileDiffMap).find(availableFile => {
if (availableFile === '__single__') return false;
const pageFileName = file.fileName;
const apiFileName = availableFile;
const getFileName = (path) => path.split('/').pop().split('\\').pop();
const pageFileBase = getFileName(pageFileName);
const apiFileBase = getFileName(apiFileName);
const matches = [
pageFileName === apiFileName,
pageFileBase === apiFileBase,
apiFileName.includes(pageFileBase),
pageFileName.includes(apiFileBase),
pageFileName.includes(apiFileName) || apiFileName.includes(pageFileName)
];
return matches.some(match => match);
});
if (newMatchingDiffFile) {
console.log(`[温和异步点击] 方法3成功: 找到智能匹配的diff: ${newMatchingDiffFile}`);
resolve(fileDiffMap[newMatchingDiffFile]);
return;
}
console.log(`[温和异步点击] 方法3失败: 未找到匹配的diff: ${file.fileName}`);
resolve(null);
}, 3000);
};
setTimeout(() => {
if (window.requestIdleCallback) {
window.requestIdleCallback(gentleCallback, { timeout: 5000 });
} else {
setTimeout(gentleCallback, 500);
}
}, 1000);
});
}
console.log(`[温和异步点击] 所有方法都失败: ${file.fileName}`);
return null;
} catch (error) {
console.error(`[温和异步点击] 点击文件 ${file.fileName} 时发生错误:`, error);
// 恢复之前的diff缓存
fileDiffMap = { ...previousDiffMap };
return null;
}
}
最新发布