-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Expand file tree
/
Copy pathbackgroundscript.js
More file actions
243 lines (220 loc) · 8.19 KB
/
backgroundscript.js
File metadata and controls
243 lines (220 loc) · 8.19 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright Adam Pritchard 2016
* MIT License : http://adampritchard.mit-license.org/
*/
"use strict";
/*global chrome:false, OptionsStore:false, MarkdownRender:false,
marked:false, hljs:false, Utils:false, CommonLogic:false */
/*jshint devel:true, browser:true*/
/*
* Chrome background script.
*/
// On each load, check if we should show the options/changelist page.
function onLoad() {
// This timeout is a dirty hack to fix bug #119: "Markdown Here Upgrade
// Notification every time I open Chrome". That issue on Github for details.
// https://github.com/adam-p/markdown-here/issues/119
window.setTimeout(upgradeCheck, 30000);
}
// In the interest of improved browser load performace, call `onLoad` after a tick.
window.addEventListener('load', Utils.nextTickFn(onLoad), false);
function upgradeCheck() {
OptionsStore.get(function(options) {
var appManifest = chrome.runtime.getManifest();
var optionsURL = '/common/options.html';
if (typeof(options['last-version']) === 'undefined') {
// Update our last version. Only when the update is complete will we take
// the next action, to make sure it doesn't happen every time we start up.
OptionsStore.set({ 'last-version': appManifest.version }, function() {
// This is the very first time the extensions has been run, so show the
// options page.
chrome.tabs.create({ url: chrome.extension.getURL(optionsURL) });
});
}
else if (options['last-version'] !== appManifest.version) {
// Update our last version. Only when the update is complete will we take
// the next action, to make sure it doesn't happen every time we start up.
OptionsStore.set({ 'last-version': appManifest.version }, function() {
// The extension has been newly updated
optionsURL += '?prevVer=' + options['last-version'];
showUpgradeNotification(chrome.extension.getURL(optionsURL));
});
}
});
}
// Load content script dependencies.
function loadContentScripts(done) {
chrome.tabs.executeScript(
{code: 'document.markdown_here_loaded'}, function(results) {
if (results[0]) {
done();
return;
}
let srcs = [
"common/utils.js",
"common/common-logic.js",
"common/jsHtmlToText.js",
"common/marked.js",
"common/mdh-html-to-text.js",
"common/markdown-here.js",
"chrome/contentscript.js"
];
for (let i=0; i<srcs.length; i++) {
chrome.tabs.executeScript({file: srcs[i]});
}
chrome.tabs.executeScript(
{code: 'document.markdown_here_loaded = true'}, done);
});
}
// Create the context menu that will signal our main code.
chrome.contextMenus.create({
contexts: ['editable'],
title: Utils.getMessage('context_menu_item'),
onclick: function(info, tab) {
loadContentScripts(function() {
chrome.tabs.sendMessage(tab.id, {action: 'context-click'});
});
}
});
// Handle rendering requests from the content script.
// See the comment in markdown-render.js for why we do this.
chrome.runtime.onMessage.addListener(function(request, sender, responseCallback) {
// The content script can load in a not-real tab (like the search box), which
// has an invalid `sender.tab` value. We should just ignore these pages.
if (typeof(sender.tab) === 'undefined' ||
typeof(sender.tab.id) === 'undefined' || sender.tab.id < 0) {
return false;
}
if (request.action === 'render') {
OptionsStore.get(function(prefs) {
responseCallback({
html: MarkdownRender.markdownRender(
request.mdText,
prefs,
marked,
hljs),
css: (prefs['main-css'] + prefs['syntax-css'])
});
});
return true;
}
else if (request.action === 'get-options') {
OptionsStore.get(function(prefs) { responseCallback(prefs); });
return true;
}
else if (request.action === 'show-toggle-button') {
if (request.show) {
chrome.browserAction.enable(sender.tab.id);
chrome.browserAction.setTitle({
title: Utils.getMessage('toggle_button_tooltip'),
tabId: sender.tab.id });
chrome.browserAction.setIcon({
path: {
"16": Utils.getLocalURL('/common/images/icon16-button-monochrome.png'),
"19": Utils.getLocalURL('/common/images/icon19-button-monochrome.png'),
"32": Utils.getLocalURL('/common/images/icon32-button-monochrome.png'),
"38": Utils.getLocalURL('/common/images/icon38-button-monochrome.png'),
"64": Utils.getLocalURL('/common/images/icon64-button-monochrome.png')
},
tabId: sender.tab.id });
return false;
}
else {
chrome.browserAction.disable(sender.tab.id);
chrome.browserAction.setTitle({
title: Utils.getMessage('toggle_button_tooltip_disabled'),
tabId: sender.tab.id });
chrome.browserAction.setIcon({
path: {
"16": Utils.getLocalURL('/common/images/icon16-button-disabled.png'),
"19": Utils.getLocalURL('/common/images/icon19-button-disabled.png'),
"32": Utils.getLocalURL('/common/images/icon32-button-disabled.png'),
"38": Utils.getLocalURL('/common/images/icon38-button-disabled.png'),
"64": Utils.getLocalURL('/common/images/icon64-button-disabled.png')
},
tabId: sender.tab.id });
return false;
}
}
else if (request.action === 'upgrade-notification-shown') {
clearUpgradeNotification();
return false;
}
else if (request.action === 'get-forgot-to-render-prompt') {
CommonLogic.getForgotToRenderPromptContent(function(html) {
responseCallback({html: html});
});
return true;
}
else if (request.action === 'open-tab') {
chrome.tabs.create({
'url': request.url
});
return false;
}
else if (request.action === 'test-request') {
responseCallback('test-request-good');
return false;
}
else {
console.log('unmatched request action', request.action);
throw 'unmatched request action: ' + request.action;
}
});
// Add the browserAction (the button in the browser toolbar) listener.
chrome.browserAction.onClicked.addListener(function(tab) {
loadContentScripts(function() {
chrome.tabs.sendMessage(tab.id, {action: 'button-click', });
});
});
/*
Showing an notification after upgrade is complicated by the fact that the
background script can't communicate with "stale" content scripts. (See https://code.google.com/p/chromium/issues/detail?id=168263)
So, content scripts need to be reloaded before they can receive the "show
upgrade notification message". So we're going to keep sending that message from
the background script until a content script acknowledges it.
*/
var showUpgradeNotificationInterval = null;
function showUpgradeNotification(optionsURL) {
// Get the content of notification element
CommonLogic.getUpgradeNotification(optionsURL, function(html) {
var tabGotTheMessage = function(gotIt) {
// From tabs that haven't been reloaded, this will get called with no arguments.
if (!gotIt) {
return;
}
// As soon as any content script gets the message, stop trying
// to send it.
// NOTE: This could result in under-showing the notification, but that's
// better than over-showing it (e.g., issue #109).
if (showUpgradeNotificationInterval !== null) {
clearInterval(showUpgradeNotificationInterval);
showUpgradeNotificationInterval = null;
}
};
var askTabsToShowNotification = function() {
chrome.tabs.query({windowType: 'normal'}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.sendMessage(
tabs[i].id,
{ action: 'show-upgrade-notification', html: html },
tabGotTheMessage);
}
});
};
showUpgradeNotificationInterval = setInterval(askTabsToShowNotification, 5000);
});
}
function clearUpgradeNotification() {
if (showUpgradeNotificationInterval !== null) {
clearInterval(showUpgradeNotificationInterval);
showUpgradeNotificationInterval = null;
}
chrome.tabs.query({windowType: 'normal'}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.sendMessage(
tabs[i].id,
{ action: 'clear-upgrade-notification' });
}
});
}