This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.js
203 lines (185 loc) · 6.54 KB
/
bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* globals Components, APP_STARTUP, APP_SHUTDOWN, ADDON_INSTALL */
const { utils: Cu } = Components;
const XULNS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
/* globals XPCOMUtils, Preferences, Services */
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Preferences', 'resource://gre/modules/Preferences.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Services', 'resource://gre/modules/Services.jsm');
/* globals idleService */
XPCOMUtils.defineLazyServiceGetter(this, 'idleService', '@mozilla.org/widget/idleservice;1', 'nsIIdleService');
/* globals strings */
XPCOMUtils.defineLazyGetter(this, 'strings', function() {
return Services.strings.createBundle(
// Work around bug 918033.
'chrome://betterreader/locale/strings.properties?' + Math.random()
);
});
var prefs = Services.prefs.getBranch('extensions.betterreader.');
var idleTimeout = 20;
/* exported install, uninstall, startup, shutdown */
function install() {
}
function uninstall() {
}
function startup(params, reason) {
if (reason == APP_STARTUP) {
Services.obs.addObserver({
observe: function() {
Services.obs.removeObserver(this, 'browser-delayed-startup-finished');
realStartup(params, reason);
}
}, 'browser-delayed-startup-finished', false);
} else {
realStartup(params, reason);
}
}
function realStartup(params, reason) {
let defaultPrefs = Services.prefs.getDefaultBranch('extensions.betterreader.');
defaultPrefs.setIntPref('donationreminder', 0);
defaultPrefs.setCharPref('version', 0);
messageListener.init();
windowObserver.init();
// Truncate version numbers to floats
let oldVersion = parseFloat(prefs.getCharPref('version'), 10);
let currentVersion = parseFloat(params.version, 10);
if (reason != ADDON_INSTALL && Services.vc.compare(oldVersion, currentVersion) == -1) {
let lastReminder = prefs.getIntPref('donationreminder') * 1000;
let shouldRemind = Date.now() - lastReminder > 604800000;
if (shouldRemind) {
idleService.addIdleObserver(idleObserver, idleTimeout);
}
}
prefs.setCharPref('version', params.version);
}
function shutdown(params, reason) {
if (reason == APP_SHUTDOWN) {
return;
}
messageListener.destroy();
windowObserver.destroy();
try {
idleService.removeIdleObserver(idleObserver, idleTimeout);
} catch (e) { // might be already removed
}
}
var messageListener = {
// Work around bug 1051238.
_frameScriptURL: 'chrome://betterreader/content/frame.js?' + Math.random(),
_processmessages: [
'BetterReader:setPref'
],
init: function() {
for (let m of this._processmessages) {
Services.ppmm.addMessageListener(m, this);
}
Services.mm.loadFrameScript(this._frameScriptURL, true);
Services.mm.broadcastAsyncMessage('BetterReader:enable');
},
destroy: function() {
Services.mm.removeDelayedFrameScript(this._frameScriptURL, true);
Services.mm.broadcastAsyncMessage('BetterReader:disable');
for (let m of this._processmessages) {
Services.ppmm.removeMessageListener(m, this);
}
},
receiveMessage: function(message) {
switch (message.name) {
case 'BetterReader:setPref':
Preferences.set('extensions.betterreader.' + message.data.name, message.data.value);
break;
}
}
};
var windowObserver = {
init: function() {
this.enumerate(this.paint);
Services.ww.registerNotification(this);
},
destroy: function() {
this.enumerate(this.unpaint);
Services.ww.unregisterNotification(this);
},
enumerate: function(callback) {
let windowEnum = Services.wm.getEnumerator('navigator:browser');
while (windowEnum.hasMoreElements()) {
callback.call(this, windowEnum.getNext());
}
},
observe: function(subject) {
subject.addEventListener('load', function() {
windowObserver.paint(subject);
}, false);
},
paint: function(win) {
if (win.location == 'chrome://browser/content/browser.xul') {
let doc = win.document;
let menu = doc.getElementById('contentAreaContextMenu');
menu.addEventListener('popupshowing', this.onPopupShowing);
let before = doc.getElementById('context-openlinkprivate').nextElementSibling;
let menuitem = doc.createElementNS(XULNS, 'menuitem');
menuitem.id = 'betterreader-openinreader';
menuitem.setAttribute('label', strings.GetStringFromName('context.openinreader.label'));
menuitem.addEventListener('command', this.onMenuItemClicked);
menu.insertBefore(menuitem, before);
}
},
unpaint: function(win) {
if (win.location == 'chrome://browser/content/browser.xul') {
let doc = win.document;
let menu = doc.getElementById('contentAreaContextMenu');
menu.removeEventListener('popupshowing', this.onPopupShowing);
doc.getElementById('betterreader-openinreader').remove();
}
},
onPopupShowing: function(event) {
let doc = event.view.document;
let win = doc.defaultView;
doc.getElementById('betterreader-openinreader').hidden = !win.gContextMenu.onSaveableLink;
},
onMenuItemClicked: function(event) {
let doc = event.view.document;
let win = doc.defaultView;
win.urlSecurityCheck(win.gContextMenu.linkURL, win.gContextMenu.principal);
win.openLinkIn(
'about:reader?url=' + encodeURIComponent(win.gContextMenu.linkURL),
event.ctrlKey ? 'tab' : 'current', win.gContextMenu._openLinkInParameters()
);
}
};
var idleObserver = {
observe: function(service, state) {
if (state != 'idle') {
return;
}
idleService.removeIdleObserver(this, idleTimeout);
let version = prefs.getCharPref('version');
let recentWindow = Services.wm.getMostRecentWindow('navigator:browser');
let notificationBox = recentWindow.document.getElementById('global-notificationbox');
let message = strings.formatStringFromName('newversion', [parseFloat(version, 10)], 1);
let changeLogLabel = strings.GetStringFromName('changelog.label');
let changeLogAccessKey = strings.GetStringFromName('changelog.accesskey');
let donateLabel = strings.GetStringFromName('donate.label');
let donateAccessKey = strings.GetStringFromName('donate.accesskey');
notificationBox.appendNotification(
message, 'betterreader-donate', null,// 'chrome://betterreader/content/icon16.png',
notificationBox.PRIORITY_INFO_MEDIUM, [{
label: changeLogLabel,
accessKey: changeLogAccessKey,
callback: function() {
recentWindow.switchToTabHavingURI(
'https://addons.mozilla.org/addon/better-reader/versions/' + version, true
);
}
}, {
label: donateLabel,
accessKey: donateAccessKey,
callback: function() {
recentWindow.switchToTabHavingURI(
'https://darktrojan.github.io/donate.html?betterreader', true
);
}
}]
);
prefs.setIntPref('donationreminder', Date.now() / 1000);
}
};