Using easyXDM from chrome and firefox extension.
Closed this issue · 6 comments
In order to use easyXDM from browser addons in chrome and firefox you have to modify the URI regex from this:
var reURI = /^((http.?:)\/\/([^:\/\s]+)(:\d+)*)/; // returns groups for protocol (2), domain (3) and port (4)
to this:
var reURI = /^((http.?:|chrome-extension.?:|resource.?:)\/\/([^:\/\s]+)(:\d+)*)/; // returns groups for protocol (2), domain (3) and port (4)
It may be useful to others to implement these changes.
My general approach to this is that it's not needed, since easyXDM shouldn't be needed if you are targeting extensions (all browsers supporting extensions like this also support postMessage, no?).
If you reuse components inside and outside extensions, then you should encapsulate this behavior so that you can use postMessage directly instead of using easyXDM.
Am I missing something?
Maybe my case is more complex than the usual might be. I communicate with my https backend via an iframe loaded in the background page of the extension instead of making ajax calls. Its not needed for communication within the extension or between extension.
After thinking about it some more, I may not have explained it properly.
I am using easyXDM to communicate from a chrome background page which has a protocol of chrome:// or resource:// to an iframe embeded on that page with a protocol of https://. I do not use it as a substitute for postMessage intra extension. This will not work without changing the regex,
Yes I can change it for just my project and have, but it is a useful way to encapsulate and reuse cross domain communication instead of using ajax when building extensions and if it worked out of the box perhaps it would help others avoid frustration when they try it.
@oyvindkinsey some chrome extensions are automatically added to all web pages in Chrome (e.g. Buffer) If a chrome extension is added easyXDM calls the getLocation function for the extension url, the url match returns null and the next line triggers an error:
var m = url.toLowerCase().match(reURI);
var proto = m[2], domain = m[3], port = m[4] || "";
@lievenjanssen I'm not sure I follow - when would getLocation
be called?
Are you perhaps referring to https://github.com/oyvindkinsey/easyXDM/blob/master/src/stack/PostMessageTransport.js#L55 which operates on incoming messages?
If you want to put up a patch for this case, I'd be happy to accept it, but this project isn't being actively developed at this time.
As a reference this was fixed by d39ebe8