Skip to content

Commit 3699866

Browse files
committed
Added iframe support to get the true count for a tab
1 parent 6728a39 commit 3699866

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

browser-extension/background.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,36 @@ if (typeof browser === 'undefined') {
103103

104104
///// ICON BADGING /////
105105

106+
class MappyMap extends Map {
107+
get(key) {
108+
let value = super.get(key);
109+
return value || new Map();
110+
}
111+
}
112+
113+
var tabCounts = new MappyMap();
114+
115+
function addTabFrameCount(tabId, frameId, count) {
116+
var tabFrames = tabCounts.get(tabId);
117+
tabFrames.set(frameId, count);
118+
tabCounts.set(tabId, tabFrames);
119+
}
120+
121+
function resetTabCount(tabId) {
122+
if (tabCounts.has(tabId)) {
123+
tabCounts.set(tabId, new Map());
124+
}
125+
}
126+
127+
function getTabCount(tabId) {
128+
var count = 0
129+
var tabFrames = tabCounts.get(tabId);
130+
for (let frameCount of tabFrames.values()) {
131+
count += frameCount;
132+
}
133+
return count;
134+
}
135+
106136
function getCountText(count) {
107137
if (count <= 999) {
108138
return count.toString();
@@ -125,10 +155,11 @@ function setBadgeColor(tabId, color) {
125155
});
126156
}
127157

128-
function setTotalCount(tabId, count) {
129-
log('setTotalCount', tabId, count);
158+
function setTotalCount(tabId, frameId, count) {
159+
log('setTotalCount', { tabId, frameId, count });
130160
setBadgeColor(tabId, colors.good);
131-
setBadgeCount(tabId, state.isCensoringActive ? count : '');
161+
addTabFrameCount(tabId, frameId, count);
162+
setBadgeCount(tabId, state.isCensoringActive ? getTabCount(tabId) : '');
132163
}
133164

134165

@@ -167,16 +198,25 @@ function contentScriptMessageHandler(request, sender, sendResponse) {
167198
sendResponse({ isActive: state.isCensoringActive });
168199
break;
169200
case 'totalRedacted':
170-
setTotalCount(sender.tab.id, request.count);
201+
setTotalCount(sender.tab.id, sender.frameId, request.count);
171202
break;
172203
}
173204
}
174205
}
175206

207+
function tabLoadingHandler(tabId, changeInfo) {
208+
log('tabLoadingHandler', tabId, changeInfo);
209+
// Reset counts when a tab loads a new page, to ensure old iframe counts are cleared
210+
if (changeInfo.status === 'loading') {
211+
resetTabCount(tabId);
212+
}
213+
}
214+
176215
browser.runtime.onMessage.addListener(contentScriptMessageHandler);
177216
browser.tabs.onActivated.addListener(function (activeInfo) {
178217
ensureTabStatus(activeInfo.tabId);
179218
});
219+
browser.tabs.onUpdated.addListener(tabLoadingHandler);
180220

181221
log('--- event page loaded ---');
182222
restoreState();
@@ -195,6 +235,13 @@ function toggleStatus() {
195235
browser.browserAction.setTitle({
196236
title: actionTitle[state.isCensoringActive ? 'active' : 'inactive']
197237
});
238+
if (state.isCensoringActive) {
239+
log('===ADD===')
240+
browser.tabs.onUpdated.addListener(tabLoadingHandler);
241+
} else {
242+
log('===REMOVE===')
243+
browser.tabs.onUpdated.removeListener(tabLoadingHandler);
244+
}
198245
saveState();
199246
}
200247

browser-extension/emoji-censor-comms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function log(...args) {
3636
var now = new Date();
3737
var stamp = [now.getHours(), now.getMinutes(), now.getSeconds()].map(pad).join(':');
3838
args.unshift('%c[emoji-censor ' + stamp + ']', 'color:#999');
39+
args.push(window.frameElement);
3940
console.log(...args);
4041
}
4142

browser-extension/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"content_scripts": [{
2323
"matches": ["*://*/*"],
24-
"js": ["emoji-censor.js", "emoji-censor-comms.js"]
24+
"js": ["emoji-censor.js", "emoji-censor-comms.js"],
25+
"all_frames": true
2526
}],
2627
"permissions": [
2728
"*://*/*"

test-iframe.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Emoji Censor tests — iframe</title>
6+
<link href="https://fonts.googleapis.com/css?family=Noto+Sans&amp;subset=devanagari" rel="stylesheet">
7+
<style>
8+
body {
9+
font-family: 'Noto Sans', sans-serif;
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
<h3>This is inside an iframe</h3>
15+
<h3>✂ Copy and 📋 Paste Emoji 👍 No apps required</h3>
16+
<p>Emojis are supported on iOS, Android, macOS, Windows, and Linux. Copy and paste emojis to use on Twitter, Facebook, Slack, Instagram, Snapchat, Tumblr and more. Use Emoji Classic for older systems. ⤵️ Jump to:</p>
17+
18+
<p>😃💁 People • 🐻🌻 Animals • 🍔🍹 Food • 🎷⚽️ Activities • 🚘🌇 Travel • 💡🎉 Objects • 💖🔣 Symbols • 🎌🏳️‍🌈 Flags</p>
19+
<cite><a href="http://getemoji.com/">getemoji.com</a></cite>
20+
</body>
21+
</html>

test.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
body {
99
font-family: 'Noto Sans', sans-serif;
1010
}
11+
iframe {
12+
float: right;
13+
height: 200px;
14+
width: 50%;
15+
}
1116
.actions button {
1217
background: #eee;
1318
border: 1px solid;
@@ -47,6 +52,7 @@
4752
<li>Most definitely using ZWJ: 👨‍👨‍👧‍👧
4853
</ul>
4954
<hr>
55+
<iframe src="test-iframe.html"></iframe>
5056
<blockquote>
5157
<h3>✂ Copy and 📋 Paste Emoji 👍 No apps required</h3>
5258
<p>Emojis are supported on iOS, Android, macOS, Windows, and Linux. Copy and paste emojis to use on Twitter, Facebook, Slack, Instagram, Snapchat, Tumblr and more. Use Emoji Classic for older systems. ⤵️ Jump to:</p>

0 commit comments

Comments
 (0)