commit
104a9666ff
26 changed files with 329 additions and 132 deletions
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.5 KiB |
@ -1,92 +0,0 @@ |
|||||||
import operations from 'shared/operations'; |
|
||||||
import messages from 'shared/messages'; |
|
||||||
import * as tabs from 'background/tabs'; |
|
||||||
import * as zooms from 'background/zooms'; |
|
||||||
|
|
||||||
const sendConsoleShowCommand = (tab, command) => { |
|
||||||
return browser.tabs.sendMessage(tab.id, { |
|
||||||
type: messages.CONSOLE_SHOW_COMMAND, |
|
||||||
command, |
|
||||||
}); |
|
||||||
}; |
|
||||||
|
|
||||||
// This switch statement is only gonna get longer as more
|
|
||||||
// features are added, so disable complexity check
|
|
||||||
/* eslint-disable complexity */ |
|
||||||
const exec = (operation, tab) => { |
|
||||||
switch (operation.type) { |
|
||||||
case operations.TAB_CLOSE: |
|
||||||
return tabs.closeTab(tab.id); |
|
||||||
case operations.TAB_CLOSE_FORCE: |
|
||||||
return tabs.closeTabForce(tab.id); |
|
||||||
case operations.TAB_REOPEN: |
|
||||||
return tabs.reopenTab(); |
|
||||||
case operations.TAB_PREV: |
|
||||||
return tabs.selectPrevTab(tab.index, operation.count); |
|
||||||
case operations.TAB_NEXT: |
|
||||||
return tabs.selectNextTab(tab.index, operation.count); |
|
||||||
case operations.TAB_FIRST: |
|
||||||
return tabs.selectFirstTab(); |
|
||||||
case operations.TAB_LAST: |
|
||||||
return tabs.selectLastTab(); |
|
||||||
case operations.TAB_PREV_SEL: |
|
||||||
return tabs.selectPrevSelTab(); |
|
||||||
case operations.TAB_RELOAD: |
|
||||||
return tabs.reload(tab, operation.cache); |
|
||||||
case operations.TAB_PIN: |
|
||||||
return tabs.updateTabPinned(tab, true); |
|
||||||
case operations.TAB_UNPIN: |
|
||||||
return tabs.updateTabPinned(tab, false); |
|
||||||
case operations.TAB_TOGGLE_PINNED: |
|
||||||
return tabs.toggleTabPinned(tab); |
|
||||||
case operations.TAB_DUPLICATE: |
|
||||||
return tabs.duplicate(tab.id); |
|
||||||
case operations.ZOOM_IN: |
|
||||||
return zooms.zoomIn(); |
|
||||||
case operations.ZOOM_OUT: |
|
||||||
return zooms.zoomOut(); |
|
||||||
case operations.ZOOM_NEUTRAL: |
|
||||||
return zooms.neutral(); |
|
||||||
case operations.COMMAND_SHOW: |
|
||||||
return sendConsoleShowCommand(tab, ''); |
|
||||||
case operations.COMMAND_SHOW_OPEN: |
|
||||||
if (operation.alter) { |
|
||||||
// alter url
|
|
||||||
return sendConsoleShowCommand(tab, 'open ' + tab.url); |
|
||||||
} |
|
||||||
return sendConsoleShowCommand(tab, 'open '); |
|
||||||
case operations.COMMAND_SHOW_TABOPEN: |
|
||||||
if (operation.alter) { |
|
||||||
// alter url
|
|
||||||
return sendConsoleShowCommand(tab, 'tabopen ' + tab.url); |
|
||||||
} |
|
||||||
return sendConsoleShowCommand(tab, 'tabopen '); |
|
||||||
case operations.COMMAND_SHOW_WINOPEN: |
|
||||||
if (operation.alter) { |
|
||||||
// alter url
|
|
||||||
return sendConsoleShowCommand(tab, 'winopen ' + tab.url); |
|
||||||
} |
|
||||||
return sendConsoleShowCommand(tab, 'winopen '); |
|
||||||
case operations.COMMAND_SHOW_BUFFER: |
|
||||||
return sendConsoleShowCommand(tab, 'buffer '); |
|
||||||
case operations.FIND_START: |
|
||||||
return browser.tabs.sendMessage(tab.id, { |
|
||||||
type: messages.CONSOLE_SHOW_FIND |
|
||||||
}); |
|
||||||
case operations.CANCEL: |
|
||||||
return browser.tabs.sendMessage(tab.id, { |
|
||||||
type: messages.CONSOLE_HIDE, |
|
||||||
}); |
|
||||||
case operations.PAGE_SOURCE: |
|
||||||
return browser.tabs.create({ |
|
||||||
url: 'view-source:' + tab.url, |
|
||||||
index: tab.index + 1, |
|
||||||
openerTabId: tab.id, |
|
||||||
}); |
|
||||||
default: |
|
||||||
return Promise.resolve(); |
|
||||||
} |
|
||||||
}; |
|
||||||
/* eslint-enable complexity */ |
|
||||||
|
|
||||||
export { exec }; |
|
@ -0,0 +1,45 @@ |
|||||||
|
import * as indicators from '../shared/indicators'; |
||||||
|
import messages from 'shared/messages'; |
||||||
|
|
||||||
|
export default class IndicatorComponent { |
||||||
|
constructor(store) { |
||||||
|
this.store = store; |
||||||
|
|
||||||
|
messages.onMessage(this.onMessage.bind(this)); |
||||||
|
|
||||||
|
browser.browserAction.onClicked.addListener(this.onClicked); |
||||||
|
browser.tabs.onActivated.addListener((info) => { |
||||||
|
return browser.tabs.query({ currentWindow: true }).then(() => { |
||||||
|
return this.onTabActivated(info); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
onTabActivated(info) { |
||||||
|
return browser.tabs.sendMessage(info.tabId, { |
||||||
|
type: messages.ADDON_ENABLED_QUERY, |
||||||
|
}).then((resp) => { |
||||||
|
return this.updateIndicator(resp.enabled); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
onClicked(tab) { |
||||||
|
browser.tabs.sendMessage(tab.id, { |
||||||
|
type: messages.ADDON_TOGGLE_ENABLED, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
onMessage(message) { |
||||||
|
switch (message.type) { |
||||||
|
case messages.ADDON_ENABLED_RESPONSE: |
||||||
|
return this.updateIndicator(message.enabled); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
updateIndicator(enabled) { |
||||||
|
if (enabled) { |
||||||
|
return indicators.enable(); |
||||||
|
} |
||||||
|
return indicators.disable(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,118 @@ |
|||||||
|
import messages from 'shared/messages'; |
||||||
|
import operations from 'shared/operations'; |
||||||
|
import * as tabs from '../shared//tabs'; |
||||||
|
import * as zooms from '../shared/zooms'; |
||||||
|
|
||||||
|
export default class BackgroundComponent { |
||||||
|
constructor(store) { |
||||||
|
this.store = store; |
||||||
|
|
||||||
|
browser.runtime.onMessage.addListener((message, sender) => { |
||||||
|
try { |
||||||
|
return this.onMessage(message, sender); |
||||||
|
} catch (e) { |
||||||
|
return browser.tabs.sendMessage(sender.tab.id, { |
||||||
|
type: messages.CONSOLE_SHOW_ERROR, |
||||||
|
text: e.message, |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
onMessage(message, sender) { |
||||||
|
switch (message.type) { |
||||||
|
case messages.BACKGROUND_OPERATION: |
||||||
|
return this.store.dispatch( |
||||||
|
this.exec(message.operation, sender.tab), |
||||||
|
sender); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// eslint-disable-next-line complexity
|
||||||
|
exec(operation, tab) { |
||||||
|
let tabState = this.store.getState().tab; |
||||||
|
|
||||||
|
switch (operation.type) { |
||||||
|
case operations.TAB_CLOSE: |
||||||
|
return tabs.closeTab(tab.id); |
||||||
|
case operations.TAB_CLOSE_FORCE: |
||||||
|
return tabs.closeTabForce(tab.id); |
||||||
|
case operations.TAB_REOPEN: |
||||||
|
return tabs.reopenTab(); |
||||||
|
case operations.TAB_PREV: |
||||||
|
return tabs.selectPrevTab(tab.index, operation.count); |
||||||
|
case operations.TAB_NEXT: |
||||||
|
return tabs.selectNextTab(tab.index, operation.count); |
||||||
|
case operations.TAB_FIRST: |
||||||
|
return tabs.selectFirstTab(); |
||||||
|
case operations.TAB_LAST: |
||||||
|
return tabs.selectLastTab(); |
||||||
|
case operations.TAB_PREV_SEL: |
||||||
|
if (tabState.previousSelected > 0) { |
||||||
|
return tabs.selectTab(tabState.previousSelected); |
||||||
|
} |
||||||
|
break; |
||||||
|
case operations.TAB_RELOAD: |
||||||
|
return tabs.reload(tab, operation.cache); |
||||||
|
case operations.TAB_PIN: |
||||||
|
return tabs.updateTabPinned(tab, true); |
||||||
|
case operations.TAB_UNPIN: |
||||||
|
return tabs.updateTabPinned(tab, false); |
||||||
|
case operations.TAB_TOGGLE_PINNED: |
||||||
|
return tabs.toggleTabPinned(tab); |
||||||
|
case operations.TAB_DUPLICATE: |
||||||
|
return tabs.duplicate(tab.id); |
||||||
|
case operations.ZOOM_IN: |
||||||
|
return zooms.zoomIn(); |
||||||
|
case operations.ZOOM_OUT: |
||||||
|
return zooms.zoomOut(); |
||||||
|
case operations.ZOOM_NEUTRAL: |
||||||
|
return zooms.neutral(); |
||||||
|
case operations.COMMAND_SHOW: |
||||||
|
return this.sendConsoleShowCommand(tab, ''); |
||||||
|
case operations.COMMAND_SHOW_OPEN: |
||||||
|
if (operation.alter) { |
||||||
|
// alter url
|
||||||
|
return this.sendConsoleShowCommand(tab, 'open ' + tab.url); |
||||||
|
} |
||||||
|
return this.sendConsoleShowCommand(tab, 'open '); |
||||||
|
case operations.COMMAND_SHOW_TABOPEN: |
||||||
|
if (operation.alter) { |
||||||
|
// alter url
|
||||||
|
return this.sendConsoleShowCommand(tab, 'tabopen ' + tab.url); |
||||||
|
} |
||||||
|
return this.sendConsoleShowCommand(tab, 'tabopen '); |
||||||
|
case operations.COMMAND_SHOW_WINOPEN: |
||||||
|
if (operation.alter) { |
||||||
|
// alter url
|
||||||
|
return this.sendConsoleShowCommand(tab, 'winopen ' + tab.url); |
||||||
|
} |
||||||
|
return this.sendConsoleShowCommand(tab, 'winopen '); |
||||||
|
case operations.COMMAND_SHOW_BUFFER: |
||||||
|
return this.sendConsoleShowCommand(tab, 'buffer '); |
||||||
|
case operations.FIND_START: |
||||||
|
return browser.tabs.sendMessage(tab.id, { |
||||||
|
type: messages.CONSOLE_SHOW_FIND |
||||||
|
}); |
||||||
|
case operations.CANCEL: |
||||||
|
return browser.tabs.sendMessage(tab.id, { |
||||||
|
type: messages.CONSOLE_HIDE, |
||||||
|
}); |
||||||
|
case operations.PAGE_SOURCE: |
||||||
|
return browser.tabs.create({ |
||||||
|
url: 'view-source:' + tab.url, |
||||||
|
index: tab.index + 1, |
||||||
|
openerTabId: tab.id, |
||||||
|
}); |
||||||
|
default: |
||||||
|
return Promise.resolve(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
sendConsoleShowCommand(tab, command) { |
||||||
|
return browser.tabs.sendMessage(tab.id, { |
||||||
|
type: messages.CONSOLE_SHOW_COMMAND, |
||||||
|
command, |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
import * as tabActions from '../actions/tab'; |
||||||
|
|
||||||
|
export default class TabComponent { |
||||||
|
constructor(store) { |
||||||
|
this.store = store; |
||||||
|
|
||||||
|
browser.tabs.onActivated.addListener((info) => { |
||||||
|
return browser.tabs.query({ currentWindow: true }).then(() => { |
||||||
|
return this.onTabActivated(info); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
onTabActivated(info) { |
||||||
|
return this.store.dispatch(tabActions.selected(info.tabId)); |
||||||
|
} |
||||||
|
} |
@ -1,15 +1,18 @@ |
|||||||
import settingReducer from './setting'; |
import settingReducer from './setting'; |
||||||
import findReducer from './find'; |
import findReducer from './find'; |
||||||
|
import tabReducer from './tab'; |
||||||
|
|
||||||
// Make setting reducer instead of re-use
|
// Make setting reducer instead of re-use
|
||||||
const defaultState = { |
const defaultState = { |
||||||
setting: settingReducer(undefined, {}), |
setting: settingReducer(undefined, {}), |
||||||
find: findReducer(undefined, {}), |
find: findReducer(undefined, {}), |
||||||
|
tab: tabReducer(undefined, {}), |
||||||
}; |
}; |
||||||
|
|
||||||
export default function reducer(state = defaultState, action = {}) { |
export default function reducer(state = defaultState, action = {}) { |
||||||
return Object.assign({}, state, { |
return Object.assign({}, state, { |
||||||
setting: settingReducer(state.setting, action), |
setting: settingReducer(state.setting, action), |
||||||
find: findReducer(state.find, action), |
find: findReducer(state.find, action), |
||||||
|
tab: tabReducer(state.tab, action), |
||||||
}); |
}); |
||||||
} |
} |
||||||
|
@ -0,0 +1,19 @@ |
|||||||
|
import actions from 'background/actions'; |
||||||
|
|
||||||
|
const defaultState = { |
||||||
|
previousSelected: -1, |
||||||
|
currentSelected: -1, |
||||||
|
}; |
||||||
|
|
||||||
|
export default function reducer(state = defaultState, action = {}) { |
||||||
|
switch (action.type) { |
||||||
|
case actions.TAB_SELECTED: |
||||||
|
return { |
||||||
|
previousSelected: state.currentSelected, |
||||||
|
currentSelected: action.tabId, |
||||||
|
}; |
||||||
|
default: |
||||||
|
return state; |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,10 @@ |
|||||||
|
const getCompletions = (keyword) => { |
||||||
|
return browser.tabs.query({ currentWindow: true }).then((tabs) => { |
||||||
|
let matched = tabs.filter((t) => { |
||||||
|
return t.url.includes(keyword) || t.title && t.title.includes(keyword); |
||||||
|
}); |
||||||
|
return matched; |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
export { getCompletions }; |
@ -0,0 +1,13 @@ |
|||||||
|
const enable = () => { |
||||||
|
return browser.browserAction.setIcon({ |
||||||
|
path: 'resources/enabled_32x32.png', |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
const disable = () => { |
||||||
|
return browser.browserAction.setIcon({ |
||||||
|
path: 'resources/disabled_32x32.png', |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
export { enable, disable }; |
@ -1,3 +0,0 @@ |
|||||||
import complete from './complete'; |
|
||||||
|
|
||||||
export { complete }; |
|
@ -0,0 +1,13 @@ |
|||||||
|
import actions from 'background/actions'; |
||||||
|
import * as tabActions from 'background/actions/tab'; |
||||||
|
|
||||||
|
describe("tab actions", () => { |
||||||
|
describe("selected", () => { |
||||||
|
it('create TAB_SELECTED action', () => { |
||||||
|
let action = tabActions.selected(123); |
||||||
|
expect(action.type).to.equal(actions.TAB_SELECTED); |
||||||
|
expect(action.tabId).to.equal(123); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
@ -0,0 +1,22 @@ |
|||||||
|
import actions from 'background/actions'; |
||||||
|
import tabReducer from 'background/reducers/tab'; |
||||||
|
|
||||||
|
describe("tab reducer", () => { |
||||||
|
it('return the initial state', () => { |
||||||
|
let state = tabReducer(undefined, {}); |
||||||
|
expect(state.previousSelected).to.equal(-1); |
||||||
|
expect(state.currentSelected).to.equal(-1); |
||||||
|
}); |
||||||
|
|
||||||
|
it('return next state for TAB_SELECTED', () => { |
||||||
|
let state = undefined; |
||||||
|
|
||||||
|
state = tabReducer(state, { type: actions.TAB_SELECTED, tabId: 123 }); |
||||||
|
expect(state.previousSelected).to.equal(-1); |
||||||
|
expect(state.currentSelected).to.equal(123); |
||||||
|
|
||||||
|
state = tabReducer(state, { type: actions.TAB_SELECTED, tabId: 456 }); |
||||||
|
expect(state.previousSelected).to.equal(123); |
||||||
|
expect(state.currentSelected).to.equal(456); |
||||||
|
}); |
||||||
|
}); |
Reference in new issue