Skip to content

Commit

Permalink
Add connection status to popup
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jan 8, 2020
1 parent daed667 commit 703b0a7
Show file tree
Hide file tree
Showing 27 changed files with 465 additions and 230 deletions.
15 changes: 1 addition & 14 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ import (
var (
tabActions = map[string]tabAction{}
urlActions = map[string]urlAction{}

scriptIcons = map[string]*aw.Icon{}

imageExts = map[string]bool{
".png": true,
".jpg": true,
".jpeg": true,
".gif": true,
".icns": true,
}
)

type tabAction interface {
Expand Down Expand Up @@ -90,12 +80,9 @@ func loadURLActions() error {
log.Printf("loaded URL action %q from %q", name, util.PrettyPath(path))
a := uAction{
name: name,
icon: iconURL,
icon: actionIcon(name, iconURL),
script: path,
}
if icon, ok := scriptIcons[name]; ok {
a.icon = icon
}
urlActions[name] = a
}

Expand Down
5 changes: 1 addition & 4 deletions actions_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ func (ca customAction) Add(it *aw.Item) {
case "tab":
m.Icon(iconTab).Var("CMD", "tab")
case "url":
m.Var("CMD", "url").Icon(iconURL)
if icon, ok := scriptIcons[ca.name]; ok {
m.Icon(icon)
}
m.Var("CMD", "url").Icon(actionIcon(ca.name, iconURL))
case "bookmarklet":
m.Var("CMD", "run-bookmarklet").Var("BOOKMARK", ca.id).
Icon(iconBookmarklet)
Expand Down
35 changes: 27 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ var (
}
)

func runOpenURL(_ []string) error {
wf.Configure(aw.TextErrors(true))
log.Printf("opening URL %q ...", URL)
_, err := util.RunCmd(exec.Command("open", URL))
return err
}

// func runOpenURL(_ []string) error {
// wf.Configure(aw.TextErrors(true))
// log.Printf("opening URL %q ...", URL)
// _, err := util.RunCmd(exec.Command("open", URL))
// return err
// }

// search Firefox history
func runHistory(_ []string) error {
checkForUpdate()
if len(query) < 3 {
Expand Down Expand Up @@ -208,6 +209,7 @@ func runHistory(_ []string) error {
return nil
}

// search Firefox bookmarks
func runBookmarks(_ []string) error {
checkForUpdate()
if len(query) < 3 {
Expand Down Expand Up @@ -251,6 +253,7 @@ func runBookmarks(_ []string) error {
return nil
}

// search Firefox bookmarklets
func runBookmarklets(_ []string) error {
checkForUpdate()
if len(query) < 3 {
Expand Down Expand Up @@ -284,6 +287,7 @@ func runBookmarklets(_ []string) error {
return nil
}

// execute a bookmarklet in a tab
func runBookmarklet(_ []string) error {
wf.Configure(aw.TextErrors(true))
log.Printf("running bookmarklet %q in tab #%d ...", bookmarkID, tabID)
Expand All @@ -292,6 +296,7 @@ func runBookmarklet(_ []string) error {
RunBookmarklet(RunBookmarkletArg{BookmarkID: bookmarkID, TabID: tabID})
}

// filter open Firefox tabs
func runTabs(_ []string) error {
log.Printf("fetching tabs for query %q ...", query)
checkForUpdate()
Expand Down Expand Up @@ -337,6 +342,7 @@ func runTabs(_ []string) error {
return nil
}

// execute a tab action on the given tab
func runTabAction(_ []string) error {
wf.Configure(aw.TextErrors(true))
log.Printf("running action %q on tab #%d ...", action, tabID)
Expand All @@ -347,6 +353,7 @@ func runTabAction(_ []string) error {
return a.Run(tabID)
}

// run an action on a URL
func runURLAction(_ []string) error {
wf.Configure(aw.TextErrors(true))
log.Printf("running action %q on URL %q ...", action, URL)
Expand All @@ -357,6 +364,7 @@ func runURLAction(_ []string) error {
return a.Run(URL)
}

// show actions for currently-active tab
func runCurrentTab(_ []string) error {
tab, err := mustClient().CurrentTab()
if err != nil {
Expand All @@ -367,6 +375,7 @@ func runCurrentTab(_ []string) error {
return runActions([]string{})
}

// filter actions for tab or URL
func runActions(_ []string) error {
if tabID != 0 {
for _, a := range tabActions {
Expand All @@ -388,7 +397,7 @@ func runActions(_ []string) error {
wf.NewItem(a.name).
UID(a.id).
Copytext("bml:"+a.id+","+a.name).
Icon(iconBookmarklet).
Icon(actionIcon(a.name, iconBookmarklet)).
Valid(true).
Var("CMD", "run-bookmarklet").
Var("BOOKMARK", a.id).
Expand All @@ -398,6 +407,9 @@ func runActions(_ []string) error {

if URL != "" {
for _, a := range urlActions {
if a.Name() == urlDefault {
continue
}
wf.NewItem(a.Name()).
UID(a.Name()).
Copytext(a.Name()).
Expand Down Expand Up @@ -431,6 +443,7 @@ func runUpdate(_ []string) error {
return nil
}

// show workflow status and options
func runStatus(_ []string) error {
if c, err := newClient(); err != nil {
wf.NewItem("No Connection to Firefox").
Expand All @@ -448,6 +461,12 @@ func runStatus(_ []string) error {
}
}

wf.NewItem("Register Workflow with Firefox").
Subtitle("Use if you've updated or moved the workflow and it isn't working").
Autocomplete("workflow:register").
Icon(iconInstall).
Valid(false)

if wf.UpdateAvailable() {
wf.NewItem("Update Available").
Subtitle("↩ or ⇥ to install new version").
Expand Down
108 changes: 42 additions & 66 deletions extension/alfred.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,8 @@
*/
const appName = 'net.deanishe.alfred.firefox';

const iconConnected = 'icons/active.svg';
const iconDisconnected = 'icons/inactive.svg';

/**
* Window object.
* @param {windows.Window} - Native window object to create Window from.
* @return {Object} - API Window object.
*/
/*
const Window = win => {
let obj = {};
win = win || {};
obj.id = win.id || 0;
obj.title = win.title || '';
obj.active = win.focused || false;
obj.tabs = [];
win.tabs.forEach(tab => {
let t = Tab(tab);
console.log(`[tabs] ${t}`);
obj.tabs.push(t);
});
obj.toString = function() {
return `#${this.id} "${this.title}", active=${this.active}, ${this.tabs.length} tab(s)`;
};
return obj;
};
*/
const iconConnected = 'icons/bowler.svg';
const iconDisconnected = 'icons/bowler-red.svg';

/**
* Tab object.
Expand Down Expand Up @@ -136,43 +108,73 @@ const Download = di => {
const Background = function() {
var self = this;

self.port = null,
self.connected = false;

self.onConnected = port => {
self.port = port;
port.onMessage.addListener(self.receive);
console.debug('connected to popup');
};

self.send = msg => {
self.port.postMessage(msg);
console.debug('sent message', msg);
};

self.receive = msg => {
console.debug('received message', msg);
if ('command' in msg) {
switch (msg.command) {
case 'status':
self.send({ status: self.connected ? 'connected' : 'disconnected' });
return;
case 'reconnect':
console.debug('reconnecting to native app ...');
self.connectNative();
return;
}
}
};

self.nativePort = null;

self.connectNative = () => {
let connected = false;
self.connected = false;

let listener = payload => {
if (!connected) {
connected = true;
self.nativePort.onDisconnect.removeListener(self.connectFailed);
self.connected();
if (!self.connected) {
self.connected = true;
self.nativePort.onDisconnect.removeListener(self.connectNativeFailed);
self.onConnectedNative();
}
self.receiveNative(payload);
};

self.nativePort = browser.runtime.connectNative(appName);
self.nativePort.onMessage.addListener(listener);
self.nativePort.onDisconnect.addListener(self.connectFailed);
self.nativePort.onDisconnect.addListener(self.connectNativeFailed);
};

/**
* Callback for connection failure.
* Logs an error message to the console.
*/
self.connectFailed = port => {
self.connectNativeFailed = port => {
let msg = '';
if (port.error) {
msg = port.error.message;
}
console.error(`connection failed: ${msg}`);
self.connected = false;
console.error(`native client connection failed: ${msg}`);
browser.browserAction.setIcon({ path: iconDisconnected });
};

/**
* Callback for successful connection to native application.
* Logs a message to the console.
*/
self.connected = () => {
self.onConnectedNative = () => {
console.log('connected to native client');
browser.browserAction.setIcon({ path: iconConnected });
};
Expand Down Expand Up @@ -283,33 +285,6 @@ const Background = function() {
});
};

/**
* Handle "all-windows" command.
* @return {Promise} - Resolves to array of Window objects for all windows
* of type "normal".
*/
// self.allWindows = () => {
// return browser.windows
// .getAll({ populate: true, windowTypes: ['normal'] })
// .then(wins => {
// return wins.map(w => Window(w));
// });
// };

/**
* Handle "current-window" command.
* @return {Promise} - Resolves to Window for active window.
*/
// self.currentWindow = () => {
// return browser.windows
// .getCurrent({ populate: true, windowTypes: ['normal'] })
// .then(w => {
// let win = Window(w);
// console.log(`[current-window] ${win}`);
// return win;
// });
// };

/**
* Handle "all-tabs" command.
* @return {Promise} - Resolves to array of Tab objects for all tabs.
Expand Down Expand Up @@ -517,6 +492,7 @@ const Background = function() {
});
};

browser.runtime.onConnect.addListener(self.onConnected);
self.connectNative();
console.log(`started`);
};
Expand Down
1 change: 0 additions & 1 deletion extension/icons/active.svg

This file was deleted.

1 change: 1 addition & 0 deletions extension/icons/bowler-black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension/icons/bowler-purple.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension/icons/bowler-red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension/icons/bowler.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions extension/icons/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion extension/icons/inactive.svg

This file was deleted.

14 changes: 7 additions & 7 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
"browser_action": {
"browser_style": true,
"default_title": "Alfred-Firefox",
"default_popup": "window.html",
"default_popup": "popup.html",

"default_icon": {
"16": "icons/active.svg",
"32": "icons/active.svg",
"64": "icons/active.svg"
"32": "icons/bowler.svg"
}
},

"browser_specific_settings": {
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "70.0"
Expand All @@ -33,8 +31,10 @@
},

"icons": {
"48": "icons/active.svg",
"96": "icons/active.svg"
"32": "icons/bowler-black.svg",
"48": "icons/bowler-black.svg",
"96": "icons/bowler-black.svg",
"128": "icons/bowler-black.svg"
},

"manifest_version": 2,
Expand Down
Loading

0 comments on commit 703b0a7

Please sign in to comment.