通过 Microsoft Edge 扩展后台页面访问 localhost
Access localhost via Microsoft Edge extension background page
最近我们将 Chrome 扩展迁移到 Microsoft Edge。由于Edge没有实现原生消息,所以我们想通过Edge扩展后台页面通过websocket与原生应用进行通信。
经过测试,我们发现,在后台页面websocket可以成功访问外部主机,但是本地主机,即使访问'127.0.0.1'失败。
然后我们尝试在网页中访问localhost,成功了!
Edge 浏览器信息:
用户代理:"Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
我们从 about:flags 检查了 "Allow localhost loopback (this might put your device at risk)"。
Edge扩展后台页面是否支持访问localhost?如果是这样,我们如何才能实现它?如果没有,有人可以帮忙吗?
我们以运行 WebSocket服务器为例:https://blog.idrsolutions.com/2013/12/websockets-an-introduction/.
扩展可以从以下网址下载:https://github.com/chhxia/Edge-Extension。
边缘扩展后台js代码:
var ws;
function openSocket(){
var socket, path;
// path = 'wss://echo.websocket.org'; // successfully access this path.
path = 'ws://localhost:8080/EchoChamber/echo';
console.log( '===> Tested path :: ', path );
try {
ws = new WebSocket( path );
}
catch ( e ) {
console.error( '===> WebSocket creation error :: ', e );
}
ws.onopen = function(){
alert('open...');
ws.send('text');
}
ws.onmessage = function(e){
alert("receive: " + e.data);
}
ws.onclose = function(e){
ws = undefined;
alert('close...' + e);
}
}
(function(){
openSocket();
browser.browserAction.onClicked.addListener(function(tab) {
if(ws === undefined){
openSocket();
}else if(ws && ws.readyState === WebSocket.OPEN){
alert('send');
ws.send('text');
}else{
alert('websocket is closed.');
}
});
})();
通过Microsoft Edge扩展后台页面访问localhost
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8984919/
为了节省您的点击次数:IE、Chrome 和 Firefox 允许,Edge 不允许。 Microsoft 表示在 Edge 扩展中访问本地主机被设计阻止:
"We are working on Native Messaging for the next release and using
native messaging is the right way to solve this scenario. Localhost
access is not enabled from extension background page is by design."
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8984919/
... Edge 的本地消息传递需要 UWP 主机:
"At a high level, Microsoft Edge extensions use the same APIs for
native messaging as Chrome and Firefox extensions. However, the native
messaging host will need to be implemented using the Universal Windows
Platform."
https://docs.microsoft.com/en-us/microsoft-edge/extensions/guides/native-messaging
我在为 Edge 开发自己的扩展时 运行 遇到了这个问题。我还检查了 about:flags 中的 "Allow loopback..." 设置,所以我感到非常困惑和沮丧。让你的扩展能够在开发时到达本地主机似乎是一件合理的事情......对吧?
事实证明,您实际上可以从 Edge 扩展程序访问本地主机。您只需确保在 Powershell 提示符中通过 运行ning CheckNetIsolation LoopbackExempt -a -n="Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
(在管理员模式下 运行ning)将 Edge 添加到环回豁免列表中。
要撤消它,只需 运行 CheckNetIsolation LoopbackExempt -d -n="Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
。
关于边缘扩展和本地主机请求的边缘问题:https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/13966307/。
最近我们将 Chrome 扩展迁移到 Microsoft Edge。由于Edge没有实现原生消息,所以我们想通过Edge扩展后台页面通过websocket与原生应用进行通信。
经过测试,我们发现,在后台页面websocket可以成功访问外部主机,但是本地主机,即使访问'127.0.0.1'失败。 然后我们尝试在网页中访问localhost,成功了!
Edge 浏览器信息: 用户代理:"Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393" 我们从 about:flags 检查了 "Allow localhost loopback (this might put your device at risk)"。
Edge扩展后台页面是否支持访问localhost?如果是这样,我们如何才能实现它?如果没有,有人可以帮忙吗?
我们以运行 WebSocket服务器为例:https://blog.idrsolutions.com/2013/12/websockets-an-introduction/.
扩展可以从以下网址下载:https://github.com/chhxia/Edge-Extension。 边缘扩展后台js代码:
var ws;
function openSocket(){
var socket, path;
// path = 'wss://echo.websocket.org'; // successfully access this path.
path = 'ws://localhost:8080/EchoChamber/echo';
console.log( '===> Tested path :: ', path );
try {
ws = new WebSocket( path );
}
catch ( e ) {
console.error( '===> WebSocket creation error :: ', e );
}
ws.onopen = function(){
alert('open...');
ws.send('text');
}
ws.onmessage = function(e){
alert("receive: " + e.data);
}
ws.onclose = function(e){
ws = undefined;
alert('close...' + e);
}
}
(function(){
openSocket();
browser.browserAction.onClicked.addListener(function(tab) {
if(ws === undefined){
openSocket();
}else if(ws && ws.readyState === WebSocket.OPEN){
alert('send');
ws.send('text');
}else{
alert('websocket is closed.');
}
});
})();
通过Microsoft Edge扩展后台页面访问localhost
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8984919/
为了节省您的点击次数:IE、Chrome 和 Firefox 允许,Edge 不允许。 Microsoft 表示在 Edge 扩展中访问本地主机被设计阻止:
"We are working on Native Messaging for the next release and using native messaging is the right way to solve this scenario. Localhost access is not enabled from extension background page is by design."
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8984919/
... Edge 的本地消息传递需要 UWP 主机:
"At a high level, Microsoft Edge extensions use the same APIs for native messaging as Chrome and Firefox extensions. However, the native messaging host will need to be implemented using the Universal Windows Platform."
https://docs.microsoft.com/en-us/microsoft-edge/extensions/guides/native-messaging
我在为 Edge 开发自己的扩展时 运行 遇到了这个问题。我还检查了 about:flags 中的 "Allow loopback..." 设置,所以我感到非常困惑和沮丧。让你的扩展能够在开发时到达本地主机似乎是一件合理的事情......对吧?
事实证明,您实际上可以从 Edge 扩展程序访问本地主机。您只需确保在 Powershell 提示符中通过 运行ning CheckNetIsolation LoopbackExempt -a -n="Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
(在管理员模式下 运行ning)将 Edge 添加到环回豁免列表中。
要撤消它,只需 运行 CheckNetIsolation LoopbackExempt -d -n="Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
。
关于边缘扩展和本地主机请求的边缘问题:https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/13966307/。