我正在开发一个Visual代码扩展,它将在某些事件上打开一个some视图。Webview有以下链接地址的一些链接:
reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_2
使用此URI,应该导航到外部Windows应用程序。当我在Chrome/Firefox浏览器中使用这个URI时,我能够导航到目标外部Windows应用程序。但是在我的扩展的when视图中,当我点击这个链接时,我得到了以下错误:
Uncaught Error: [UriError]: If a URI does not contain an authority component, then the path cannot begin with two slash characters ("//")
我尝试将所有"//“替换为"/”,以生成URI : reqtify:/NKR8WIN764PLP/Users/Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_2
但是,我仍然无法导航到外部Windows应用程序。
下面是我的呈现the视图的代码:
protected async doUpdate(): Promise<void> {
let content: string;
this.throttleTimer = undefined;
content = await this.contentProvider.provideRequirementsContent(document.uri,
this.state, this.editor.webview);
this.editor.webview.options = getWebviewOptions(this.contentProvider);
this.editor.webview.html = content;
}
protected static getWebviewOptions(contentProvider: BaseContentProvider
): vscode.WebviewOptions {
return {
enableScripts: true,
localResourceRoots: contentProvider.localResourceRoots
};
}
public async provideRequirementsContent(sourceUri: vscode.Uri, state: any, webview: any): Promise<string> {
let html = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<div class="sect2">
<h2>
<span class="numbering">1. </span>Requirements of Specifications</h2>
<table width="100%" align="center">
<colgroup>
<col width="80"></col>
<col width="1*"></col></colgroup>
<thead>
<tr>
<th>Requirement</th>
<th>Text</th></tr></thead>
<tbody>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_1">SPEC_1</a></td>
<td>For any of the cruise control (CC) functions to take effect, CC must be turned on first.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_2">SPEC_2</a></td>
<td>CC can be in the following states: off, enabled (i.e., on and cruising), and disabled (on, but not cruising).</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_3">SPEC_3</a></td>
<td>The CC system should be automatically disabled below 30mph and above 90mph.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_4">SPEC_4</a></td>
<td>Four actions are permitted during CC: set speed, accelerate, decelerate, and resume speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_5">SPEC_5</a></td>
<td>When the system is under CC and the brake is pressed, CC is disabled. When the resume button is pressed, the system resumes at the last set CC speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_6">SPEC_6</a></td>
<td>When the system is under CC and the accelerator pedal is pressed, CC is disabled and the speed increases correspondingly. When the accelerator is released, the CC resumes at its last set CC speed. If at any point of time during acceleration the CC speed is set, CC replaces the old set speed with the new speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_7">SPEC_7</a></td>
<td>If CC is enabled and the vehicle starts going uphill or downhill, CC should automatically apply the accelerator or brake to maintain the set speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_8">SPEC_8</a></td>
<td>You can assume an automatic transmission vehicule.</td></tr></tbody></table></div>
</body>
</html>
"
html = html.split('reqtify:////').join('reqtify:/');
html = html.split('//Public').join('/Public');
}我想导航到具有这些HTML内容链接的外部Windows应用程序。你能帮忙吗?
发布于 2019-08-23 06:40:50
经过一些研究,我设法解决了这个问题,但我不确定它是否正确的方式。
我在上面的HTML字符串中添加了一个脚本,其中包含了一个在body的load事件上调用的方法。
因此,我的最后一个HTML字符串将类似于:
let html = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body onload="myFunction()>
<div class="sect2">
<h2>
<span class="numbering">1. </span>Requirements of Specifications</h2>
<table width="100%" align="center">
<colgroup>
<col width="80"></col>
<col width="1*"></col></colgroup>
<thead>
<tr>
<th>Requirement</th>
<th>Text</th></tr></thead>
<tbody>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_1">SPEC_1</a></td>
<td>For any of the cruise control (CC) functions to take effect, CC must be turned on first.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_2">SPEC_2</a></td>
<td>CC can be in the following states: off, enabled (i.e., on and cruising), and disabled (on, but not cruising).</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_3">SPEC_3</a></td>
<td>The CC system should be automatically disabled below 30mph and above 90mph.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_4">SPEC_4</a></td>
<td>Four actions are permitted during CC: set speed, accelerate, decelerate, and resume speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_5">SPEC_5</a></td>
<td>When the system is under CC and the brake is pressed, CC is disabled. When the resume button is pressed, the system resumes at the last set CC speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_6">SPEC_6</a></td>
<td>When the system is under CC and the accelerator pedal is pressed, CC is disabled and the speed increases correspondingly. When the accelerator is released, the CC resumes at its last set CC speed. If at any point of time during acceleration the CC speed is set, CC replaces the old set speed with the new speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_7">SPEC_7</a></td>
<td>If CC is enabled and the vehicle starts going uphill or downhill, CC should automatically apply the accelerator or brake to maintain the set speed.</td></tr>
<tr>
<td><a href="reqtify:////NKR8WIN764PLP/Users//Public/Documents/Reqtify/2020x/Examples/Coupling/Code/Code%20C/Code%20Sample.rqtf?id=SPEC_8">SPEC_8</a></td>
<td>You can assume an automatic transmission vehicule.</td></tr></tbody></table></div>
<script>
function myFunction() {
const vscode = acquireVsCodeApi();
let reqs = document.getElementsByTagName('a');
for (let item of reqs) {
item.addEventListener('click', function() {
vscode.postMessage({
command: 'open',
text: item.getAttribute('href')
})
return false;
}, false);
}
}
</script>
</body>
</html>
"如果您观察到脚本,我将使用onDidReceiveMessage发送一条包含命令"open“和url作为数据的消息,该消息将由webview的onDidReceiveMessage方法接收,如下所示:
this.editor.webview.onDidReceiveMessage(message => {
switch (message.command) {
case 'open':
var url = message.text;
var start = (process.platform === 'darwin'? 'open': process.platform === 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + url);
}
}, null, super.disposables);当从postMessage接收到"open“命令时,我在Nodejs的帮助下使用url导航到外部应用程序。
谢谢。
https://stackoverflow.com/questions/57557684
复制相似问题