Skip to content

Commit

Permalink
1.1.1: add an option to turn on the button of context menu in the page
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwangjie committed Aug 1, 2018
1 parent 644db18 commit 4b9cab3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 39 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### v1.1.1 8/1/2018

- feat: add an option to turn on the button of context menu in the page
- fix: do not await sync complete when access storage

### v1.1.0 7/29/2018

- feat: add an operation to store all tabs in all windows (in context menus and keyboard shortcut)
Expand Down
5 changes: 4 additions & 1 deletion src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"opt_desc_pinNewList": {
"message": "Auto pin new list"
},
"opt_desc_pageContext": {
"message": "Turn on the button of context menu in the page"
},
"opt_desc_syncOptions": {
"message": "Sync settings"
},
Expand Down Expand Up @@ -173,4 +176,4 @@
"ui_title_down_btn": {
"message": "move list down"
}
}
}
5 changes: 4 additions & 1 deletion src/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"opt_desc_pinNewList": {
"message": "自动固定新列表"
},
"opt_desc_pageContext": {
"message": "开启页面中的右键菜单中的按钮"
},
"opt_desc_addHistory": {
"message": "将储存的标签加入浏览器历史记录中"
},
Expand Down Expand Up @@ -173,4 +176,4 @@
"ui_title_down_btn": {
"message": "向下移动列表"
}
}
}
54 changes: 22 additions & 32 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,50 +36,40 @@ const updateBrowserAction = action => {
}
}

const setupContextMenus = () => {
browser.contextMenus.removeAll()
browser.contextMenus.create({
id: 'STORE_SELECTED_TABS',
title: __('menu_STORE_SELECTED_TABS'),
contexts: ['browser_action'],
})
browser.contextMenus.create({
id: 'STORE_ALL_TABS_IN_CURRENT_WINDOW',
title: __('menu_STORE_ALL_TABS_IN_CURRENT_WINDOW'),
contexts: ['browser_action'],
})
browser.contextMenus.create({
id: 'SHOW_TAB_LIST',
title: __('menu_SHOW_TAB_LIST'),
contexts: ['browser_action'],
})
browser.contextMenus.create({
id: 'STORE_ALL_TABS_IN_ALL_WINDOWS',
title: __('menu_STORE_ALL_TABS_IN_ALL_WINDOWS'),
contexts: ['browser_action'],
})

window.contextMenusClickedHandler = info => {
if (info.menuItemId === 'STORE_SELECTED_TABS') tabs.storeSelectedTabs()
else if (info.menuItemId === 'STORE_ALL_TABS_IN_CURRENT_WINDOW') tabs.storeAllTabs()
else if (info.menuItemId === 'SHOW_TAB_LIST') tabs.openTabLists()
else if (info.menuItemId === 'STORE_ALL_TABS_IN_ALL_WINDOWS') tabs.storeAllTabInAllWindows()
const setupContextMenus = async pageContext => {
await browser.contextMenus.removeAll()
const contexts = ['browser_action']
if (pageContext) contexts.push('page')
const menus = {
STORE_SELECTED_TABS: tabs.storeSelectedTabs,
STORE_ALL_TABS_IN_CURRENT_WINDOW: tabs.storeAllTabs,
SHOW_TAB_LIST: tabs.openTabLists,
STORE_ALL_TABS_IN_ALL_WINDOWS: tabs.storeAllTabInAllWindows,
}
for (const key of Object.keys(menus)) {
await browser.contextMenus.create({
id: key,
title: __('menu_' + key),
contexts,
})
}
window.contextMenusClickedHandler = info => menus[info.menuItemId]()
}

const init = async () => {
const opts = await storage.getOptions().catch(i => {}) || {}
_.defaults(opts, options.getDefaultOptions())
await storage.setOptions(opts)
updateBrowserAction(opts.browserAction)
setupContextMenus()
browser.runtime.onMessage.addListener(msg => {
setupContextMenus(opts.pageContext)
browser.runtime.onMessage.addListener(async msg => {
console.log(msg)
if (msg.optionsChanged) {
const changes = msg.optionsChanged
console.log(changes)
if (changes.browserAction) updateBrowserAction(changes.browserAction)
browser.runtime.sendMessage({optionsChangeHandledStatus: 'success'})
if ('pageContext' in changes) await setupContextMenus(changes.pageContext)
await browser.runtime.sendMessage({optionsChangeHandledStatus: 'success'})
if (PRODUCTION) Object.keys(changes).map(key => ga('send', 'event', 'Options', key, changes[key]))
}
})
Expand Down Expand Up @@ -110,4 +100,4 @@ const init = async () => {
})
}

init()
init()
8 changes: 7 additions & 1 deletion src/common/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export const optionsList = [
type: Boolean,
default: false,
},
{
name: 'pageContext',
desc: __('opt_desc_pageContext'),
type: Boolean,
default: true,
},
{
name: 'syncOptions',
desc: __('opt_desc_syncOptions'),
Expand All @@ -125,4 +131,4 @@ const getDefaultOptions = () => optionsList.reduce((opts, item) => {
return Object.assign(opts, {[item.name]: item.default})
}, {})

export default {getDefaultOptions, optionsList}
export default {getDefaultOptions, optionsList}
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "__MSG_ext_name__",
"version": "1.1.0",
"version": "1.1.1",
"default_locale": "en",
"description": "__MSG_ext_desc__",
"author": "WangJie <[email protected]>",
Expand Down Expand Up @@ -53,4 +53,4 @@
"open_in_tab": true
},
"content_security_policy": "script-src 'self' 'unsafe-eval' https://www.google-analytics.com; object-src 'self'"
}
}
5 changes: 3 additions & 2 deletions src/page/Options.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import storage from '@/common/storage'
import options from '@/common/options'
import __ from '@/common/i18n'
import _ from 'lodash'
export default {
data() {
Expand All @@ -70,15 +71,15 @@ export default {
},
methods: {
__,
async optionsChanged(key, value) {
optionsChanged: _.debounce(async function (key, value) {
console.log(1)
console.log(key, value)
// when type of option is string options can not be set correctly after first storage.setOptions() called
await storage.setOptions(this.options)
await storage.setOptions(this.options)
console.log(2)
chrome.runtime.sendMessage({optionsChanged: {[key]: value}})
},
}, 100),
async init() {
const opts = await storage.getOptions()
Object.keys(opts).map(key => {
Expand Down

0 comments on commit 4b9cab3

Please sign in to comment.