Reputation: 157
Recently we migrated a Chrome extension to Microsoft Edge. For Edge hasn't implemented native messaging, so we want to communicate with native app by websocket via Edge extension background page.
After testing, we found that, in the background page websocket can access external host successfully, but localhost, even though access '127.0.0.1' failed. And we try to access localhost in the web page, it did!
Edge browser info: userAgent: "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" We have checked "Allow localhost loopback (this might put your device at risk)" from about:flags.
Does Edge extension background page support access localhost? If it does, how can we achieve it? If not, could anyone help?
We run the WebSocket server as this example: https://blog.idrsolutions.com/2013/12/websockets-an-introduction/.
The extension can be downloaded from: https://github.com/chhxia/Edge-Extension. The code of edge extension background 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.');
}
});
})();
Upvotes: 1
Views: 2400
Reputation: 280
I ran into this issue while developing my own extension for Edge. I had also checked the "Allow loopback..." setting in about:flags and so I was very confused and frustrated. Having your extension be able reach localhost while developing seems like a reasonable thing to want... right?
It turns out that you can actually access localhost from an Edge extension. You just have to ensure that you add Edge to the loopback exempt list by running CheckNetIsolation LoopbackExempt -a -n="Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
in a Powershell prompt (running in Administrator mode).
To undo that, just run CheckNetIsolation LoopbackExempt -d -n="Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
.
Edge issue regarding Edge extensions and localhost requests: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/13966307/.
Upvotes: 1
Reputation: 1870
To save you a few clicks: IE, Chrome, and Firefox allow it, Edge doesn't. Microsoft says that accessing localhost in Edge extensions is blocked by design:
"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/
... and native messaging for Edge requires an UWP host:
"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://learn.microsoft.com/en-us/microsoft-edge/extensions/guides/native-messaging
Upvotes: 1
Reputation: 157
Access localhost via Microsoft Edge extension background page
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8984919/
Upvotes: 0