npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-bridge-promise

v1.2.3

Published

electron call promise btween node<->chromium

Downloads

8

Readme

electron-bridge-promise

用法

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using Node.js
    <script>
        document.write(process.version);
    </script>
    and Electron <script>document.write(process.versions['electron'])</script>.
  </body>
</html>
var {ipcMain, app} = require('electron');
var BrowserWindow = require('electron-bridge-promise');

app.on('window-all-closed', function() {
    app.quit();
});

app.on('ready', function() {
    var win = new BrowserWindow({
        width: 1024,
        height: 768,
    });

    win.openURL('file://' + __dirname + '/app.html')
    .then((uri) => {
        console.log('open ready: ', uri);
        return win.callChrome(()=>{
            console.log('i am in chrome');
            return new Promise((resolve, reject) => {
                setTimeout(()=>{
                    console.log('log from node');
                    resolve('abc');
                },1000);
            });
        });
    })
    .then(function(ret) {
        console.log('retttttttt', typeof ret, ret);
    })
    .catch(function(err) {
        console.log('errrrrrrrr', typeof err, err instanceof Error, err, err.stack);
    });
});
# 把上面的 程序分别保存为 app.html 和 above.js,然后执行用 Electron 执行 above.js
$ Electron.app/Contents/MacOS/Electron above.js

然后,在 devTool 下执行下面这段 js 片段

window.callNode(function(name) {
    console.log('i am in node');
    return new Promise((resolve, reject) => {
        setTimeout(resolve, 1000, name);
    });
}, 'moon')
.then(name => {
    console.log('i am in chrome', name);
})
.catch(err => console.log('errr name', err));

moduleAttachToWindow

属性

[
    {from: 'fs', to: 'FS'},
    {from: 'http', to: 'Http'},
]

指定要挂在 window 上的模块名字

模块的查找规则,同node的模块查找规则

bridgeTimeout

属性

bridgeTimeout 用来设定桥通信的超时时间,毫秒单位。

比如,你在 chrome 中执行一个异步任务,但是这个异步任务迟迟不能完成。那就会引发超时。

这个超时时间有个范围,必须在 3 秒 到 600 秒之间。默认值是 10 秒

    var win = new BrowserWindow({
        bridgeTimeout: 10000,
    });

dev

属性

用来设置打开devTools 默认值 true

帮助你自动执行 window.openDevTools() 因为开发的时候 devTools 的使用频率太高了

当设置为 false 时,不会打开 openDevTools,并且禁止打开 openDevTools(无论如何都打不开开发工具)

openURL302(url)

方法

打开url 本方法考虑了 302,它会打开最终目标页。当然,如果页面没有 302,那是最好,直接到达目标页。 返回promise, resolve 最终的 url。

openURL(url)

方法

打开url url需要指定成跳转的最终页面,不帮助跳转到最终页 返回promise, resolve 当前 url

callChrome(script, ...args)

目前你在 node 进程下,去 chrome 环境中执行一段 js,就要用的该方法
参数:
    func, arg1, arg2, arg3, ...
    func是要在chronmium的js环境中执行的函数
    arg1 - arg3 是传递给func的参数
  或者,你可以指定文件
    filename, arg1, arg2, arg3, ...
    filename 是你想注入到前端的js脚本
    arg1 - arg3 是传递给前端脚本的参数
返回值:
  script 中包含的函数的返回值。推荐你写的脚本返回 promise。
  如果是 promise 的话: 
    resolve你的 js 片段执行结果,
    reject 执行出错,chrome 中的 Error 对象,会尽量保持原样,发送到 node 进程中

关于超时:见 #bridgeTimeout ,默认情况下,你的脚本当超过 10 秒还没有返回,则会触发超时的 Error

## window.callNode(script, ...args)

假设你在 chrome 下,那么可以用  window.callNode 在 node 进程中执行代码。

参数和返回值的设计和 callChrome 一样。

test

electron test/index.js