javascript - 电子在浏览器中打开每个链接而不是电子本身
问题描述
我想在浏览器中而不是在电子中打开一个外部链接。因此我实现了这段代码:
document.addEventListener('click', function(event) {
if (event.target.tagName === 'A' && event.target.href.startsWith('http')) {
event.preventDefault();
shell.openExternal(event.target.href);
}
});
我现在的问题是我创建的每个链接都在浏览器中打开。
例如
<a href="http://google.com">foo</a>
<-想要在浏览器中打开的行为
<a href="#">bar</a>
<- 不希望在浏览器中打开行为
我怎样才能让它在浏览器中打开外部链接?
解决方案
<a href="#"></a>
实际上是href="http://localhost:X000/#"
这样 if 条件始终为真。用这个替换你的代码,它应该可以工作:
const { app, shell, BrowserWindow } = require("electron");
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1300,
height: 800,
});
// Load your React app or any other HTML which creates your Electron app content
mainWindow.loadFile("./build/index.html");
mainWindow.webContents.on("new-window", function(event, url) {
event.preventDefault();
// This will open a new Electron window to load the url.
shell.openExternal(url);
});
推荐阅读
- c# - 记录器和服务的 2 个项目中的 C#-代码,错误
- linux - 从第 n 级子文件夹复制或移动到另一个目录或文件夹
- python-3.x - 使用 write()、writelines()、to_csv() 的 Python 文件写入不起作用
- css - 媒体查询更改 Div 背景颜色但不更改其宽度?
- python - 焊点X射线图像中的特征检测和测量
- ffmpeg - 如何为 Windows 10 构建(编译)ffmpeg.exe
- c++ - 将解决方案从 Visual Studio 2008 迁移到 Visual Studio 2015
- android - 带有 align_parent_bottom 的材质卡与导航按钮重叠
- javascript - Stimulsoft 报告允许在使用 jsp 导出为 pdf 后编辑可编辑字段
- java - Mockito 无缘无故抛出 UnfinishedStubingException