Skip to content
This repository was archived by the owner on Dec 18, 2021. It is now read-only.

Commit 2728bf4

Browse files
committed
#3 - Changed flow of messages between eventPage and content page.
Chromes caches the favicon, so to circunvent that, we listen for tab loaded event and if the page doesn't have a favicon, we set to one similar to the default one.
1 parent 7ea9704 commit 2728bf4

File tree

2 files changed

+106
-70
lines changed

2 files changed

+106
-70
lines changed

src/content.js

Lines changed: 103 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,94 @@
11
(function () {
22

3+
var rule = null;
4+
5+
/**
6+
* Applies the new title to Tab
7+
* @param rule
8+
*/
9+
function changeTabTitle(rule) {
10+
if (rule.title === null) {
11+
return;
12+
}
13+
document.title = rule.title;
14+
}
15+
16+
/**
17+
* Applies the new favicon to Tab
18+
* @param rule
19+
*/
20+
function changeTabIcon(rule) {
21+
if (rule.icon === null)
22+
return;
23+
24+
var el = document.querySelectorAll('head link[rel*="icon"]');
25+
var icon = chrome.extension.getURL('/assets/' + rule.icon.path);
26+
27+
Array.prototype.forEach.call(el, function (node) {
28+
node.parentNode.removeChild(node);
29+
});
30+
31+
var link = document.createElement('link');
32+
link.type = 'image/x-icon';
33+
link.rel = 'shortcut icon';
34+
link.href = icon;
35+
36+
document.getElementsByTagName('head')[0].appendChild(link);
37+
}
38+
39+
/**
40+
* Adds the Banner to Tab
41+
* @param rule
42+
*/
43+
function addSiteBanner(rule) {
44+
if (!rule.banner) {
45+
return;
46+
}
47+
48+
var envGuardSpan = document.createElement('envGuardSpan');
49+
envGuardSpan.id = 'envGuardSpan';
50+
envGuardSpan.style.fontSize = '23px';
51+
envGuardSpan.style.fontWeight = 'bold';
52+
envGuardSpan.textContent = (rule.banner) ? rule.banner.text : 'PRODUCTION';
53+
envGuardSpan.style.color = (rule.banner) ? rule.banner.textColor : '#FFF';
54+
var elemDiv = document.createElement('div');
55+
elemDiv.style.width = '100%';
56+
elemDiv.style.height = '40px';
57+
elemDiv.style.backgroundColor = (rule.banner) ? rule.banner.bgColor : '#EB1342';
58+
elemDiv.style.textAlign = 'center';
59+
elemDiv.style.position = 'fixed';
60+
elemDiv.style.top = '0';
61+
elemDiv.style.left = '0';
62+
elemDiv.style.zIndex = '9999999999';
63+
elemDiv.appendChild(envGuardSpan);
64+
65+
window.document.body.insertBefore(elemDiv, window.document.body.firstChild);
66+
}
67+
68+
/**
69+
* Resets the favicon for sites that doesn't have a favicon itself.
70+
* After removing or changing a Rule for a site, the env-guard icon remained on chrome due to caching.
71+
* @param tab
72+
*/
73+
function resetFavIcon(tab) {
74+
// if the favIconUrl of the tab contains chrome-extension we need to remove it.
75+
// This is a left over from the deleted/modified rule
76+
if (tab.favIconUrl.indexOf('chrome-extension') > -1) {
77+
var link = document.createElement('link');
78+
link.rel = 'shortcut icon';
79+
link.href = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAIVBMVEX////+/v7+/v7+/v7+/v7+/v59fX1+fn5/f399fX3w8PHiH3LTAAAACXRSTlMAAgMEBQbExcYkn27pAAAAMElEQVR4AWPAAjghgIORiRUqABXmYGFBEeDiZGNGFeDiZEcRAAGYAAwMsAACYPM7ACxPAgieMjNvAAAAAElFTkSuQmCC';
80+
document.getElementsByTagName('head')[0].appendChild(link);
81+
}
82+
}
83+
384
chrome.storage.sync.get('envGuard', function (storageItems) {
485
if (storageItems.envGuard === undefined) {
586
return;
687
}
788

889
var allRules = storageItems.envGuard;
9-
var rule = null;
10-
var applyRules;
1190

12-
applyRules = function () {
91+
function applyRules() {
1392
for (var i = 0; i < allRules.length; i++) {
1493
switch (allRules[i].operator) {
1594
case 'StartsWith':
@@ -41,74 +120,31 @@
41120
}
42121
}
43122

44-
// no rule, nothing we can do.
45-
if (rule === null) {
46-
return;
47-
}
48-
49-
changeTabTitle(rule);
50-
changeTabIcon(rule);
51-
addSiteBanner(rule);
52-
53-
// register callback for tab changes
54-
chrome.runtime.onMessage.addListener(
55-
function (message, sender, sendResponse) {
56-
if (message.title) {
57-
changeTabTitle(rule);
58-
} else if (message.favIconUrl) {
59-
changeTabIcon(rule);
60-
}
61-
}
62-
);
63-
};
64-
65-
function changeTabTitle(rule) {
66-
if (rule.title === null) {
67-
return;
123+
// This is unfortunately needed because some sites load faster
124+
// than the return from storage.sync.get. So, chrome.runtime.onMessage is already executed.
125+
if (rule) {
126+
changeTabTitle(rule);
127+
changeTabIcon(rule);
128+
addSiteBanner(rule);
68129
}
69-
document.title = rule.title;
70130
}
71131

72-
function changeTabIcon(rule) {
73-
if (rule.icon === null)
74-
return;
75-
76-
var el = document.querySelectorAll('head link[rel*="icon"]');
77-
var icon = chrome.extension.getURL('/assets/' + rule.icon.path);
78-
79-
Array.prototype.forEach.call(el, function (node) {
80-
node.parentNode.removeChild(node);
81-
});
82-
83-
var link = document.createElement('link');
84-
link.type = 'image/x-icon';
85-
link.rel = 'shortcut icon';
86-
link.href = icon;
87-
88-
document.getElementsByTagName('head')[0].appendChild(link);
89-
}
132+
applyRules();
133+
});
90134

91-
function addSiteBanner(rule) {
92-
if (!rule.banner) {
93-
return;
135+
/**
136+
* Receives a message from the eventPage.onUpdated.
137+
* This callback is fired after the tab is loaded.
138+
*/
139+
chrome.runtime.onMessage.addListener(
140+
function (tab, _, _) {
141+
if (rule) {
142+
changeTabTitle(rule);
143+
changeTabIcon(rule);
144+
addSiteBanner(rule);
145+
} else {
146+
resetFavIcon(tab);
94147
}
95-
96-
var envGuardSpan = document.createElement("envGuardSpan");
97-
envGuardSpan.id = "envGuardSpan";
98-
envGuardSpan.style.fontSize = "23px";
99-
envGuardSpan.style.fontWeight = "bold";
100-
envGuardSpan.textContent = (rule.banner) ? rule.banner.text : "PRODUCTION";
101-
envGuardSpan.style.color = (rule.banner) ? rule.banner.textColor : "#FFF";
102-
var elemDiv = document.createElement('div');
103-
elemDiv.style.width = 'width:100%';
104-
elemDiv.style.height = 'height:10%';
105-
elemDiv.style.backgroundColor = (rule.banner) ? rule.banner.bgColor : "#EB1342";
106-
elemDiv.style.textAlign = 'center';
107-
elemDiv.appendChild(envGuardSpan);
108-
109-
window.document.body.insertBefore(elemDiv, window.document.body.firstChild);
110148
}
111-
112-
applyRules();
113-
});
149+
);
114150
})();

src/eventPage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ openExtension = function () {
1414
};
1515

1616
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
17-
// sends a message to content.js only when Title or Favicon have changed.
18-
if (changeInfo.title || changeInfo.favIconUrl) {
19-
chrome.tabs.sendMessage(tabId, changeInfo);
17+
// sends a message to content.js only when the tab finished loading
18+
if (changeInfo.status === 'complete') {
19+
chrome.tabs.sendMessage(tabId, tab);
2020
}
2121
});
2222

0 commit comments

Comments
 (0)