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);
0 commit comments