|
1 | 1 | (function () {
|
2 | 2 |
|
| 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 | + |
3 | 84 | chrome.storage.sync.get('envGuard', function (storageItems) {
|
4 | 85 | if (storageItems.envGuard === undefined) {
|
5 | 86 | return;
|
6 | 87 | }
|
7 | 88 |
|
8 | 89 | var allRules = storageItems.envGuard;
|
9 |
| - var rule = null; |
10 |
| - var applyRules; |
11 | 90 |
|
12 |
| - applyRules = function () { |
| 91 | + function applyRules() { |
13 | 92 | for (var i = 0; i < allRules.length; i++) {
|
14 | 93 | switch (allRules[i].operator) {
|
15 | 94 | case 'StartsWith':
|
|
41 | 120 | }
|
42 | 121 | }
|
43 | 122 |
|
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); |
68 | 129 | }
|
69 |
| - document.title = rule.title; |
70 | 130 | }
|
71 | 131 |
|
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 | + }); |
90 | 134 |
|
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); |
94 | 147 | }
|
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); |
110 | 148 | }
|
111 |
| - |
112 |
| - applyRules(); |
113 |
| - }); |
| 149 | + ); |
114 | 150 | })();
|
0 commit comments