Skip to content

Commit 357bc58

Browse files
committed
Merge pull request #3 from lewisl9029/dev-single-codebase-for-multiple-browsers
Dev single codebase for multiple browsers
2 parents fb87d25 + dd410a8 commit 357bc58

13 files changed

+104
-153
lines changed

chrome/just-save.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

chrome/manifest.json

Lines changed: 0 additions & 20 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

extension/just-save.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
let isChrome = window.browser === undefined;
4+
5+
// adding browser shim for chrome support
6+
if (isChrome) {
7+
window.browser = chrome;
8+
}
9+
10+
const logResult = result => {
11+
if (browser.extension.lastError) {
12+
return;
13+
// debugging to console is not allowed for production extensions according to
14+
// preliminary review for firefox:
15+
// Please note the following for the next update:
16+
// 1) Your add-on prints debugging information to the Console, which is generally not allowed in production add-ons.
17+
18+
// return console.error(browser.extension.lastError);
19+
}
20+
21+
// return console.log(result);
22+
};
23+
24+
25+
const addOnInstalledListener = handleInstalled => {
26+
if (isChrome) {
27+
// enables use of non-persistent event page for chrome (not supported in firefox)
28+
// https://developer.chrome.com/extensions/event_pages
29+
return browser.runtime.onInstalled.addListener(handleInstalled);
30+
}
31+
32+
return handleInstalled();
33+
};
34+
35+
const addOnClickListener = handleClick => {
36+
if (isChrome) {
37+
// workaround for firefox contextMenus.onClicked.addListener bug
38+
return browser.contextMenus.onClicked.addListener(handleClick);
39+
}
40+
};
41+
42+
const downloadElement = clickContext => {
43+
if (clickContext.menuItemId !== 'just-save') {
44+
return;
45+
}
46+
47+
const url = clickContext.srcUrl || clickContext.linkUrl || clickContext.pageUrl;
48+
49+
if (!url) {
50+
// return console.error(`No url found for current click context`);
51+
return;
52+
}
53+
54+
// need to specify filename for non-image contexts to workaround firefox name bug
55+
// TODO: document this bug and report on https://discourse.mozilla-community.org/c/add-ons/development
56+
const filename = isChrome ?
57+
undefined :
58+
clickContext.srcUrl ?
59+
undefined :
60+
// FIXME: not ideal for links to actual files, images, etc
61+
clickContext.linkUrl ? 'link.htm' : 'page.htm';
62+
63+
const downloadOptions = {
64+
url,
65+
filename
66+
// filename,
67+
// conflictAction: 'prompt', //not implemented yet in firefox 47, defaults to uniquify
68+
// saveAs: false //not implemented yet in firefox 47, defaults to false
69+
};
70+
71+
browser.downloads.download(
72+
downloadOptions,
73+
logResult
74+
);
75+
};
76+
77+
const createMenuItems = () => {
78+
// avoids duplicates on upgrade by removing existing menu items first
79+
browser.contextMenus.removeAll(result => {
80+
logResult(result);
81+
82+
const menuProperties = {
83+
id: 'just-save',
84+
title: 'Just Save',
85+
contexts: ['all'],
86+
// workaround for firefox contextMenus.onClicked.addListener bug
87+
onclick: isChrome ? undefined : downloadElement
88+
};
89+
90+
browser.contextMenus.create(
91+
menuProperties,
92+
logResult
93+
);
94+
});
95+
};
96+
97+
addOnInstalledListener(createMenuItems);
98+
addOnClickListener(downloadElement);
99+
100+
// FIXME: doesn't seem to work?
101+
// TODO: report as a bug
102+
// browser.contextMenus.onClicked.addListener(downloadElement);

firefox/manifest.json renamed to extension/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"48": "icons/just-save-48.png",
1010
"96": "icons/just-save-96.png"
1111
},
12-
12+
1313
"applications": {
1414
"gecko": {
1515
@@ -18,6 +18,7 @@
1818
},
1919

2020
"background": {
21+
"persistent": false,
2122
"scripts": ["just-save.js"]
2223
},
2324

firefox/icons/just-save-128.png

-4.79 KB
Binary file not shown.

firefox/icons/just-save-48.png

-1.76 KB
Binary file not shown.

firefox/icons/just-save-96.png

-3.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)