kfix / MacPin

a webapp container & site specific browser made from WebKit.swift and JavaScriptCore
GNU General Public License v3.0
334 stars 28 forks source link

Multi-tab app, with notifications. #33

Open codefriar opened 4 years ago

codefriar commented 4 years ago

I'm trying to work my way through building a perfect 'franz' style app in macpin.

So far, I've got Gmail, Gcal and Slack setup, and the app reliably loads all three sites and they function 99% perfectly fine.

That last 1% though, is driving me nuts. Whenever the app starts, all 3 tabs show their respective webapp's request to display desktop notifications.

However, no notifications are ever displayed, and the app doesn't 'remember' the notification settings after a restart.

Below is what I've hacked together.

/*
 * eslint-env applescript
 * eslint-env builtins
 * eslint-env es6
 * eslint eqeqeq:0, quotes:0, space-infix-ops:0, curly:0
*/
"use strict";

const {app, BrowserWindow, WebView} = require('@MacPin');

let injectTab = require('app_injectTab.js');
let enDarken = require('enDarken.js');

let slackTab = {
  url: 'https://app.slack.com/client/T0FSN3Z16/D19MH3CQ2/thread/C2K8LNETG-1574707732.133300',
  authorizedOriginsForNotifications: [
        'https://app.slack.com',
        'https://app.slack.com:443'
    ],
    style: ['autodark']
}

let sfCalendarTab = {
  url: 'https://calendar.google.com/calendar/r/customday?pli=1',
  authorizedOriginsForNotifications: [
        'https://notifications.google.com',
        'https://notifications.google.com:443'
    ],
}
let sfMailTab = {
  url: 'https://mail.google.com/mail/u/0/#inbox',
  authorizedOriginsForNotifications: [
        'https://notifications.google.com',
        'https://notifications.google.com:443'
    ]
}
var SFCalendar = new WebView(sfCalendarTab);
var SFMail = new WebView(sfMailTab);
var SFSlack = new WebView(slackTab);

let browser = new BrowserWindow();

app.on("postedDesktopNotification", (note, tab) => {
    console.log(Date() + `[${tab.url}] posted HTML5 desktop notification: ${note.id}`);
    console.log(JSON.stringify(note));
    return false
});

app.on('handleClickedNotification', (note) => {
    console.log("App signals a clicked notification: "+ JSON.stringify(note));
    return false;
});

app.on('didWindowOpenForURL', function(url, newTab, tab) {
    if (url.length > 0 && newTab)
        console.log(`window.open(${url})`);

    return false; // macpin will popup(newTab, url) and return newTab to tab's JS
});

let openInChrome = function(tab) {
    console.log(`app.js: opening (${tab.url}) in chrome`);
    switch (app.platform) {
        case "OSX":
            app.openURL(tab.url, "com.google.Chrome");
            break;
        case "iOS":
            app.openURL(tab.url,
                tab.url.startsWith("https://") ? "googlechromes" : "googlechrome");
            break;
    }
};

// handles cmd-line and LaunchServices openURL()s
app.on('launchURL', (url) => {
    console.log("app.js: launching " + url);
    var comps = url.split(':'),
        scheme = comps.shift(),
        addr = comps.shift();
    switch (scheme + ':') {
        default:
            browser.tabSelected = new WebView({url: url}); //FIXME: retains???!
    }
});
// app.on('launchURL', (url) => new $.WebView({url: url}));

app.on('networkIsOffline', (url, tab) => {
    console.log(`Could not navigate to ${url} - network is offline!`);
});

let setAgent = function(agent, tab) { tab.userAgent = agent; };
let getAgent = function(tab) { tab.evalJS(`window.alert(navigator.userAgent);`); };

app.on('AppWillFinishLaunching', (AppUI) => {
    //browser.unhideApp();
    browser.addShortcut('DarkMode', [], enDarken);

    // http://user-agents.me
    browser.addShortcut('Show current User-Agent', [], getAgent);

    if (app.doesAppExist("com.google.Chrome")) browser.addShortcut('Open in Chrome', [], openInChrome);

    //browser.addShortcut('HTML5 editor -> preview', editorPreview);
    //editorPreview should find editor tab (passed by ref?) and render its source

    //  WebView.init ends up with WebViewInitProps.handlers: <__NSFrozenDictionaryM>

    if (app.platform === "OSX") app.changeAppIcon('icon.png');
    //browser.tabSelected = new $.WebView({url: 'http://github.com/kfix/MacPin'});

    AppUI.browserController = browser; // make sure main app menu can get at our shortcuts
    console.log(app.browserController);

  // shuffle the _tabs using the tabs Proxy
    browser.tabs.push(SFMail);
  browser.tabs.push(SFCalendar);
  browser.tabs.push(SFSlack);

});

app.on('AppFinishedLaunching', (launchURLs) => {
    console.log(launchURLs);
});

app.on('AppShouldTerminate', (app) => {
    console.log("main.js shutting down!");
    docTab = null;
    gitTab = null;
});

// https://www.lucidchart.com/techblog/2018/02/14/javascriptcore-the-holy-grail-of-cross-platform/
//app.loadAppScript('fetchDeps.js`);
//let fetchURL = require('fetchURL');
kfix commented 3 years ago

Yeah, I've had similar issues with the bundled Slack app -- I've since given into using their Electron app since its got Dark Mode now.

There was a daily ritual to click the on-page banner to re-enable notifications.