1
var webdeveloper_editCSSIntervalId = null;
2
var webdeveloper_editCSSSelectedTab = 0;
3
var webdeveloper_editCSSUpdateFrequency = 500;
6
function webdeveloper_applyCSS()
8
var contentDocument = webdeveloper_getContentDocument();
9
var headElementList = contentDocument.getElementsByTagName("head");
10
var styleElement = null;
11
var styleElementParent = contentDocument.documentElement;
14
var textBoxes = document.getElementById("webdeveloper-edit-css-tab-panels").getElementsByTagName("textbox");
15
var textBoxesLength = textBoxes.length;
17
// If there is a head element
18
if(headElementList.length > 0)
20
styleElementParent = headElementList[0];
23
// Loop through the text boxes
24
for(var i = 0; i < textBoxesLength; i++)
26
styleElement = contentDocument.getElementById("webdeveloper-edit-css-style-" + i);
27
textBox = textBoxes[i];
28
styleText = textBox.value;
30
// If the strip import preference is not set or is set to true
31
if(!webdeveloper_isPreferenceSet("webdeveloper.edit.css.import.strip") || webdeveloper_getBooleanPreference("webdeveloper.edit.css.import.strip", true))
33
styleText = styleText.replace(new RegExp("@import.*?;", "gi"), "");
36
// If the style element does not exist
39
styleElement = contentDocument.createElement("style");
41
styleElement.setAttribute("id", "webdeveloper-edit-css-style-" + i);
42
styleElement.setAttribute("type", "text/css");
43
styleElement.setAttribute("xml:base", textBox.getAttribute("webdeveloper-base"));
44
styleElementParent.appendChild(styleElement);
47
// If the style text is not the same as the style element content
48
if(styleText != styleElement.innerHTML)
50
webdeveloper_removeAllChildNodes(styleElement);
51
styleElement.appendChild(contentDocument.createTextNode(styleText));
57
function webdeveloper_clearCSS()
59
webdeveloper_getSelectedPanel().firstChild.value = "";
62
// Reinitializes the sidebar when the page changes
63
function webdeveloper_editCSSContentPageLoad(event)
65
var eventTarget = event.target;
66
var originalTarget = event.originalTarget;
68
// If the page is the target and the URI matches
69
if(originalTarget && eventTarget && eventTarget.contentDocument && eventTarget.hasAttribute && eventTarget.hasAttribute("id") && eventTarget.getAttribute("id").toLowerCase() == "content" && originalTarget.documentURI == eventTarget.contentDocument.documentURI)
71
// If the CSS stick preference is not set to true
72
if(!webdeveloper_getBooleanPreference("webdeveloper.edit.stick", true))
74
webdeveloper_resetCSS();
79
// Handles a browser tab being selected
80
function webdeveloper_editCSSMainTabSelect(event)
82
var browser = window.top.getBrowser();
83
var selectedTab = browser.mTabBox.selectedIndex;
85
// If the selected tab is different
86
if(selectedTab != webdeveloper_editCSSSelectedTab)
88
var browsers = browser.browsers;
89
var browserLength = browsers.length;
91
// If the previous selected tab equals the browser length
92
if(webdeveloper_editCSSSelectedTab == browserLength)
94
webdeveloper_editCSSSelectedTab = browserLength - 1;
97
webdeveloper_resetDocument(browsers[webdeveloper_editCSSSelectedTab].contentDocument);
99
webdeveloper_editCSSSelectedTab = selectedTab;
101
// If the CSS stick preference is not set to true
102
if(!webdeveloper_getBooleanPreference("webdeveloper.edit.stick", true))
104
webdeveloper_resetCSS();
109
// Enables a style sheet
110
function webdeveloper_enableStyleSheet(styleSheet)
112
// If the style sheet is a valid style sheet and is not an alternate style sheet
113
if(webdeveloper_isValidStyleSheet(styleSheet) && !webdeveloper_isAlternateStyleSheet(styleSheet))
116
var cssRules = styleSheet.cssRules;
117
var cssRulesLength = cssRules.length;
118
var importRule = Components.interfaces.nsIDOMCSSRule.IMPORT_RULE;
120
// Loop through the the style sheet rules
121
for(var i = 0; i < cssRulesLength; i++)
123
cssRule = cssRules[i];
125
// If this is an import rule
126
if(cssRule.type == importRule)
128
webdeveloper_enableStyleSheet(cssRule.styleSheet);
132
styleSheet.disabled = false;
136
// Returns the selected panel
137
function webdeveloper_getSelectedPanel()
139
var selectedPanel = document.getElementById("webdeveloper-edit-css-tab-box").selectedPanel;
141
// If the selected panel is not set
144
selectedPanel = document.getElementById("webdeveloper-edit-css-tab-panels").firstChild;
147
return selectedPanel;
150
// Returns the selected tab
151
function webdeveloper_getSelectedTab()
153
var selectedTab = document.getElementById("webdeveloper-edit-css-tab-box").selectedTab;
155
// If the selected tab is not set
158
selectedTab = document.getElementById("webdeveloper-edit-css-tabs").firstChild;
164
// Initializes the edit CSS sidebar
165
function webdeveloper_initializeEditCSS()
167
var browser = window.top.getBrowser();
168
var mainTabBox = browser.mTabBox;
170
webdeveloper_editCSSSelectedTab = mainTabBox.selectedIndex;
171
webdeveloper_editCSSUpdateFrequency = webdeveloper_getIntegerPreference("webdeveloper.edit.update.frequency", true);
173
mainTabBox.addEventListener("select", webdeveloper_editCSSMainTabSelect, true);
174
window.top.addEventListener("load", webdeveloper_editCSSContentPageLoad, true);
176
webdeveloper_updateStickCSS();
177
webdeveloper_retrieveCSS();
179
// If the update frequency is greater than 0
180
if(webdeveloper_editCSSUpdateFrequency > 0)
182
webdeveloper_editCSSIntervalId = window.setInterval(webdeveloper_applyCSS, webdeveloper_editCSSUpdateFrequency);
187
function webdeveloper_loadCSS()
189
var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
190
var stringBundle = document.getElementById("webdeveloper-string-bundle");
192
filePicker.appendFilter(stringBundle.getString("webdeveloper_styleSheetDescription"), "*.css");
193
filePicker.init(window, stringBundle.getString("webdeveloper_loadStyleSheet"), filePicker.modeOpen);
195
// If the user selected a style sheet
196
if(filePicker.show() == filePicker.returnOK)
198
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
199
var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
201
inputStream.init(filePicker.file, 0x01, 00444, null);
202
scriptableStream.init(inputStream);
204
webdeveloper_getSelectedPanel().firstChild.value = scriptableStream.read(scriptableStream.available());
206
scriptableStream.close();
211
// Resets the edited CSS
212
function webdeveloper_resetCSS()
214
webdeveloper_removeAllChildNodes(document.getElementById("webdeveloper-edit-css-tab-panels"));
215
webdeveloper_removeAllChildNodes(document.getElementById("webdeveloper-edit-css-tabs"));
216
webdeveloper_resetDocument(webdeveloper_getContentDocument());
217
webdeveloper_retrieveCSS();
220
// Resets the specified document
221
function webdeveloper_resetDocument(contentDocument)
223
var styleElement = null;
224
var styleElements = contentDocument.getElementsByTagName("style");
225
var styleSheet = null;
226
var styleSheetList = webdeveloper_getStyleSheetsForDocument(contentDocument, true, false);
227
var styleSheetLength = styleSheetList.length;
229
// Loop through the style elements
230
for(var i = 0; i < styleElements.length; i++)
232
styleElement = styleElements[i];
234
// If the style element has an id starting with webdeveloper-edit-css-style-
235
if(styleElement.hasAttribute("id") && styleElement.getAttribute("id").indexOf("webdeveloper-edit-css-style-") == 0)
237
styleElement.parentNode.removeChild(styleElement);
242
// Loop through the style sheets
243
for(i = 0; i < styleSheetLength; i++)
245
styleSheet = styleSheetList[i];
247
// If the style sheet is a valid style sheet, is for the screen and is not an alternate style sheet
248
if(webdeveloper_isValidStyleSheet(styleSheet) && webdeveloper_isMediaStyleSheet(styleSheet, "screen") && !webdeveloper_isAlternateStyleSheet(styleSheet))
250
styleSheet.disabled = false;
256
// Retrieves the CSS from the current page
257
function webdeveloper_retrieveCSS()
259
var contentDocument = webdeveloper_getContentDocument();
260
var documentURL = contentDocument.documentURI;
261
var inlineStylesText = "";
263
var results = new Array();
264
var resultsLength = null;
265
var stringBundle = document.getElementById("webdeveloper-string-bundle");
266
var styleElement = null;
267
var styleSheet = null;
268
var styleSheetList = webdeveloper_getStyleSheetsForDocument(contentDocument, true, false);
269
var styleSheetLength = styleSheetList.length;
272
var tabs = document.getElementById("webdeveloper-edit-css-tabs");
273
var tabPanels = document.getElementById("webdeveloper-edit-css-tab-panels");
275
var textBoxStyle = "";
276
var url = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
278
url.spec = documentURL;
280
// If the edit CSS background color preference is set
281
if(webdeveloper_isPreferenceSet("webdeveloper.edit.color.background"))
283
textBoxStyle += "background-color: " + webdeveloper_getStringPreference("webdeveloper.edit.color.background", true) + " !important; ";
286
// If the edit CSS text color preference is set
287
if(webdeveloper_isPreferenceSet("webdeveloper.edit.color.text"))
289
textBoxStyle += "color: " + webdeveloper_getStringPreference("webdeveloper.edit.color.text", true) + " !important; ";
292
// If the edit CSS font preference is set
293
if(webdeveloper_isPreferenceSet("webdeveloper.edit.font.size"))
295
textBoxStyle += "font-size: " + webdeveloper_getIntegerPreference("webdeveloper.edit.font.size", true) + "px !important; ";
298
textBoxStyle = textBoxStyle.trim();
300
// Loop through the style sheets
301
for(var i = 0; i < styleSheetLength; i++)
303
styleSheet = styleSheetList[i];
305
// If this is a valid style sheet, is for the screen, is not an alternate style sheet, is not disabled and is not an inline style sheet
306
if(webdeveloper_isValidStyleSheet(styleSheet) && webdeveloper_isMediaStyleSheet(styleSheet, "screen") && !webdeveloper_isAlternateStyleSheet(styleSheet) && !styleSheet.disabled && styleSheet.href != documentURL)
308
results = webdeveloper_retrieveStyleSheetDetails(styleSheet, textBoxStyle, results);
312
styleSheetList = contentDocument.getElementsByTagName("style");
313
styleSheetLength = styleSheetList.length;
315
// Loop through the inline style sheets
316
for(i = 0; i < styleSheetLength; i++)
318
styleElement = styleSheetList[i];
319
styleSheet = styleElement.sheet;
321
// If this is a valid style sheet, is for the screen and is not disabled
322
if(webdeveloper_isValidStyleSheet(styleSheet) && webdeveloper_isMediaStyleSheet(styleSheet, "screen") && !styleSheet.disabled)
324
inlineStylesText += styleElement.innerHTML.trim() + "\n\n";
326
styleSheet.disabled = true;
330
// If there are inline styles
331
if(inlineStylesText != "")
333
tab = document.createElement("tab");
334
tabPanel = document.createElement("tabpanel");
335
textBox = document.createElement("textbox");
337
tab.setAttribute("label", stringBundle.getString("webdeveloper_embeddedStyles"));
338
textBox.setAttribute("flex", "1");
339
textBox.setAttribute("multiline", "true");
340
textBox.setAttribute("style", textBoxStyle);
341
textBox.setAttribute("value", inlineStylesText);
342
textBox.setAttribute("webdeveloper-base", url.directory);
344
// If the edit CSS wrap preference is not set to true
345
if(!webdeveloper_getBooleanPreference("webdeveloper.edit.wrap", true))
347
textBox.setAttribute("wrap", "off");
350
tabPanel.appendChild(textBox);
352
results.push(new Array(tab, tabPanel));
355
resultsLength = results.length;
357
// Loop through the results
358
for(i = 0; i < resultsLength; i++)
362
tabPanels.appendChild(result[1]);
363
tabs.appendChild(result[0]);
366
// If there are no tabs
367
if(tabs.childNodes.length == 0)
369
tab = document.createElement("tab");
370
tabPanel = document.createElement("tabpanel");
371
textBox = document.createElement("textbox");
373
tab.setAttribute("label", stringBundle.getString("webdeveloper_editCSS"));
374
textBox.setAttribute("flex", "1");
375
textBox.setAttribute("multiline", "true");
376
textBox.setAttribute("style", textBoxStyle);
377
textBox.setAttribute("webdeveloper-base", url.directory);
379
// If the edit CSS wrap preference is not set to true
380
if(!webdeveloper_getBooleanPreference("webdeveloper.edit.wrap", true))
382
textBox.setAttribute("wrap", "off");
385
tabPanel.appendChild(textBox);
386
tabPanels.appendChild(tabPanel);
387
tabs.appendChild(tab);
390
tabs.selectedIndex = 0;
392
webdeveloper_applyCSS();
395
// Retrieves the style sheet details
396
function webdeveloper_retrieveStyleSheetDetails(styleSheet, textBoxStyle, results)
398
var styleSheetHref = styleSheet.href;
399
var position = styleSheetHref.lastIndexOf("/");
400
var stylesText = webdeveloper_retrieveSource(styleSheetHref);
401
var tab = document.createElement("tab");
402
var tabPanel = document.createElement("tabpanel");
403
var textBox = document.createElement("textbox");
404
var url = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURL);
406
url.spec = styleSheetHref;
408
tab.setAttribute("label", url.fileName);
409
textBox.setAttribute("flex", "1");
410
textBox.setAttribute("multiline", "true");
411
textBox.setAttribute("style", textBoxStyle);
412
textBox.setAttribute("value", stylesText);
413
textBox.setAttribute("webdeveloper-base", url.directory);
415
// If the edit CSS wrap preference is not set to true
416
if(!webdeveloper_getBooleanPreference("webdeveloper.edit.wrap", true))
418
textBox.setAttribute("wrap", "off");
421
tabPanel.appendChild(textBox);
423
results.push(new Array(tab, tabPanel));
425
styleSheet.disabled = true;
431
function webdeveloper_saveCSS()
433
var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
435
var stringBundle = document.getElementById("webdeveloper-string-bundle");
436
var styleText = webdeveloper_getSelectedPanel().firstChild.value;
438
filePicker.defaultExtension = "css";
439
filePicker.defaultString = webdeveloper_getSelectedTab().getAttribute("label");
441
filePicker.appendFilter(stringBundle.getString("webdeveloper_styleSheetDescription"), "*.css");
442
filePicker.init(window, stringBundle.getString("webdeveloper_saveStyleSheet"), filePicker.modeSave);
444
result = filePicker.show();
446
// If the user selected a style sheet
447
if(result == filePicker.returnOK || result == filePicker.returnReplace)
449
var file = filePicker.file;
450
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
452
// If the file does not exist
455
file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 00644);
458
outputStream.init(file, 0x04 | 0x08 | 0x20, 00644, null);
460
outputStream.write(styleText, styleText.length);
461
outputStream.close();
465
// Toggles sticking the CSS
466
function webdeveloper_toggleStickCSS()
468
webdeveloper_setBooleanPreference("webdeveloper.edit.stick", !webdeveloper_getBooleanPreference("webdeveloper.edit.stick", true));
470
webdeveloper_updateStickCSS();
474
function webdeveloper_unloadEditCSS()
476
webdeveloper_resetDocument(webdeveloper_getContentDocument());
477
window.clearInterval(webdeveloper_editCSSIntervalId);
479
// Try to remove the event listener
482
window.top.getBrowser().mTabBox.removeEventListener("select", webdeveloper_editCSSMainTabSelect, true);
489
// Try to remove the event listener
492
window.top.removeEventListener("load", webdeveloper_editCSSContentPageLoad, true);
500
// Updates the stick CSS button
501
function webdeveloper_updateStickCSS()
503
var element = document.getElementById("webdeveloper-stick-sidebar");
505
var stick = webdeveloper_getBooleanPreference("webdeveloper.edit.stick", true);
506
var stringBundle = document.getElementById("webdeveloper-string-bundle");
508
// If the element exists
511
// If sticking the CSS
514
label = stringBundle.getString("webdeveloper_unstick");
516
element.setAttribute("class", "unstick webdeveloper-sidebar-button");
517
element.setAttribute("tooltiptext", label);
521
label = stringBundle.getString("webdeveloper_stick");
523
element.setAttribute("class", "webdeveloper-sidebar-button");
524
element.setAttribute("tooltiptext", label);
b'\\ No newline at end of file'