Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Add quick documentation about chrome extensions #417

Merged
merged 5 commits into from
Sep 18, 2018
Merged

Add quick documentation about chrome extensions #417

merged 5 commits into from
Sep 18, 2018

Conversation

shadowcodex
Copy link
Contributor

Just added a quick documentation to the docs/tutorial folder for using extensions directly in muon.

It introduces how to get access to chrome.ipcRenderer, and the important extensions. api hooks.

It also shows a sample of how to load an extension via interaction from the renderer thread.

A basic example of using extensions
removed console logs
Copy link

@icaroharry icaroharry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great contribution. That is an awesome feature of Muon but I have no idea how to use it.


In the case of the following extension.js file, shadowcodex is using the `chrome.ipcRenderer` to call the functions via a lifecycle in the renderer thread. For example a simple vuejs app is shown:

> Note: In order to use `chrome.ipcRenderer` your render file must be loaded with `chrome://brave/` and this one in particular is loaded like `mainWindow.loadURL('chrome://brave/' + path.join(__dirname, '../client/index.html'))`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to use chrome://brave it won't load the extension. Otherwise, when I load directly a web page it works. Do you know what is the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extensions should be loaded via the chrome-extension:// url which can be done in a new window or in a webview which requires chrome://brave/


> Note: In order to use `chrome.ipcRenderer` your render file must be loaded with `chrome://brave/` and this one in particular is loaded like `mainWindow.loadURL('chrome://brave/' + path.join(__dirname, '../client/index.html'))`

### Vuejs renderer lifecycle hook

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide this live example, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not public yet, but soon


ipcMain.on('halo', (event, arg) => {
if(!hapiRegistered){
session.defaultSession.extensions.load(path.join(__dirname,`extensions/hapi`), {}, 'unpacked');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question. Does that trigger the extension popup? Do you know how to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it doesn't you have to call it via chrome-extension://

@shadowcodex
Copy link
Contributor Author

Added part to show how to call up an extension resource.

@kewde
Copy link

kewde commented Jan 24, 2018

@shadowcodex

This is good work, docs are by far the most missing part of Muon imo (from app dev perspective).

@kewde
Copy link

kewde commented Feb 7, 2018

So after a bit of toying with the following fix:

mainWindow.loadURL('chrome://brave/' + __dirname + '/index.html');

It also exposes the remote API which is basically a gold mine right?

$ const proc = chrome.remote.require('child_process')
undefined
$ const ls = proc.exec('ls')
undefined
$ ls.pid
9233

^executing processes from the Chrome debugging tool..

@shadowcodex
Copy link
Contributor Author

shadowcodex commented Feb 7, 2018 via email

@kewde
Copy link

kewde commented Feb 7, 2018

@shadowcodex is their an issue that's keeping track of the .remote() removal?

@kewde
Copy link

kewde commented Feb 8, 2018

'chrome://brave/' + __dirname + '/index.html'

This doesn't work very well across operating systems.

On linux it will result in the following:

'chrome://brave//somepath/index.html'

The second double slashes (after keyword 'brave'), they are misinterpreted as the double slash for a protocol.
It will mess with router in frameworks like Angular and React. It will produce and error along the likes of:

Failed to execute 'replaceState' on 'History': A history state object with URL  cannot be created in a document with origin '' and URL '

Basically, it breaks the whole application.

Removing the / creates an issue on Windows, so we should make use of the path.join() or url.format() functions.

So if I can suggest a minor change to the way you load the URL in the BrowserWindow:

    mainWindow.loadURL(url.format({
      protocol: 'chrome:',
      pathname: path.join('brave', __dirname, 'index.html'),
      slashes: true
    }));

It becomes

'chrome://brave/somepath/index.html'

in a consistent way.

@shadowcodex
Copy link
Contributor Author

@kewde yeah that's a good point.

You could also just do:

mainWinow.loadURL(path.join('chrome://brave', __dirname, 'index.html')

@shadowcodex
Copy link
Contributor Author

I'm hoping when I get time I'll create a full demo on extension lifecycle management and leave it up on my github. I have some more refined ways of handling extensions and could strip it down to a bare minimum for people to use.

@shadowcodex
Copy link
Contributor Author

@bridiver what do I need to do to get this PR moved forward?

Copy link
Member

@bsclifton bsclifton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

@bsclifton bsclifton merged commit 43661f9 into brave:master Sep 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants