-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
executable file
·281 lines (222 loc) · 10.3 KB
/
background.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* See license.txt for terms of usage */
// *************************************************************************************************
//var bookmarklet = "javascript:(typeof Firebug!='undefined')?Firebug.chrome.toggle():(function(F,i,r,e,b,u,g,L,I,T,E){if(F.getElementById(b))return;E=F[i+'NS']&&F.documentElement.namespaceURI;E=E?F[i+'NS'](E,'script'):F[i]('script');E[r]('id',b);E[r]('src',I+g+T);E[r](b,u);(F[e]('head')[0]||F[e]('body')[0]).appendChild(E);E=new%20Image;E[r]('src',I+L);})(document,'createElement','setAttribute','getElementsByTagName','FirebugLite','4','firebug-lite.js','releases/lite/latest/skin/xp/sprite.png','https://getfirebug.com/','#startOpened');";
var firebugVersion = "Firebug Lite 1.4.0";
var extensionURL = chrome.extension.getURL("");
var isActive = false;
// *************************************************************************************************
function handleIconClick(tab)
{
if (tab.url.indexOf("https://chrome.google.com/webstore") == 0 ||
tab.url.indexOf("https://chrome.google.com/extensions") == 0 ||
tab.url.indexOf("chrome://") == 0)
{
alert("Sorry, for security reasons extensions cannot run scripts in this page, "+
"which means Firebug Lite can't work here. :(");
return;
}
if (tab.url.indexOf("file:///") == 0)
{
// TODO: include message here about the problem, and chrome bug
//chrome.tabs.update(tab.id, {url: bookmarklet});
alert("So, you want to load Firebug Lite in a local file, huh?\n\n" +
"Sorry to say but this is a complicated issue... there's a Chrome bug preventing us " +
"to load Firebug Lite here, and there's a JavaScript security restriction " +
"preventing us to do XHR calls. :(\n\n" +
"You can solve all these problems by using a local web server which is simple to " +
"install and is safer for you. :)\n\n" +
"If you want to know more about this problem, and how to solve it, please read " +
"our FAQ entry for this subject at:\n\n" +
"http://getfirebug.com/wiki/index.php/Firebug_Lite_FAQ"
);
return;
}
var isContentScriptActive = false;
var tryToActivateFirebug = function(){
chrome.tabs.sendMessage( tab.id, {name: "FB_isActive"},
function(response)
{
if(!!chrome.runtime.lastError && !!chrome.runtime.lastError.message) {
isContentScriptActive = false;
unsetActivationStorage(tab);
}else{
isContentScriptActive = true;
}
// if (!!response && !!response.value && response.value == "true")
// {
// chrome.tabs.executeScript(tab.id, {code: "Firebug.chrome.toggle()"});
// }
// else
// {
if (!!response && !!response.value && response.value == "false") {
isContentScriptActive = false;
unsetActivationStorage(tab);
}else if(!!response && !!response.value && response.value == "csp") {
isContentScriptActive = false;
firebugShouldBeLoaded = false;
unsetActivationStorage(tab);
}else{
setActivationStorage(tab);
chrome.tabs.sendMessage(tab.id, {name: "FB_loadFirebug"});
}
//}
}
);
};
var firebugShouldBeLoaded = tab.url.indexOf("https://") == 0 || tab.url.indexOf("http://") == 0;
tryToActivateFirebug();
setTimeout(function(){
// the problem of this approach is that if the page does not allow content scripts, like
// the Chrome Web Store, it will falsely warn users that reloading the page will complete
// the activation process when in the reality it will not. But, the most common case is
// when the user just installed Firebug Lite and have other tabs already opened, which
// completely ruins the very first experience with using the Firebug Lite extension.
if (!isContentScriptActive)
{
// try again
tryToActivateFirebug();
setTimeout(function(){
if (!isContentScriptActive)
{
//chrome.tabs.update(tab.id, {url: bookmarlet});
//enableBrowserActionIcon();
//setActivationStorage(tab);
if (firebugShouldBeLoaded)
{
setActivationStorage(tab);
if (confirm("It seems that this page was opened before Firebug Lite was "+
"enabled. It will (hopefully) load after reloading this page. :)"+
"\n\nPress ok to reload the page now, or cancel to reload it later."))
{
chrome.tabs.executeScript(tab.id, {code: "window.location.reload()"});
}
}
else
{
// TODO: add FAQ entry with the problem and point it here
alert("Sorry, Firebug Lite cannot be loaded in this page. :(\n\nMost likely the security rules of the page prevent Firebug Lite from loading\n\nFor "+
"support, please visit:\nhttps://chrome.google.com/webstore/detail/firebug-lite-for-google-c/ehemiojjcpldeipjhjkepfdaohajpbdo?hl=en");
}
}
},500);
}
},500);
};
chrome.browserAction.onClicked.addListener(handleIconClick);
// *************************************************************************************************
function handleTabChange(tabId, selectInfo)
{
var isUpdated = false;
chrome.tabs.sendMessage(tabId, {name: "FB_isActive"},
function(response)
{
isUpdated = true;
if (!!response && !!response.value && response.value == "true")
{
enableBrowserActionIcon();
isActive = true;
}
else
{
disableBrowserActionIcon();
isActive = false;
}
}
);
setTimeout(function(){
chrome.tabs.get(tabId, function(tab){
var title = tab.title || "";
if (!isUpdated && !title.indexOf("Firebug Lite") == 0)
{
disableBrowserActionIcon();
isActive = false;
}
});
},100);
};
// *************************************************************************************************
chrome.tabs.onSelectionChanged.addListener(handleTabChange);
// *************************************************************************************************
function handleUpdateTab(tabId, updateInfo, tab)
{
if (updateInfo.status == "complete") return;
handleTabChange(tabId, updateInfo);
}
// memory leaking here
//chrome.tabs.onUpdated.addListener(handleUpdateTab);
// *************************************************************************************************
chrome.runtime.onMessage.addListener
(
function(request, sender, sendResponse)
{
if (request.name == "FB_enableIcon")
enableBrowserActionIcon();
else if (request.name == "FB_disableIcon")
disableBrowserActionIcon();
else if (request.name == "FB_deactivate")
{
disableBrowserActionIcon();
chrome.tabs.query({currentWindow: true}, function(tab){
unsetActivationStorage(tab[0].id);
chrome.tabs.sendMessage(tab[0].id, {name: "FB_deactivate"});
});
}
sendResponse({}); // snub them.
}
);
// *************************************************************************************************
chrome.contextMenus.create({
title: "Inspect with Firebug Lite",
"contexts": ["all"],
onclick: function(info, tab) {
//console.log(tab);
if(!!tab && !!tab.id) {
handleIconClick(tab);
//chrome.tabs.sendMessage(tab.id, {name: "FB_contextMenuClick"});
}else{
chrome.tabs.query({currentWindow: true}, function(tabs) {
//console.log(tabs);
//console.log(tabs[0].id);
//chrome.tabs.sendMessage(tabs[0].id, {name: "FB_contextMenuClick"});
handleIconClick(tabs[0]);
});
}
}
});
chrome.contextMenus.create({
title: "Donate to keep Extensions Alive",
"contexts": ["browser_action"],
onclick: function(info, tab) {
chrome.tabs.create({ url: 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=67TZLSEGYQFFW' });
}
});
chrome.contextMenus.create({
title: "Leave a review",
"contexts": ["browser_action"],
onclick: function(info, tab) {
chrome.tabs.create({ url: 'https://chrome.google.com/webstore/detail/firebug-lite-for-google-c/ehemiojjcpldeipjhjkepfdaohajpbdo' });
}
});
// *************************************************************************************************
function enableBrowserActionIcon()
{
chrome.browserAction.setTitle({title: firebugVersion + " (On)"});
chrome.browserAction.setIcon({path:"bug128.png"});
};
function disableBrowserActionIcon()
{
chrome.browserAction.setTitle({title: firebugVersion + " (Off)"});
chrome.browserAction.setIcon({path:"bug128off.png"});
};
// *************************************************************************************************
function setActivationStorage(tab)
{
chrome.tabs.executeScript(tab.id, {code: "localStorage.setItem('Firebug','1,1,"+extensionURL+"')"});
isActive = true;
};
function unsetActivationStorage(tab)
{
chrome.tabs.executeScript(tab.id, {code: "localStorage.removeItem('Firebug')"});
isActive = false;
};
// *************************************************************************************************