Logging network tab automatically without opening it in Google Chrome - google-chrome

I need to log the information shown in network tab to a file. I can do it by firing network tab first and then exporting it to a file. But is it possible to run the network tab and exporting it to a file in the background automatically whenever the Chrome is opened?
Is it possible to do?

It depends on your requirement. If your extension involves DevTools, and therefore it is open, you can use the chrome.devtools.network.getHAR() method to get the network traffic. You don't need to navigate to the Network tab.
However, if you want to access the network data without DevTools being opened, this API will not work, as it's only exposed to the DevTools instance. There are a couple of possible options.
Option 1
You could use the chrome.webRequest API to intercept each request/response and append whatever data you want/can to an object. You could then use the chrome.downloads API to download the data. In your case, you could use a data URI.
var url = 'data:application/json;base64,' + btoa(data);
chrome.downloads.download({url: url, filename: 'notQuiteAHAR'json'});
I haven't tested this in practice, and I'm not too sure if you can determine when all requests are done before calling the download.
Option 2
Use the more low level chrome.debugger API, as per the comment by #wOxxOm. The debugging protocol only allows one instance of the debugger at a time, so this will only ever work if you don't have DevTools running. The API exposes a lot more than the chrome.webRequest API, but requires a bit of work to get all the data you need.
There's a repository called chrome-har-capturer, which uses the debugging protocol. Of particular interest is har.js, which uses the events found in the debugger API to manually construct the HAR. The purpose of the library is for remote debugging purposes, but I believe you can use the debugger API in an extension, and so you could probably use aspects of this repository.

As suggested by Gideon Pyzer, HAR Recorder uses chrome debug protocol to record HAR and generate a har file (without opening devtools). If you want a variation, you can fork and make changes on it.

Related

How can I use a Chrome browser extension to monitor and parse the output in the devtools console?

I am building a chrome extension that is supposed to aid in the debugging of software that runs on top of a website. This software can have a debug mode enabled that will cause a lot of output to the console using console.log.
I want to use my chrome extension to parse the console messages and show the important events in the UI for quicker debugging. However, I am not seeing a way to simply do this with the API. Is there something I am missing? Should I override the console.log function? How would I go about doing that?
There are two methods.
Override console.log, console.warn, and so on, in page context (this is important!). There are lots of examples (here's a random one). In your case it'll be even simpler as you'll just call the original method and transfer the arguments via CustomEvent to your content script (example), which will accumulate them.
Use chrome.debugger API with Console.messageAdded or Runtime.consoleAPICalled events. This will show a message bar in the entire browser about debugger being active unless you hide it globally by running chrome with --silent-debugger-extension-api command line, but that's somewhat dangerous if you accidentally install a malicious extension that uses chrome.debugger API.

Saving HTTP requests from Chrome's DevTools

I'm currently working on my Bachelor's Degree paper and I want to create an environment for software testing.
I found that the requests shown in the Network tab of Chrome's Developer tools would be very helpful for what I'm trying to develop.
So I was wondering...Is there a way of saving these kind of requests in a file on my local or to access them through Java code?
Is your goal to save the request or response data?
If you're interested in saving the responses, you could either save the entire page, or save individual requests using the network requests tab in the Chrome Inspector.

Possible to save a filter in the Chrome console?

I have an adblocker installed, which means that whenever I open up Chrome's console I'm greeted by dozens of net::ERR_BLOCKED_BY_CLIENT entries.
I can filter them out with something like ^((?!net::ERR_BLOCKED_BY_CLIENT).)+$, but filters are not persisted, so I have to do this constantly.
Is there a more permanent solution available, such as saving console filters or maybe a config/preference that can be edited?
You can filter them by checking "Hide network messages". You will be able to see responses in the Network tab still. I'm not aware of any built in features to save specifc filters unfortunately.
In many cases, you can hide/modify logs and errors by re-defining the Console API functions using Snippets. However, in this case, the error is not logged from the page itself, so it won't help.

Automatize HAR files generation from Google Chrome

Basically what I need is a way to automatize the result of the following operations:
open a new tab;
open the Network tab in the developer tools;
load an URL;
select "Save All as HAR".
Often, proposed solutions involves the use of PhantomJS, browsermob-proxy, or pcap2har; those won't fit my case since I need to work with SPDY traffic.
I tried to dive into the Google Chrome Extensions API and indeed I managed to automatize some tasks, but still no luck for what concerns the HAR files generation. Now this method is particularly promising but I still can't figure out how would I use it.
In other words, I need something like this experiment from the Google guys. Note the following:
We used Chrome's remote debugging interface with a custom client that starts up the browser on the phone, clears its cache and other state, initiates a web page load, and receives the Chrome developer tools messages to determine the page load times and other performance metrics.
Any ideas?
Solution
For the curious, I ended up with a Node.js module that automates such kind of tests: chrome-har-capturer. This also gave me the opportunity to dig deeper into the Remote Debugging Protocol and to write a lower-level Node.js interface for general-purpose Chrome automation: chrome-remote-interface.
The short answer is, there is no way to get at the data you are after directly. The getHAR method is only applicable to extensions meant to extend DevTools itself. The good news is, you can construct the HAR file yourself without too much trouble - this is exactly what phantom.js does.
Start Chrome with remote debugging
Connect to Chrome on the debugging port with a websocket connection
Enable "Network" debugging, you can also clear cache, etc - see Network API.
Tell the browser to navigate to the page you want to capture, and Chrome will stream all the request meta-data back to you.
Massage the network data into HAR format, ala phantom.js
...
Profit.
For a head start, I have a post that with sample Ruby code that should you get started with steps 1-4: http://www.igvita.com/2012/04/09/driving-google-chrome-via-websocket-api/
By now there's a browser plugin to do that: https://github.com/devtools-html/har-export-trigger
It uses the WebExtensions DevTools API and I got it to work with both Firefox and Chrome.
See my code for Chrome here: https://github.com/theri/web-measurement-tools/blob/master/load/load_url_using_chrome.py#L175
Automatically installing the plugin in Chrome is a bit more complicated than in Firefox, but feasible - I extracted the plugin archive locally and then link to it in chrome_prefs.json (see same repository).
Not sure if it helps, HAR Recorder uses chrome debug protocol to record HAR and generate a har file (without opening devtools). If you want a variation, you can fork and make changes on it.

How can I pass messages between two instances of *the same* Google Chrome extension?

While Google Chrome's messaging API allows communication between two different extensions using their 'Extension ID', I'm yet to see the issue of communication between two instances of the same extension be addressed. Is this possible using the current Google Chrome API? Is there an 'Instance ID' to uniquely identify each instance of my extension?
If not, I think I will try using my own server be the -middle man- and let all the instances of my extension talk to each other using my -middle man-. In that case, does the Chrome API expose the Instance ID for extensions? If not, any advice on coming up with my own Instance ID scheme?
you can certainly do that, I have done the same thing for my Reload All Tabs extension.
https://chrome.google.com/extensions/detail/bfenodnbilondijnaionekngdhadmegk
Basically, you would need an "extension ID" what I did, I basically uploaded the main extension to the gallery, which gave me the unique ID.
With that ID, you can send requests to that extension via:
http://code.google.com/chrome/extensions/extension.html#method-sendRequest
And listen through external extension requests through:
http://code.google.com/chrome/extensions/extension.html#event-onRequestExternal
You can take a look at the source code for Reload All Tabs, to see how it is done:
https://github.com/mohamedmansour/reload-all-tabs-extension