lib: Make webext context agnostic

This commit is contained in:
Colin Caine 2017-11-19 01:57:30 +00:00
parent 143a690d4d
commit 8893b3b5a5

View file

@ -1,3 +1,19 @@
import * as convert from '../convert'
import browserProxy from './browser_proxy'
export function inContentScript() {
return ! ('tabs' in browser)
}
let browserBg
// Make this library work for both content and background.
if (inContentScript()) {
browserBg = browserProxy
} else {
browserBg = browser
}
/** await a promise and console.error and rethrow if it errors
Errors from promises don't get logged unless you seek them out.
@ -22,10 +38,17 @@ export async function l(promise) {
*/
//#background_helper
export async function activeTab() {
return (await l(browser.tabs.query({active: true, currentWindow: true})))[0]
return (await l(browserBg.tabs.query({active: true, currentWindow: true})))[0]
}
//#background_helper
export async function activeTabId() {
return (await activeTab()).id
}
/** Compare major firefox versions */
export async function firefoxVersionAtLeast(desiredmajor: number) {
const versionstr = (await browserBg.runtime.getBrowserInfo()).version
const actualmajor = convert.toNumber(versionstr.split('.')[0])
return actualmajor >= desiredmajor
}