Git Product home page Git Product logo

electron-as-browser's Introduction

electron-as-browser

versiondownloadslicense

A node module to help build browser like windows in electron.

./screenshot.png

Features

Install

npm i electron-as-browser

Usage

First, create BrowserLikeWindow in Main process

import BrowserLikeWindow from 'electron-as-browser';

let browser;

browser = new BrowserLikeWindow({
  controlPanel: 'renderer/you-control-interface.html',
  startPage: 'https://page-to-load-once-open',
  blankTitle: 'New tab',
  debug: true // will open controlPanel's devtools
});

// Trigger on new tab created
browser.on('new-tab', ({ webContents }) => {
  // Customize webContents if your like
});

browser.on('closed', () => {
  // Make it garbage collected
  browser = null;
});

Second, style your own browser control interface(renderer process).

To make the control interface works, there are two steps:

  • Setup ipc channels to receive tabs' informations
  • Send actions to control the behaviours

For react users, there is a custom hook useConnect to help you setup ipc channels.

import useConnect from 'electron-as-browser/useConnect';

const ControlPanel = () => {
  const { tabs, tabIDs, activeID } = useConnect();
  return (
    <div>Use tabs' information to render your control panel</div>
  );
}

For non-react users, you have to setup ipc channels yourself, there are three steps:

  • ipcRenderer.send('control-ready') on dom ready
  • ipcRenderer.on('tabs-update', (e, tabs) => { // tabs updated })
  • ipcRenderer.on('active-update', (e, activeID) => { // Active tab's id updated })

Don't forget to removeListener on ipcRenderer once control panel unmounted.

Once setup ipc channels, you'll get all your control panel needed informations:

  • tabs an object contains all the opened tab's informations
  • tabIDs array of opened tab's ids
  • activeID current active tab's id

Construct and style your control interface as your like.

Then you can send actions to control the browser view, the actions can require from electron-as-browser/control:

import {
  sendEnterURL, // sendEnterURL(url) to load url
  sendChangeURL, // sendChangeURL(url) on addressbar input change
  sendGoBack,
  sendGoForward,
  sendReload,
  sendStop,
  sendNewTab, // sendNewTab([url])
  sendSwitchTab, // sendSwitchTab(toID)
  sendCloseTab // sendCloseTab(id)
} from 'electron-as-browser/control';

See example for a full control interface implementation.

Run Example

  • yarn install
  • yarn start:control
  • yarn start

electron-as-browser's People

Contributors

abyabyss avatar dependabot[bot] avatar hulufei avatar juancgarza avatar pandapip1 avatar time-river avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

electron-as-browser's Issues

twice window.open but can not open a tab

A very cool project, and i found
there is a strange case when i ran project with a simple main page just like this below:

<div><button id="b2" > test2 </button></div>
<script>
b2 = document.getElementById("b2")
b2.addEventListener("click",
    (event) => {
        alert('b2')
        window.open('https://www.google.com', 'bbbddd');
    },
    false,
)
</script>

First when i clicked the button 'test2' to open a new Google tab-page, it worked;

then i closed this new-opend page by clicking the 'x' on the tab card;

so that i switched to the original simple main page;

and next, i clicked the button 'test2' again to open a new Google tab-page, BUT it can't work.

As I repeated these steps with the simple page on Chrome browser, it work well.

i found it would worked as long as using un-uniqe value on the second parm of func 'window.open' like

window.open('https://www.google.com',  'bbbddd'+new Date().getTime());

Can u help this out?

bug when electron upgrade

Hi,
I've tried to run this module under electron version 25, but it doesn't work. Is it possible to update it with the latest version of electron? Thank you.

Unresponsive Controls Bar

Hey ,

I'm trying to recreate a similar experiment from scratch by putting two BrowserViews.

However, once I add the second browser view - the first browser view goes unresponsive - but is still in the background. I am not sure how to focus it back when I click on it.

Have you run into this? I feel like I might be missing a flag maybe.

Debug value and errors

Setting debug to false in order to prevent the dev tools window from popping up results in getting a blank page.

Is there a way to disable the dev tools window popping up?

I am also getting the following errors upon start in the console:

(node:517563) UnhandledPromiseRejectionWarning: Error: An object could not be cloned.
    at EventEmitter.t.ipcRendererInternal.send (node:electron/js2c/renderer_init:85:328)
    at EventEmitter.<anonymous> (node:electron/js2c/renderer_init:81:357)
(Use `electron --trace-warnings ...` to show where the warning was created)
(node:517563) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

Add typings

Pretty simple request: add .d.ts files for all the .js files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.