Skip to content

Commit

Permalink
fix: better pdf context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Dec 31, 2018
1 parent acf3fb3 commit e7ada83
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/background/context-menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ browser.contextMenus.onClicked.addListener(info => {
openMicrosoftPage()
break
case 'view_as_pdf':
openPDF(linkUrl)
openPDF(linkUrl, info.menuItemId !== 'view_as_pdf_ba')
break
case 'search_history':
openURL(browser.runtime.getURL('history.html'))
Expand Down Expand Up @@ -127,14 +127,23 @@ export function init (initConfig: ContextMenusConfig): Observable<void> {
return setMenus$$
}

export function openPDF (linkUrl?: string) {
/**
* @param url provide a url
* @param force load the current tab anyway
*/
export async function openPDF (url?: string, force?: boolean) {
const pdfURL = browser.runtime.getURL('static/pdf/web/viewer.html')
if (linkUrl) {
if (url) {
// open link as pdf
openURL(pdfURL + '?file=' + linkUrl)
} else {
openURL(pdfURL)
return openURL(pdfURL + '?file=' + encodeURIComponent(url))
}
const tabs = await browser.tabs.query({ active: true, currentWindow: true })
if (tabs.length > 0 && tabs[0].url) {
if (/pdf$/i.test(tabs[0].url as string) || force) {
return openURL(pdfURL + '?file=' + encodeURIComponent(tabs[0].url))
}
}
return openURL(pdfURL)
}

export function openGoogle (cn?: boolean) {
Expand Down Expand Up @@ -249,7 +258,8 @@ async function setContextMenus (
break
case 'view_as_pdf':
containerCtx.add('link')
contexts = ['link']
containerCtx.add('page')
contexts = ['link', 'page']
break
default:
contexts = ['selection']
Expand Down

0 comments on commit e7ada83

Please sign in to comment.