Skip to content

Commit

Permalink
Merge pull request #3 from lewisl9029/dev-single-codebase-for-multipl…
Browse files Browse the repository at this point in the history
…e-browsers

Dev single codebase for multiple browsers
  • Loading branch information
lewisl9029 committed Apr 30, 2016
2 parents fb87d25 + dd410a8 commit 357bc58
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 153 deletions.
69 changes: 0 additions & 69 deletions chrome/just-save.js

This file was deleted.

20 changes: 0 additions & 20 deletions chrome/manifest.json

This file was deleted.

File renamed without changes
File renamed without changes
File renamed without changes
102 changes: 102 additions & 0 deletions extension/just-save.js
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);
3 changes: 2 additions & 1 deletion firefox/manifest.json → extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"48": "icons/just-save-48.png",
"96": "icons/just-save-96.png"
},

"applications": {
"gecko": {
"id": "just-save@lewisl.webextension",
Expand All @@ -18,6 +18,7 @@
},

"background": {
"persistent": false,
"scripts": ["just-save.js"]
},

Expand Down
Binary file removed firefox/icons/just-save-128.png
Binary file not shown.
Binary file removed firefox/icons/just-save-48.png
Binary file not shown.
Binary file removed firefox/icons/just-save-96.png
Binary file not shown.
63 changes: 0 additions & 63 deletions firefox/just-save.js

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 357bc58

Please # to comment.