Register custom file protocols in Chrome/Firefox extension? - google-chrome-extension

I want to register a custom file protocol in Chrome or Firefox, much like the way Electron does (please see their API for doing this: https://www.electronjs.org/docs/api/protocol). I am wondering if there is a way to implement this as a browser extension, or I have to modify the source code of the browsers.
I expect that the API would look like this (just a pseudocode to help explain what I mean):
registerHandler('myprotocol://', req => {
response('<body>You requested: ' + req.url);
});
Clarification: navigator.registerProtocolHandler is NOT what I need. What it does is to register a protocol that, when clicked, opens an external application to deal with that. But what I want is a protocol handler scheme that works in a request--response way, e.g. can be used in JS/CSS/HTML queries and responds with a content that can be rendered within the browser.

I would answer my own question because I found exactly what I need here: https://github.com/mozilla/libdweb
This is an experimental feature of Firefox Nightly that allows one to register a custom protocol and serve all requests to that protocol using firefox addon. This is not a WebExtension standard nor does it work on browser other than Nightly, but I'm glad to hear that someone is doing this.

Related

Is there a way to access and control "Remote Devices" section of Chrome Devtools programmatically?

I understand that the Chrome Devtools protocol exposes the Google Chrome DevTools via APIs to be controlled programmatically, and puppeteer provides a Node.js implementation to do so. However, if I am not wrong, puppeteer doesn't allow us to do everything that the DevTools protocol can do.
If I want to remotely debug my android chrome browser, like I can do manually, by going to "Remote Devices" section of my DevTools, is there a way to do so bypassing puppeteer? Is there a package that lets us access more core functionalities of the DevTools protocol?
Yes, you can do that. puppeteer is just a wrapper around the Chrome DevTools Protocol. To understand how the protocol works, you might want to have a look at Protocol Fundamentals, where the basics are explained.
To communicate on this lower level, you can also use puppeteer. Check out the CDPSession documentation, which exposes the "low level" part of the API.
Minimal example:
const client = await page.target().createCDPSession(); // creates a "low level" session
await client.send('COMMAND'); // sends the command to the browser
client.on('MESSAGE', () => { /* message from the browser received */ });
Alternatives
If you don't want to use puppeteer, there are multiple other libraries. The most used one for Node.js is chrome-remote-interface, which is more low-level than puppeteer. According to the docs:
chrome-remote-interface [...] is a general purpose 1:1 Node.js binding for the Chrome Debugging Protocol. Use it if you need all the power of the raw protocol, e.g., to implement your own high-level API.
There are also multiple other libraries (also for other languages). You can find a full list here.

How can I communicate in a secure way with my firefox extension

I am working on a webpage that depends on browser extensions to perform certain tasks.
From the webpage I can communicate with the chrome browser using 'externally_connectable' and:
chrome.runtime.sendMessage(string extensionId, any message, object options, function responseCallback)
The good thing here is that from the point of view of my website I am sure I am communicating with my extension and not with a 'reverse engineered' version of my extension.
In firefox extensions however there is no support for externally_connectable and I have to use events or window.postmessage:
const event = new CustomEvent('msg-for-content-script');
document.querySelector('body').dispatchEvent(event);
This works ok, but the problem is that when somebody manages to reverse engineer my extension I am not able to check whether the extension I am communicating with is really my extension.
Is there anybody who can give advice on how to make sure that I am communicating with the right extension?

Titanium: How to set referer when loading WebView

I currently use this
mywebview.setUrl(the_url);
to load a URL.
Can I, instead, load it in such as way as to include a referer in the http header?
EDIT: The reason for this is that the linked-to website should be able to see where the traffic is coming from even if the URL is loaded into a different webview than the one that contained the link. (I use multiple WebViews to create a tab UI.)
Edit:
The xcode equivalent seems to be this, although I'm not sure if this is also loading it into a new webview: Specifying HTTP referer in embedded UIWebView
It appears defining custom headers for WebViews is not available yet. You can watch ticket TIMOB-17467 to view updates.
It appears you will need a native module.
A quick check for iOS..
Safari Dialog module doesn't support headers (class limitation).
WKWebview module doesn't support headers (appears to be possible).
Perhaps you could extend these.
Android appears to be easier to implement, (but still not available in Titanium SDK), via extraHeaders: Read more: https://stackoverflow.com/a/5342527
I've looked at the Android Webview header (php getallheaders()) and there is x-requested-with: com.app.id
So in you page you could check for that value and know at least that it was visited by the app with the bundle identifier. Otherwise you could attach a get parameter to the url ?mobile and count this.
I'm trying to add a patch to the Android SDK and add a setHeader() method. The loadUrl() call is at:
https://github.com/appcelerator/titanium_mobile/blob/bc85170157d3bebc5de1d61a9fe6e34bce84a8c9/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java#L462
If you change it according to #tzmartin
extraHeaders.put("Referer", "http://www.referer.tld/login.html");
getWebView().loadUrl(finalUrl, extraHeaders);
then it already works but its hard coded.

How to catch a flash stream url from browser plugin

My question has similar point like this one.
I’m wondering how I can catch a media URL which SWF loads from browser add-on. Let’s say YouTube flash player starts playing or loading some video (let it be via http) and I want to know that url. Just like browser plugins from “RealDownloader” and “Moyea YouTube FLV Downloader” does. I’m newbie with plugin development and flash and I want to know what technologies it may be. XPCOM, NPAPI, ActiveX, or simple API hooking. Any ideas how this may be accomplished?
NPAPI plugins typically ask the browser to load data for them, they don't do it themselves. This means that a browser extension can intercept these requests. This can be done for example by implementing a content policy. Requests initiated by a plugin will cause a shouldLoad call with type OBJECT_SUBREQUEST.
The simpler option is using HTTP observers - but this way you won't recognize requests initiated by Flash, they will look just like any other request processed by the browser.
Firebug does that, and it's open source. Why not study it a little?
https://github.com/firebug/
It's easy if you only want to get the url from a single swf in a single website. for example if all you need are urls from that swf,you can keep only one instance of your browser open and use a tool to intercept its http requests.

Browser plugin which can register its own protocol

I need to implement a browser plugin which can register its own protocol (like someprotocol://someurl) and be able to handle calls to this protocol (like user clicking on 'someprotocol' link calls function inside my plugin). As far as I understand, Skype does something similar, except I need to handle links within page context and not in a separate app. Any advice on how this can be done? Can this be done without installing my own plugin, with the help of flash/java?
Things are going to be slightly more complicated than you think.
You're going to have to create an entire application, not just a browser plugin (that plugin can be part of your application). The reason I consider it to be a complete application is that you're going to need to modify registry settings on the client machine to register your custom URL handler.
Here's an MSDN article describing exactly what you have to do to register the custom URL handler on a Windows client:
Registering an Application to a URL Protocol

Resources