gmail.js icon indicating copy to clipboard operation
gmail.js copied to clipboard

Error when using observe.on

Open htbrown opened this issue 6 years ago • 5 comments

Hello! I'm trying to make an electron gmail app for personal use, and I'm fairly new to this sort of thing, but I get an error telling me $.each is not a function. I know this is a jQuery thing, but I've tried some troubleshooting steps and it hasn't worked. Here's my entire app.js file:

const {app, BrowserWindow} = require('electron'), log = require('hexalogger'), GmailFactory = require('gmail-js'), notify = require('electron-notification');

const gmail = new GmailFactory.Gmail();

let win;

function checkMail() {
    setInterval(() => {
        gmail.observe.on("new-email", (id, url, body, xhr) => {
            notify(id, {
                body: body
            }, () => {
                win.focus();
            })
        })
    }, 2000)
}

app.on('ready', () => {
    win = new BrowserWindow({
        width: 1000,
        height: 600
    });
    win.loadURL('https://mail.google.com');
    win.on('closed', () => {
        win = null;
    });
    setTimeout(checkMail, 3000);
});

/* gmail.observe.on("new-email", (id, url, body, xhr) => {
    notify(id, {
        body: body
    }, () => {
        win.focus();
    })
 }) */

Help very much appreciated.

htbrown avatar Dec 15 '18 14:12 htbrown

Can you identify the line failing? That might help.

Also: I’ve never tested this with electron, so I’m not sure it will work.

Also: you are registering the same event-handlers again and again and again. That’s probably not healthy.

josteink avatar Dec 15 '18 14:12 josteink

Right, so I updated the code slightly realising that .on emits when something happens it doesn't just check once, updated code is below. The error message is

TypeError: $.each is not a function
    at Object.Gmail.api.observe.initialize_dom_observers (/Volumes/HaydenDrive/Code/Applications/Gmail/node_modules/gmail-js/src/gmail.js:1974:11)
    at Object.Gmail.api.observe.on_dom (/Volumes/HaydenDrive/Code/Applications/Gmail/node_modules/gmail-js/src/gmail.js:2032:25)
    at Object.Gmail.api.observe.on (/Volumes/HaydenDrive/Code/Applications/Gmail/node_modules/gmail-js/src/gmail.js:1726:24)
    at App.app.on (/Volumes/HaydenDrive/Code/Applications/Gmail/app.js:18:23)
    at App.emit (events.js:187:15)

Updated code:

const {app, BrowserWindow} = require('electron'), log = require('hexalogger'), GmailFactory = require('gmail-js'), notify = require('electron-notification');

const gmail = new GmailFactory.Gmail();

let win;


app.on('ready', () => {
    win = new BrowserWindow({
        width: 1000,
        height: 600
    });
    win.loadURL('https://mail.google.com');
    win.on('closed', () => {
        win = null;
    });
    try {
        gmail.observe.on("new-email", (id, url, body, xhr) => {
            notify(id, {
                body: body
            }, () => {
                win.focus();
            })
        })
    } catch (err) {
        log.error(err.stack);
        
    }
});

htbrown avatar Dec 15 '18 14:12 htbrown

I have tried getting jQuery into the file, but that just threw the same error. It's currently saying that the error is on line 18.

htbrown avatar Dec 16 '18 13:12 htbrown

Thanks for the update.

Please bare in mind I've never tested gmail-js in a Electron context (in fact I've never written an Electron-app).

That said, I see you have another, structural issue which I think you will have to solve before you can get running.

From what I can tell you're pulling gmail-js into your Electron-app context, but gmail-js needs to be run in a browser-context where Gmail is the loaded application.

That means you will have to:

  1. find a way to inject a custom script (gmail-js AND your custom event-handler code) into the loaded window,
  2. signal your Electron app context from "the inside" (possibly with window.postMessage()).
  3. subscribe to such message in your Electron app context.
  4. and finally... trigger notification when such messages are received.

I suspect you will find Electron resources online for all of these online if you do a little searching.

All in all, a bit of work, but shouldn't be undoable, and certainly not any worse than your avarage WebExtension-developer has to cope with for a similar use-case.

josteink avatar Dec 17 '18 07:12 josteink

Ok thanks. I'll try in a couple of days, as I'm fairly busy with school and stuff right now.

htbrown avatar Dec 17 '18 17:12 htbrown

Old issue is old. Closing.

josteink avatar Oct 11 '23 08:10 josteink