Intercept navigator.usb.requestDevice dialog with puppeteer - google-chrome

Is it possible to intercept the dialog created after requesting a usb device via navigator.usb.requestDevice? I tried page.on("dialog", dialog => () from the puppeteer script and document.addEventListener('dialog', () => ...) from the page DOM but none of those callbacks were invoked.

This is not possible today but it is something the Chromium project is aware developers are interested in.
To help prioritize this feature, can you say what your use case for automating this dialog is?

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.

Does Puppeteer run JavaScript on the page?

I'm using Puppeteer for some testing tasks, and am a bit confused:
Does Puppeteer run the JavaScript it encounters? Specifically, our app fires of a bunch of requests (eg. loading Stripe, Facebook, etc), and I want to ensure that code is being run during a Puppeteer request.
Does Puppeteer run JavaScript on the page?
Yes, it does.
Does Puppeteer run the JavaScript it encounters?
Yes, it does.
JavaScript is enabled by default:
While Puppeteer's documentation does not explicitly say that JavaScript is enabled by default, their tutorial documentation does say that it launches Chrome in headless mode by default, and Chrome's headless mode has JavaScript enabled by default, therefore Puppeteer has JavaScript enabled by default).
You can enable or disable it with page.setJavaScriptEnabled(boolean): Promise<void>
https://pptr.dev/#?product=Puppeteer&version=v5.2.1&show=api-pagesetjavascriptenabledenabled
You can check if it is enabled with page.isJavaScriptEnabled(): boolean
Specifically, our app fires of a bunch of requests (eg. loading Stripe, Facebook, etc), and I want to ensure that code is being run during a Puppeteer request.
Use Puppeteer's Code Coverage feature to test that your JavaScript is actually executed, including the lines and functions you want to run.
This (unrelated) blog page has an example.

Launching Chrome Packaged Web App from Website

I have a Chrome Packaged Web app (which is required as it needs to access the Serial Port), and I'd like to be able to launch it from my website (with some data) when I click on a link/button on that website.
It'd be even better if it could detect if the user wasn't running chrome or didn't have the web app installed and could direct them to the right place...
Are there any examples? It seems like an obvious thing to want to do, but I'm really struggling to find anything...
To launch an app you can use url_handlers, a new feature recently landed (should make Chrome 31). You can pass data in the URL.
You can check if an app is already installed, and initiate the install if not, by using the chrome web store's inline install functionality.
Found a better solution to this problem.
In #Vincent Scheib solution you have to redirect the user to /some/url, or open a new window (that can be actually blocked as popup). All of this it's not so good from an UX aproach.
My solution is to create the app and configure the externally_connectable. You have to pass the url domain of the web site that will try to open the app. For example:
"externally_connectable": {
"matches": ["*://developer.chrome.com/*"]
}
Then, in your packaged app in your background script, you can do something like this:
chrome.runtime.onMessageExternal.addListener(function(message) {
if(message.launch){ //This parameter will be passed in sendMessage method below
chrome.app.window.create("main.html");
}
});
Finally, to trigger the app open you have to send a message using your packaged app id. For example:
chrome.runtime.sendMessage("mdoedhlejmepngilmgoenbhmipoclckb", { launch: true });

Windows .net Google.Apis hangs on the call to InsertMediaUpload.Upload -- there is no timeout

I am using GoogleApis to upload documents to Google Drive using the InsertMediaUpload class from the FilesResource namespace and the Upload method. It is working well for me with the following exception:
After calling InsertMediaUpload, a browser window appears asking the user to log into their Google (usually Gmail) account. If the user simply closes the browser window instead of clicking on "Accept" or "Cancel" then the current process appears to be hung. I suppose there should a timeout of a minute or two so that if the user opts to not log in the current windows application will not simply hang and stop working indefinitely.
There is no need for sample code here. What should happen when the user simply closes the browser window instead of clicking cancel if they are no longer interested in uploading a document? Crashing (or hanging) the current process should not be a possibility, but that is what occurs. One would hope closing the browser window would have the same effect as clicking the cancel button -- just another way of opting out of an upload to Google Drive, right?
Thanks in advance for any help with this.
You're not supposed to get authentication message from InsertMediaUpload class. You should handle authentication by yourself. Authentication browser window you get is for your development convenience, not for production code. Please take a look at .net quickstart. In this quickstart, you'll see GetAuthorization method which handles authentication. Modify this method on your needs and you'll get what you want.

Is there a chrome.* API for my google-chrome extension to find out on which pages it is barred from scripting?

I am developing a Google Chrome Extension which has a content-script, a background page, a settings page and a popup page. I learnt the hard way that though the content-script loads on the Chrome Web Store, the onMessage listeners are never invoked.
I can consistently break at the point where I am executing chrome.extension.onMessage.addListener, but the listener is never invoked. Therefore when I execute chrome.tabs.sendMessage from the background or popup pages, the responseCallback function never gets invoked - i.e. the logic inside the responseCallback never executes. Chrome doesn't even treat this like an error scenario - it accepts the request, but goes silent thereafter.
If it had treated this as an error case (as with attempting to message a chrome:// page) then the responseCallback would've been invoked with a undefined response argument - and that would cause my error-handling code to execute... but no such luck.
I could add special behavior in my code for http(s)://chrome.google.com/webstore/*, but was hoping that there was a more official way of finding pages that were barred from execution. Would appreciate any pointers there.
I found a great post here, but it doesn't have a programmatic API, hence this question.
I am already aware that the Chrome webstore and chrome:// URLs are special cases. However, per my investigation, the behavior on chrome:// URLs is different than on the Chrome Web Store. On chrome:// URLs, my content-script appears to not load at all and the sendMessage's responseCallback function gets invoked with a null/undefined response object. That is the API's official way of signalling an error and it works fine for me.
My issue is with being able to programmatically detect the errors like those experienced with the Chrome Web Store without having to hardcode the URLs (the URLs could change with time).
Thanks in advance for your responses.
How about adding a listener for chrome.webNavigation.onCompleted in the background script? When a new page is loaded, send a special "ping" message to the tab, and use chrome.extension.onMessage to receive and respond to the ping in your content script. If the content script doesn't respond then you know that it isn't running on that page.
You could try running chrome.tabs.executeScript to see if you're allowed scripting in that domain.
chrome.tabs.executeScript(tabId, {code: ""}, function() {
if(chrome.runtime.lastError) {
// Something went wrong, perhaps no rights
} else {
// Scripting allowed
}
});