Skip to content

Instantly share code, notes, and snippets.

@imdwit imdwit/init.popup.js Secret
Created Aug 24, 2019

Embed
What would you like to do?
import '@/js/firebase';
import { Elm } from '@/PopUp.elm';
import { loginWithPassword } from '@/js/auth';
let app;
const port = chrome.runtime.connect({ name: 'popUp' });
port.onMessage.addListener((cmd, ...rest) => {
// mount app on first broadcast
switch (cmd.tag) {
case 'init': {
if (!app) {
app = Elm.Popup.init({ flags: cmd.payload });
return subscriptions(app);
}
return subscriptions(app);
}
case 'broadcast': {
app.ports.extIn.send(cmd);
}
default: {
}
}
});
function subscriptions(app) {
app.ports.extOut.subscribe(cmd => {
switch (cmd.tag) {
case 'syncPopup': {
return chrome.runtime.sendMessage(cmd, console.log);
}
case 'getHostname': {
chrome.tabs.getSelected(null, function(tab) {
var url = new URL(tab.url);
var domain = url.hostname;
app.ports.extIn.send({ tag: 'gotHostname', hostname: domain });
});
return;
}
default: {
console.log('unknown', cmd);
}
}
});
app.ports.authOut.subscribe(cmd => {
switch (cmd.tag) {
case 'LoginWithPassword':
return loginWithPassword(cmd.credentials).then(user => {
app.ports.authIn.send({ tag: 'loginWithPasswordComplete', user });
});
default: {
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.