te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Electron - How to use main and renderer processes - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron - How to use main and renderer processes - Stack Overflow

programmeradmin3浏览0评论

At this point, after much research and googling, I understand what the main and the renderer do, and their importance in an Electron app.

However, here I am sending out my plea to be answered by all those knowledgeable people out there: Please can I have a clear explanation of how exactly to implement this in my app?

I have a main.js, index.html, and style.css, and I'm trying to fire a javascript function from the html file. @Manprit Singh Sahota has the same question, button click event binding in Electron js, and solves it (lucky him), but simply states that he's setting his function in renderer.js without explaining where and what and how. @NealR also has a similar question but also doesn't explain how he's associating his renderer.js.

Please, someone unveil the secret of where this mysterious file is kept, and how I can reference it in my program?

Don't advise the Electron documentation, I've already been through it and it seems to need some serious improvement...

main.js

const electron = require('electron');
const { app, BrowserWindow } = require('electron');

//stuff creating window...

function applyFormattingRules() {
  console.log('called!');
};

//more stuff opening and closing window...

index.html

<head>
    //...
    <script src="main.js"></script>
</head>

<body>
    //...
    <button type="button" class="btn btn-secondary" name="applyRules"
    onclick="applyFormattingRules()">Apply formatting</button>
</body>

My window works fine, no errors there. But when I click the button, nothing happens, and nothing logs to the console. Maybe I'm doing something wrong in the code but all my research seems to point to the Electron main and renderer processes.

Any advice much appreciated.

At this point, after much research and googling, I understand what the main and the renderer do, and their importance in an Electron app.

However, here I am sending out my plea to be answered by all those knowledgeable people out there: Please can I have a clear explanation of how exactly to implement this in my app?

I have a main.js, index.html, and style.css, and I'm trying to fire a javascript function from the html file. @Manprit Singh Sahota has the same question, button click event binding in Electron js, and solves it (lucky him), but simply states that he's setting his function in renderer.js without explaining where and what and how. @NealR also has a similar question but also doesn't explain how he's associating his renderer.js.

Please, someone unveil the secret of where this mysterious file is kept, and how I can reference it in my program?

Don't advise the Electron documentation, I've already been through it and it seems to need some serious improvement...

main.js

const electron = require('electron');
const { app, BrowserWindow } = require('electron');

//stuff creating window...

function applyFormattingRules() {
  console.log('called!');
};

//more stuff opening and closing window...

index.html

<head>
    //...
    <script src="main.js"></script>
</head>

<body>
    //...
    <button type="button" class="btn btn-secondary" name="applyRules"
    onclick="applyFormattingRules()">Apply formatting</button>
</body>

My window works fine, no errors there. But when I click the button, nothing happens, and nothing logs to the console. Maybe I'm doing something wrong in the code but all my research seems to point to the Electron main and renderer processes.

Any advice much appreciated.

Share Improve this question edited Dec 17, 2019 at 11:44 half of a glazier asked Dec 17, 2019 at 11:20 half of a glazierhalf of a glazier 2,0762 gold badges26 silver badges51 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

With electron you have to pay attention in which context your JS files run:

  • process / nodejs context
    Typically main.js runs here, it does all the bootstrapping of the electron environment and browser / electron windows. At some point you will tell a window to load some HTML file - which enters the second context.
  • electron window / browser context
    Anything that got loaded into a window, runs "remotely". To get JS files extecuted in the browser context, you pretty much do the same as with any other web application (use <script> tags etc).

Up to that point an electron app is not different to any other web application - the process/nodejs part acts as a server ponent, while the window context is the webpage/client context. Note that those contexts are only loosely coupled, you need IPC mechanisms to exchange data between them.

Still electron goes abit further - it allows to directly embed nodejs modules into a window context. This is possible due to some extensions made by the electron team to the underlying chrome libraries. Use that with caution as it might introduce security issues (there is even a security setting for this).

To get what you want:

  • create a window in main.js
  • load some HTML document into that window
  • refer to some other JS file in that HTML document, which now gets loaded along with the HTML document (thats the ominous render.js in your reference)
  • put some logic in that other JS file --> gets executed within the window context

There is a nice walkthrough to get a basic example up and running in the electron docs (https://electronjs/docs/tutorial/first-app).

The main process and the render process are pletely separate.

You define the main process in your package.json:

{
    "name": "my-electron-app",
    "version": "0.1.0",
    "description": "Super cool electron app",
    "main": "index.js",

    /* more scripts and dependencies here */
}

And then in the main process you create a window that loads your html resources:

    window = new BrowserWindow({
        x: windowState.x,
        y: windowState.y,
        width: windowState.width,
        height: windowState.height,
        backgroundColor: '#000000',
    })

    // load main file
    window.loadFile('path/to/index.html')

That html file acts just like any standard web page and can have its own styles and scripts. You should not be including your backend main.js file into your front end HTML. Instead, if you need to municate between your front end and back end, you can either set up a websocket for munication or use the ipcMain and ipcRenderer to send messages.

发布评论

评论列表(0)

  1. 暂无评论