Skip to content

Commit

Permalink
fix(vscodePlugin): prevent the default redirect behavior of links in …
Browse files Browse the repository at this point in the history
…vscode webview (#973)
  • Loading branch information
RSS1102 authored Nov 13, 2024
1 parent 67c5ae5 commit 8f9a319
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions vscodePlugin/web-resources/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,25 @@ const customMenuFont = Cherry.createMenuHook('字体样式', {


/** 处理 a 链接跳转问题 */
const onClickLink = (target) => {
const onClickLink = (e, target) => {


// 这里不能直接使用 target.href,因为本地相对文件地址会被vscode转成`webview://`协议
const href = target.attributes?.href.value;
if (!href) {

const hrefValidation = href ? href : 'href-invalid';
if (isHttpUrl(hrefValidation) || hrefValidation) {
// 阻止a链接在webview的默认跳转行为
e.preventDefault();
vscode.postMessage({
type: 'open-url',
data: 'href-invalid',
data: href,
});
};
return;
}
vscode.postMessage({
type: 'open-url',
data: href,
data: 'href-invalid',
});
};

Expand Down Expand Up @@ -266,11 +273,11 @@ const basicConfig = {
switch (target?.nodeName) {
case 'SPAN':
if (target?.parentElement?.nodeName === 'A') {
onClickLink(target?.parentElement);
onClickLink(e, target?.parentElement);
}
break;
case 'A':
onClickLink(target);
onClickLink(e, target);
break;
};
},
Expand Down

0 comments on commit 8f9a319

Please sign in to comment.