-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lewisl9029/dev-single-codebase-for-multipl…
…e-browsers Dev single codebase for multiple browsers
- Loading branch information
Showing
13 changed files
with
104 additions
and
153 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
let isChrome = window.browser === undefined; | ||
|
||
// adding browser shim for chrome support | ||
if (isChrome) { | ||
window.browser = chrome; | ||
} | ||
|
||
const logResult = result => { | ||
if (browser.extension.lastError) { | ||
return; | ||
// debugging to console is not allowed for production extensions according to | ||
// preliminary review for firefox: | ||
// Please note the following for the next update: | ||
// 1) Your add-on prints debugging information to the Console, which is generally not allowed in production add-ons. | ||
|
||
// return console.error(browser.extension.lastError); | ||
} | ||
|
||
// return console.log(result); | ||
}; | ||
|
||
|
||
const addOnInstalledListener = handleInstalled => { | ||
if (isChrome) { | ||
// enables use of non-persistent event page for chrome (not supported in firefox) | ||
// https://developer.chrome.com/extensions/event_pages | ||
return browser.runtime.onInstalled.addListener(handleInstalled); | ||
} | ||
|
||
return handleInstalled(); | ||
}; | ||
|
||
const addOnClickListener = handleClick => { | ||
if (isChrome) { | ||
// workaround for firefox contextMenus.onClicked.addListener bug | ||
return browser.contextMenus.onClicked.addListener(handleClick); | ||
} | ||
}; | ||
|
||
const downloadElement = clickContext => { | ||
if (clickContext.menuItemId !== 'just-save') { | ||
return; | ||
} | ||
|
||
const url = clickContext.srcUrl || clickContext.linkUrl || clickContext.pageUrl; | ||
|
||
if (!url) { | ||
// return console.error(`No url found for current click context`); | ||
return; | ||
} | ||
|
||
// need to specify filename for non-image contexts to workaround firefox name bug | ||
// TODO: document this bug and report on https://discourse.mozilla-community.org/c/add-ons/development | ||
const filename = isChrome ? | ||
undefined : | ||
clickContext.srcUrl ? | ||
undefined : | ||
// FIXME: not ideal for links to actual files, images, etc | ||
clickContext.linkUrl ? 'link.htm' : 'page.htm'; | ||
|
||
const downloadOptions = { | ||
url, | ||
filename | ||
// filename, | ||
// conflictAction: 'prompt', //not implemented yet in firefox 47, defaults to uniquify | ||
// saveAs: false //not implemented yet in firefox 47, defaults to false | ||
}; | ||
|
||
browser.downloads.download( | ||
downloadOptions, | ||
logResult | ||
); | ||
}; | ||
|
||
const createMenuItems = () => { | ||
// avoids duplicates on upgrade by removing existing menu items first | ||
browser.contextMenus.removeAll(result => { | ||
logResult(result); | ||
|
||
const menuProperties = { | ||
id: 'just-save', | ||
title: 'Just Save', | ||
contexts: ['all'], | ||
// workaround for firefox contextMenus.onClicked.addListener bug | ||
onclick: isChrome ? undefined : downloadElement | ||
}; | ||
|
||
browser.contextMenus.create( | ||
menuProperties, | ||
logResult | ||
); | ||
}); | ||
}; | ||
|
||
addOnInstalledListener(createMenuItems); | ||
addOnClickListener(downloadElement); | ||
|
||
// FIXME: doesn't seem to work? | ||
// TODO: report as a bug | ||
// browser.contextMenus.onClicked.addListener(downloadElement); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.