Chrome Application - Interract with a tab [duplicate] - javascript

I need to combine functionality available only in a Chrome packaged app (access to syncFileSystem) and functionality available only in a Chrome extension (injecting a script into a 3rd party website).
It seems that neither a packaged app nor an extension can achieve both these things, so I'm now considering trying to achieve what I'm after with a separate packaged app and extension communicating.
I see that Chrome's documentation explains how two extensions can communicate via chrome.runtime.onMessageExternal.addListener and chrome.runtime.sendMessage, but nothing about packaged apps and extensions communicating.
Does anyone know if this is allowed? Is there any documentation, or a working example out there?

Yes, that is possible. The code sample in the documentation you linked works for any combination of app and extension.
The extension documentation for chrome.runtime.sendMessage says:
Sends a single message to onMessage event listeners within the extension (or another extension/app).
Messaging works the same in both extensions and apps, and they seem to be fully compatible; simply use the ID for the destination extension or app. If you look at the docs for the app version of chrome.runtime.sendMessage, you'll see that it is identical to the extension version.

Related

'chrome.sockets.tcp' in a PWA?

I am creating a PWA using Nuxt and I want to use the Chrome TCP API to communicate with TCP devices directly. The API is detailed here https://developer.chrome.com/apps/sockets_tcp
I have confirmed that clients are downloading it when they access the page.
manifest.json
{
"name":"pwa-demo",
"short_name":"pwa-demo",
"description":"Test App",
"publicPath":"//_nuxt/",
"icons":[
{
"src":"/_nuxt/icons/icon_64.fj_mLYH_Zr_.png",
"sizes":"64x64",
"type":"image/png"
}
],
"start_url":"/?standalone=true",
"display":"standalone",
"background_color":"#ffffff",
"theme_color":false,
"lang":"en",
"sockets":{
"tcp":{
"connect":[
"*:8023"
]
}
}
}
However, when the PWA is run in the browser or after installation (both on desktop and Android) in all cases I get the following error trying to call a chrome.sockets.tcp API: TypeError: Cannot read property 'tcp' of undefined
The Chrome "apps" API (https://developer.chrome.com/apps) does seem to suggest that it is meant to be done using Cordova but I figured that that suggestion was old (there's other content on the page from 2014) and that once a PWA is installed there shouldn't be a functional difference between an installed Cordova app and a new installed PWA. They're both wrappers around a Chrome WebView (except of course Cordova can expose more native APIs).
I am guessing that the chrome.sockets.tcp API is actually exposed through the Cordova wrapper and not Chrome itself, but I have not found anything that confirms this. Google has publicly expressed that they want PWAs to replace Chrome Apps so one would hope that the API was moved into Chrome itself. There seems to be a lot of people confused about whether it can be used by Chrome extensions too.
Is my manifest wrong? Or can someone confirm that this API is not usable by PWAs?
Chrome Apps are specialized apps that are distributed through the Chrome Web Store. Think browser extensions but more app like. Websites do not have access to the privileged APIs available to Chrome Apps.
Also note that Chrome Apps are being retired for everything other than Chrome OS.

Google Oauth2 in Chrome Extension in background.js

I am building a Chrome Extension for my personal use (i.e. it will not be packaged and distributed) to dump data from a website into Google Sheets. I'd like to click on the Extension and have it process the data to my sheet. I believe this means I need to do the Oauth flow in background.js.
I did the initial authorization flow by customizing this Google Sheets demo, can't figure out how to make it work in my Extension.
I've tried a number of approaches, including using the chrome.identity API, and gapi.client.init(), and following the Chrome App sample. No dice.
Some of my questions...thanks in advance:
To what extent do Chrome Extensions mirror Chrome Apps? I understand that Chrome Apps are being deprecated, so wondering if the docs are inconsistent.
Is it possible to do this without packaging and uploading my app? The Oauth credentials page in Console asks for a Web Store URL
Is it acceptable to store a copy of Google's api.js in my extension, or must I load it from https://apis.google.com/js/client.js? If so,
For the Chrome App Sample, Where do I get the key included in manifest.json? I've seen instructions like "Copy key in the installed manifest.json to your source manifest" but I don't understand.
Is anyone aware of a complete, self-contained Chrome Extension sample?
To what extent do Chrome Extensions mirror Chrome Apps? I understand that Chrome Apps are being deprecated, so wondering if the docs are inconsistent.
Extensions and Apps are similar in many ways, however for your situation the main hurdle to overcome is the two handle Google Authentication differently. Extensions have permission limitations, where javascript can't run in certain places. Therefore, Chrome Extensions use chrome.identity in background.js to establish a secure connection and token. The general process to implement it is as follows:
Make a Chrome Extension, zip it, upload to your Google Dev account & get extensionID#
In Google API Console, register an OAuth ClientID# using the extensionID#
Update your Chrome Extension manifest to include an 'oauth2' section with the OAuth ClientID# as well as the scopes you allow, and include 'identity' under "permissions:"
Enable the API of your choosing in the Google API Console and generate a key. Include this key in your background.js file so you can use the API.
Is it possible to do this without packaging and uploading my app? The Oauth credentials page in Console asks for a Web Store URL
No, mainly because you need both the chrome extension and the API to be aware of each other and be 'linked' in a sense so they can be secure and work properly. You can still have a private app however, as you only need to package (.zip it) and upload it into your Developer Dashboard, and you can leave it out of the public Chrome Store by simply not publishing. It can forever linger in 'Draft' stage for your personal use.
Is it acceptable to store a copy of Google's api.js in my extension, or must I load it from https://apis.google.com/js/client.js? If so,
For the Chrome App Sample, Where do I get the key included in manifest.json? I've seen instructions like "Copy key in the installed manifest.json to your source manifest" but I don't understand.
You don't need to store a copy within your extension, you can add the following to your manifest.json:
"content_security_policy": "script-src 'self' https://apis.google.com/; object-src 'self'"
and then this at the bottom of your popup.html:
<script src="https://apis.google.com/js/client.js?onload=onGAPILoad"></script>
It's a rather confusing process without a guide; here is the one that finally made sense of it all for me. This official example from Google is a good one as well.
Is anyone aware of a complete, self-contained Chrome Extension sample?
'self-contained' is a bit tricky here, as the manifest needs to reference keys specific to the OAuth ClientID and API that YOU are utilizing, however this (download link) along with the two links above should be enough to get you to a working extension.

Possible to open link in Chrome Application from javascript?

From the command line, I can open a url as a chrome application by running e.g.
chromium --app=https://www.stackoverflow.com
Is it possible to do open a url in this mode from javascript in an existing page? To be clear, the url should ideally be opened in a new window, which has the properties implied by the --app flag (e.g. no address bar), whether or not the current page is running in that mode.
My reason for asking is that I'd like to integrate this into vimium.
There is no way to do what you need directly from Javascript. I think you'll need to create an Chrome extension to do that.
You still can find an extension called "Open with external application" which do that, but its use NPAPI and NPAPI is not supported anymore by latest Chrome version. The source is hosted on BitBucket.
The new way to do the same thing is using the native messaging API. In this case the external application would have to register a native messaging host in order to exchange messages with your application. You can see more at http://developer.chrome.com/extensions/messaging.html#native-messaging

upload chrome extension to store including native application

I have my first brand new google chrome extension, which I'd like to upload to the chrome web store. The thing is that my extension is using native messaging via stdio to a c++ exe. The extension also required a registry key to be installed.
Is it possible to upload the required files to the chrome web store (including exe and dll dependency)? Is there a way to automate the installation of the registry key (required to send messages from chrome extension js to exe), and register dll?
The idea behind chrome-extensions (and native messaging) is to limit the giant security hole that is NPAPI: to create a divide between the browser and the desktop that runs it. For this reason, you'll need to perform two separate installations: one for the extension and another for the native code.
To the best of my knowledge, you can't bundle the binary with the extension. But you might be able to (partially) do it the other way around: have the native code also install the extension:
There is a way to partially automate the process of installing the extension: you can pre-load the extension via the registry. This only applies to extensions on the chrome web-store (CWS). I believe this requires to reboot chrome. And of course the user is notified that an extension was installed. see here - https://developer.chrome.com/extensions/external_extensions
There's also something called "inline installation" - not quite "automatic", but this should make it easier for your users to install the extension from "within" your site (as opposed to the CWS). The extension still needs to be hosted on the CWS, but the user needs not navigate there.
see here - https://developer.chrome.com/webstore/inline_installation (and especially the line section - about verified site - which requires that you register your site with Google - and thus may render this technique useless to you)

Specifying Native Messaging Host for Chrome extension

I am using native messaging API in Chrome extension, and I want to ship the native application within my extension.
In Windows, I add a registry key under HKLM\SOFTWARE\Google\Chrome\NativeMessagingHosts. Since my application is inside the extension folder after installed, I reference the full path by "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Extensions\...\....json". But in this way, my extension complains "Specified native messaging host not found.".
If I expand the %LOCALAPPDATA% and write the path as "C:\Users\...\AppData\Local\Google\Chrome\User Data\Default\Extensions\...\....json", then my extension can successfully communicate with the host.
I wonder if this is an intended behavior? Thank you for your help.
There is no expansion of environment variables in the current Chromium code. It sounds like a reasonable feature request, though there might be security reasons not to do it. If you file a bug, we'll have a look and see whether it's feasible.

Categories