Skip to content

Commit

Permalink
feat(webext): add save to anchr context menu option (resolve #51)
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Jan 4, 2022
1 parent 0809d37 commit 332f681
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
8 changes: 6 additions & 2 deletions webext/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Anchr Bookmarks Collector",
"author": "Ferdinand Mütsch",
"homepage_url": "https://github.com/muety/anchr/tree/master/webext",
"version": "1.3.2",
"version": "1.4.0",
"description": "Easily bookmark webpages to your Anchr collection",
"options_ui": {
"page": "src/options/options.html",
Expand All @@ -14,6 +14,9 @@
"id": "anchr@muetsch.io"
}
},
"background": {
"scripts": [ "src/background.js" ]
},
"icons": {
"48": "icons/logo-48.png"
},
Expand All @@ -25,6 +28,7 @@
"permissions": [
"https://anchr.io/*",
"storage",
"tabs"
"tabs",
"contextMenus"
]
}
2 changes: 1 addition & 1 deletion webext/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anchr-webext",
"version": "1.3.2",
"version": "1.4.0",
"description": "Browser extension to save bookmarks to Anchr.io, compatible with Firefox and Chromium",
"main": "src/app.js",
"scripts": {
Expand Down
28 changes: 17 additions & 11 deletions webext/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ browser.storage.local.get()
})
.then(fetchCollections)
.then(updateCollectionList)
.then(readTab)
.then(readTabOrTemp)
.then(updateLinkInformation)
.catch(onError)
.then(refreshToken)
Expand All @@ -41,16 +41,22 @@ formMain.addEventListener('submit', event => {
.catch(onError)
})

function readTab() {
return browser.tabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT })
.then(tabs => {
if (!tabs || !tabs.length) {
return Promise.reject('Could not read current tab')
}
return {
url: tabs[0].url,
title: tabs[0].title
}
function readTabOrTemp() {
return browser.storage.local.get('tempLink')
.then(data => data?.tempLink || browser.tabs.query({ active: true, windowId: browser.windows.WINDOW_ID_CURRENT })
.then(tabs => {
if (!tabs || !tabs.length) {
return Promise.reject('Could not read current tab')
}
return {
url: tabs[0].url,
title: tabs[0].title
}
})
)
.then(data => {
browser.storage.local.remove('tempLink')
return data
})
.catch(err => console.error(JSON.stringify(err)))
}
Expand Down
25 changes: 25 additions & 0 deletions webext/src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Context Menu

browser.contextMenus.create({
id: 'anchr-link',
title: 'Save Link to Anchr',
contexts: ['link']
}, () => { })

browser.contextMenus.onClicked.addListener((info, tab) => {
switch (info.menuItemId) {
case 'anchr-link':
browser.storage.local.get('token')
.then(token => token && token.token && browser.storage.local.set({
tempLink: {
url: info.linkUrl,
title: info.linkText,
}
}))
// normally, popup should be opened only after the callback returns
// unfortunately, that is not allowed, as the call must happen directly from within an user action handler
// however, calls to resolve collections, etc. in app.js take long enough for value to be present then
browser.browserAction.openPopup()
break
}
})

0 comments on commit 332f681

Please sign in to comment.