/
backgroundscript.js
174 lines (156 loc) · 5.78 KB
/
backgroundscript.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
/*
* Copyright Adam Pritchard 2013
* 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*/
/*
* Chrome background script.
*/
// On each load, check if we should show the options/changelist page.
function onLoad() {
OptionsStore.get(function(options) {
var appDetails = chrome.app.getDetails();
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': appDetails.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'] !== appDetails.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': appDetails.version }, function() {
// The extension has been newly updated
optionsURL += '?prevVer=' + options['last-version'];
showUpgradeNotification(chrome.extension.getURL(optionsURL));
});
}
});
}
// In the interest of improved browser load performace, call our onLoad after a tick.
window.addEventListener('load', Utils.nextTickFn(onLoad), false);
// Create the context menu that will signal our main code.
chrome.contextMenus.create({
contexts: ['editable'],
title: 'Mar&kdown Toggle',
onclick: function(info, tab) {
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);
return false;
}
else {
chrome.browserAction.disable(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 === '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) {
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' });
}
});
}