Skip to content

Commit d896fea

Browse files
committed
Switched to WebExtensions' browser.* APIs instead of chrome.*
1 parent 2454265 commit d896fea

File tree

3 files changed

+97
-21
lines changed

3 files changed

+97
-21
lines changed

browser-extension/TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
## Extension infrastructure
44

5-
* Make compatible with Web Extensions
65
* Publish to various web stores
76
* Add links to README and demo page
87

browser-extension/background.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@ function log(...args) {
6363
}
6464

6565

66+
///// WEBEXTENSIONS POLYFILL /////
67+
/**
68+
* NOTE: This is suuuuuper-simple polyfill, only replacing the APIs that are used in this file.
69+
* The concept is based on https://github.com/mozilla/webextension-polyfill
70+
* but trimmed down to keep dependencies to a minimum.
71+
*/
72+
73+
if (typeof browser === 'undefined') {
74+
var promisedCallback = function (origFn) {
75+
return function (...args) {
76+
return new Promise((resolve, reject) => {
77+
origFn(...args, function (...resArgs) {
78+
if (chrome.runtime.lastError) {
79+
reject(chrome.runtime.lastError);
80+
return;
81+
}
82+
resolve(...resArgs);
83+
});
84+
});
85+
}
86+
};
87+
88+
var makeProxy = function (target, promiseKeys) {
89+
promiseKeys = promiseKeys || [];
90+
return new Proxy(target, {
91+
get: function (obj, key) {
92+
return promiseKeys.includes(key) ? promisedCallback(obj[key]) : obj[key];
93+
}
94+
})
95+
};
96+
97+
window.browser = {};
98+
browser.browserAction = makeProxy(chrome.browserAction);
99+
browser.runtime = makeProxy(chrome.runtime);
100+
browser.tabs = makeProxy(chrome.tabs, ['query']);
101+
}
102+
103+
66104
///// ICON BADGING /////
67105

68106
function getCountText(count) {
@@ -77,11 +115,11 @@ function setBadgeCount(tabId, count) {
77115
if (count != null && count !== '') {
78116
badgeOptions.text = getCountText(count);
79117
}
80-
chrome.browserAction.setBadgeText(badgeOptions);
118+
browser.browserAction.setBadgeText(badgeOptions);
81119
}
82120

83121
function setBadgeColor(tabId, color) {
84-
chrome.browserAction.setBadgeBackgroundColor({
122+
browser.browserAction.setBadgeBackgroundColor({
85123
tabId: tabId,
86124
color: color
87125
});
@@ -98,7 +136,7 @@ function setTotalCount(tabId, count) {
98136

99137
function ensureTabStatus(tabId) {
100138
log('ensureTabStatus', tabId);
101-
chrome.tabs.sendMessage(tabId, {
139+
browser.tabs.sendMessage(tabId, {
102140
msg: state.isCensoringActive ? 'setActive' : 'setInactive'
103141
});
104142
}
@@ -135,8 +173,8 @@ function contentScriptMessageHandler(request, sender, sendResponse) {
135173
}
136174
}
137175

138-
chrome.runtime.onMessage.addListener(contentScriptMessageHandler);
139-
chrome.tabs.onActivated.addListener(function (activeInfo) {
176+
browser.runtime.onMessage.addListener(contentScriptMessageHandler);
177+
browser.tabs.onActivated.addListener(function (activeInfo) {
140178
ensureTabStatus(activeInfo.tabId);
141179
});
142180

@@ -149,15 +187,15 @@ restoreState();
149187
function toggleStatus() {
150188
state.isCensoringActive = !state.isCensoringActive;
151189
log('toggleStatus, active =', state.isCensoringActive);
152-
chrome.tabs.query({ active: true }, function (tabs) {
190+
browser.tabs.query({ active: true }).then(function (tabs) {
153191
tabs.forEach(function (tab) {
154192
ensureTabStatus(tab.id);
155193
});
156194
});
157-
chrome.browserAction.setTitle({
195+
browser.browserAction.setTitle({
158196
title: actionTitle[state.isCensoringActive ? 'active' : 'inactive']
159197
});
160198
saveState();
161199
}
162200

163-
chrome.browserAction.onClicked.addListener(toggleStatus);
201+
browser.browserAction.onClicked.addListener(toggleStatus);

browser-extension/emoji-censor-comms.js

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* - On removed content, look for removed redactions and update count
1010
*/
1111

12+
///// CONFIG /////
13+
1214
var DEBUG = false;
1315

1416
var specialCases = [
@@ -18,15 +20,8 @@ var specialCases = [
1820

1921
var isCensoringActive = false;
2022

21-
function redact(elems) {
22-
var options = {};
23-
specialCases.forEach(function (rule) {
24-
if (location.hostname === rule[0]) {
25-
options.customDisplayElements = rule[1];
26-
}
27-
});
28-
emojiCensor.redactElements(elems, options);
29-
}
23+
24+
///// DEBUG LOGGING /////
3025

3126
function pad(n) {
3227
return ('00' + n).substr(-2);
@@ -41,6 +36,19 @@ function log(...args) {
4136
console.log(...args);
4237
}
4338

39+
40+
///// REDACTION /////
41+
42+
function redact(elems) {
43+
var options = {};
44+
specialCases.forEach(function (rule) {
45+
if (location.hostname === rule[0]) {
46+
options.customDisplayElements = rule[1];
47+
}
48+
});
49+
emojiCensor.redactElements(elems, options);
50+
}
51+
4452
var observerConfig = {
4553
characterData: true,
4654
childList: true,
@@ -81,9 +89,40 @@ function setIsActive(isNowActive) {
8189
}
8290
}
8391

92+
93+
///// COMMUNICATION & LIFECYCLE /////
94+
95+
var runtimeOnMessage, runtimeSendMessage;
96+
if (typeof browser !== 'undefined') {
97+
runtimeOnMessage = browser.runtime.onMessage;
98+
runtimeSendMessage = function (message, onResponse) {
99+
var promise = browser.runtime.sendMessage(message);
100+
return onResponse ? promise.then(onResponse) : promise;
101+
};
102+
} else {
103+
runtimeOnMessage = chrome.runtime.onMessage;
104+
runtimeSendMessage = function (message, onResponse) {
105+
var promise = new Promise((resolve, reject) => {
106+
if (onResponse) {
107+
chrome.runtime.sendMessage(message, function (...resArgs) {
108+
if (chrome.runtime.lastError) {
109+
reject(chrome.runtime.lastError);
110+
return;
111+
}
112+
resolve(...resArgs);
113+
});
114+
} else {
115+
chrome.runtime.sendMessage(message);
116+
resolve();
117+
}
118+
});
119+
return onResponse ? promise.then(onResponse) : promise;
120+
};
121+
}
122+
84123
function getGlobalStatus() {
85124
log('getGlobalStatus')
86-
chrome.runtime.sendMessage({ msg: 'isGlobalActive' }, function (response) {
125+
runtimeSendMessage({ msg: 'isGlobalActive' }, function (response) {
87126
log('isGlobalActive response', response);
88127
setIsActive(response.isActive);
89128
});
@@ -92,13 +131,13 @@ function getGlobalStatus() {
92131
function sendTotalCount(explicitCount) {
93132
var count = explicitCount !== undefined ? explicitCount : emojiCensor.redactedCount();
94133
log('sendTotalCount', count);
95-
chrome.runtime.sendMessage({
134+
runtimeSendMessage({
96135
msg: 'totalRedacted',
97136
count: count
98137
});
99138
}
100139

101-
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
140+
runtimeOnMessage.addListener(function (request, sender, sendResponse) {
102141
log('runtime.onMessage', request);
103142
switch (request.msg) {
104143
case 'setActive': setIsActive(true); break;

0 commit comments

Comments
 (0)