browserViewMenu.js
Go to the documentation of this file.
1 /*
2  * This file contains the code that is required to support the view menu
3  * functionality copied from Firefox. See:
4  * http://mxr.mozilla.org/seamonkey/source/browser/base/content/browser.js
5  */
6 
7 function delayedStartup() {
8  if ( typeof gBrowser != 'undefined' ) { // if (!gBrowser) causes javascript error
9  gBrowser.addEventListener("pageshow", function(evt) {
11  }, true);
12  }
13 }
14 
15 window.addEventListener("load", function(e) {
17 }, false);
18 
19 var nsIWebNavigation = Components.interfaces.nsIWebNavigation;
20 
24 var gInPrintPreviewMode = false;
25 
27 { try {
28  var node = event.target;
29  var name = node.getAttribute('name');
30 
31  if (name == 'detectorGroup') {
32  SetForcedDetector(true);
33  SelectDetector(event, false);
34  } else if (name == 'charsetGroup') {
35  var charset = node.getAttribute('id');
36  charset = charset.substring('charset.'.length, charset.length)
37  SetForcedCharset(charset);
38  } else if (name == 'charsetCustomize') {
39  //do nothing - please remove this else statement, once the charset prefs moves to the pref window
40  } else {
41  SetForcedCharset(node.getAttribute('id'));
42  }
43  } catch(ex) { alert(ex); }
44 }
45 
46 function SelectDetector(event, doReload)
47 {
48  var uri = event.target.getAttribute("id");
49  var prefvalue = uri.substring('chardet.'.length, uri.length);
50  if ("off" == prefvalue) { // "off" is special value to turn off the detectors
51  prefvalue = "";
52  }
53 
54  try {
55  var pref = Components.classes["@mozilla.org/preferences-service;1"]
56  .getService(Components.interfaces.nsIPrefBranch);
57  var str = Components.classes["@mozilla.org/supports-string;1"]
58  .createInstance(Components.interfaces.nsISupportsString);
59 
60  str.data = prefvalue;
61  pref.setComplexValue("intl.charset.detector",
62  Components.interfaces.nsISupportsString, str);
63  if (doReload) window.content.location.reload();
64  }
65  catch (ex) {
66  dump("Failed to set the intl.charset.detector preference.\n");
67  }
68 }
69 
70 function SetForcedDetector(doReload)
71 {
72  BrowserSetForcedDetector(doReload);
73 }
74 
76 {
78 }
79 
80 function BrowserSetForcedCharacterSet(aCharset)
81 {
82  var docCharset = getBrowser().selectedBrowser.docShell.QueryInterface(
83  Components.interfaces.nsIDocCharset);
84  docCharset.charset = aCharset;
85  BrowserReloadWithFlags(nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
86 }
87 
88 function BrowserSetForcedDetector(doReload)
89 {
90  gBrowser.selectedBrowser.documentCharsetInfo.forcedDetector = true;
91  if (doReload)
92  BrowserReloadWithFlags(nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
93 }
94 
96 {
97  var menuitem = null;
98 
99  // exctract the charset from DOM
100  var wnd = document.commandDispatcher.focusedWindow;
101  if ((window == wnd) || (wnd == null)) wnd = window.content;
102  menuitem = document.getElementById('charset.' + wnd.document.characterSet);
103 
104  if (menuitem) {
105  // uncheck previously checked item to workaround Mac checkmark problem
106  // bug 98625
107  if (gPrevCharset) {
108  var pref_item = document.getElementById('charset.' + gPrevCharset);
109  if (pref_item)
110  pref_item.setAttribute('checked', 'false');
111  }
112  menuitem.setAttribute('checked', 'true');
113  }
114 }
115 
117 {
118  var prefvalue;
119 
120  try {
121  var pref = Components.classes["@mozilla.org/preferences-service;1"]
122  .getService(Components.interfaces.nsIPrefBranch);
123  prefvalue = pref.getComplexValue("intl.charset.detector",
124  Components.interfaces.nsIPrefLocalizedString).data;
125  }
126  catch (ex) {
127  prefvalue = "";
128  }
129 
130  if (prefvalue == "") prefvalue = "off";
131  dump("intl.charset.detector = "+ prefvalue + "\n");
132 
133  prefvalue = 'chardet.' + prefvalue;
134  var menuitem = document.getElementById(prefvalue);
135 
136  if (menuitem) {
137  menuitem.setAttribute('checked', 'true');
138  }
139 }
140 
142 {
143  // use setTimeout workaround to delay checkmark the menu
144  // when onmenucomplete is ready then use it instead of oncreate
145  // see bug 78290 for the detail
150 }
151 
152 function CreateMenu(node)
153 {
154  var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
155  observerService.notifyObservers(null, "charsetmenu-selected", node);
156 }
157 
159 {
160  var charset = window.content.document.characterSet;
161 
162  if (charset.length > 0 && (charset != gLastBrowserCharset)) {
163  if (!gCharsetMenu)
164  gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener);
165  gCharsetMenu.SetCurrentCharset(charset);
166  gPrevCharset = gLastBrowserCharset;
167  gLastBrowserCharset = charset;
168  }
169 }
170 
171 function getBrowser()
172 {
173  if (!gBrowser)
174  gBrowser = document.getElementById("content");
175  return gBrowser;
176 }
177 
179 {
180  return gBrowser.selectedBrowser.markupDocumentViewer;
181 }
182 
183 /* Begin Page Style Functions */
184 function getStyleSheetArray(frame)
185 {
186  var styleSheets = frame.document.styleSheets;
187  var styleSheetsArray = new Array(styleSheets.length);
188  for (var i = 0; i < styleSheets.length; i++) {
189  styleSheetsArray[i] = styleSheets[i];
190  }
191  return styleSheetsArray;
192 }
193 
194 function getAllStyleSheets(frameset)
195 {
196  var styleSheetsArray = getStyleSheetArray(frameset);
197  for (var i = 0; i < frameset.frames.length; i++) {
198  var frameSheets = getAllStyleSheets(frameset.frames[i]);
199  styleSheetsArray = styleSheetsArray.concat(frameSheets);
200  }
201  return styleSheetsArray;
202 }
203 
204 function stylesheetFillPopup(menuPopup)
205 {
206  var noStyle = menuPopup.firstChild;
207  var persistentOnly = noStyle.nextSibling;
208  var sep = persistentOnly.nextSibling;
209  while (sep.nextSibling)
210  menuPopup.removeChild(sep.nextSibling);
211 
212  var styleSheets = getAllStyleSheets(window.content);
213  var currentStyleSheets = [];
214  var styleDisabled = getMarkupDocumentViewer().authorStyleDisabled;
215  var haveAltSheets = false;
216  var altStyleSelected = false;
217 
218  for (var i = 0; i < styleSheets.length; ++i) {
219  var currentStyleSheet = styleSheets[i];
220 
221  // Skip any stylesheets that don't match the screen media type.
222  var media = currentStyleSheet.media.mediaText.toLowerCase();
223  if (media && (media.indexOf("screen") == -1) && (media.indexOf("all") == -1))
224  continue;
225 
226  if (currentStyleSheet.title) {
227  if (!currentStyleSheet.disabled)
228  altStyleSelected = true;
229 
230  haveAltSheets = true;
231 
232  var lastWithSameTitle = null;
233  if (currentStyleSheet.title in currentStyleSheets)
234  lastWithSameTitle = currentStyleSheets[currentStyleSheet.title];
235 
236  if (!lastWithSameTitle) {
237  var menuItem = document.createElement("menuitem");
238  menuItem.setAttribute("type", "radio");
239  menuItem.setAttribute("label", currentStyleSheet.title);
240  menuItem.setAttribute("data", currentStyleSheet.title);
241  menuItem.setAttribute("checked", !currentStyleSheet.disabled && !styleDisabled);
242  menuPopup.appendChild(menuItem);
243  currentStyleSheets[currentStyleSheet.title] = menuItem;
244  } else {
245  if (currentStyleSheet.disabled)
246  lastWithSameTitle.removeAttribute("checked");
247  }
248  }
249  }
250 
251  noStyle.setAttribute("checked", styleDisabled);
252  persistentOnly.setAttribute("checked", !altStyleSelected && !styleDisabled);
253  persistentOnly.hidden = (window.content.document.preferredStyleSheetSet) ? haveAltSheets : false;
254  sep.hidden = (noStyle.hidden && persistentOnly.hidden) || !haveAltSheets;
255  return true;
256 }
257 
258 function stylesheetInFrame(frame, title) {
259  var docStyleSheets = frame.document.styleSheets;
260 
261  for (var i = 0; i < docStyleSheets.length; ++i) {
262  if (docStyleSheets[i].title == title)
263  return true;
264  }
265  return false;
266 }
267 
268 function stylesheetSwitchFrame(frame, title) {
269  var docStyleSheets = frame.document.styleSheets;
270 
271  for (var i = 0; i < docStyleSheets.length; ++i) {
272  var docStyleSheet = docStyleSheets[i];
273 
274  if (title == "_nostyle")
275  docStyleSheet.disabled = true;
276  else if (docStyleSheet.title)
277  docStyleSheet.disabled = (docStyleSheet.title != title);
278  else if (docStyleSheet.disabled)
279  docStyleSheet.disabled = false;
280  }
281 }
282 
283 function stylesheetSwitchAll(frameset, title) {
284  if (!title || title == "_nostyle" || stylesheetInFrame(frameset, title)) {
285  stylesheetSwitchFrame(frameset, title);
286  }
287  for (var i = 0; i < frameset.frames.length; i++) {
288  stylesheetSwitchAll(frameset.frames[i], title);
289  }
290 }
291 
292 function setStyleDisabled(disabled) {
293  getMarkupDocumentViewer().authorStyleDisabled = disabled;
294 }
295 
296 /* End of the Page Style functions */
297 
299 {
300  var pageCookie;
301  var webNav;
302 
303  // Get the document charset
304  var docCharset = "charset=" + aDocument.characterSet;
305 
306  // Get the nsIWebNavigation associated with the document
307  try {
308  var win;
309  var ifRequestor;
310 
311  // Get the DOMWindow for the requested document. If the DOMWindow
312  // cannot be found, then just use the content window...
313  //
314  // XXX: This is a bit of a hack...
315  win = aDocument.defaultView;
316  if (win == window) {
317  win = content;
318  }
319  ifRequestor = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
320 
321  webNav = ifRequestor.getInterface(nsIWebNavigation);
322  } catch(err) {
323  // If nsIWebNavigation cannot be found, just get the one for the whole
324  // window...
325  webNav = getWebNavigation();
326  }
327  //
328  // Get the 'PageDescriptor' for the current document. This allows the
329  // view-source to access the cached copy of the content rather than
330  // refetching it from the network...
331  //
332  try{
333  var PageLoader = webNav.QueryInterface(Components.interfaces.nsIWebPageDescriptor);
334 
335  pageCookie = PageLoader.currentDescriptor;
336  } catch(err) {
337  // If no page descriptor is available, just use the view-source URL...
338  }
339 
340  ViewSourceOfURL(webNav.currentURI.spec, pageCookie, aDocument);
341 }
342 
343 function ViewSourceOfURL(aURL, aPageDescriptor, aDocument)
344 {
345  var utils = window.top.gViewSourceUtils;
346  if (getBoolPref("view_source.editor.external", false)) {
347  utils.openInExternalEditor(aURL, aPageDescriptor, aDocument);
348  }
349  else {
350  utils.openInInternalViewer(aURL, aPageDescriptor, aDocument);
351  }
352 }
353 
function SetForcedCharset(charset)
var gCharsetMenu
function UpdateCharsetDetector()
function stylesheetFillPopup(menuPopup)
function BrowserSetForcedDetector(doReload)
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
function UpdateMenus(event)
function MultiplexHandler(event)
var menuItem
Definition: FeedWriter.js:970
function SelectDetector(event, doReload)
var pref
Definition: openLocation.js:44
function getMarkupDocumentViewer()
function BrowserSetForcedCharacterSet(aCharset)
var gPrevCharset
var event
function UpdateCurrentCharset()
function getBoolPref(prefname, def)
let window
_collectFormDataForFrame aDocument
function ViewSourceOfURL(aURL, aPageDescriptor, aDocument)
function stylesheetInFrame(frame, title)
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
function getAllStyleSheets(frameset)
function BrowserReloadWithFlags(reloadFlags)
Definition: browser.js:2539
return null
Definition: FeedWriter.js:1143
let node
function setStyleDisabled(disabled)
var uri
Definition: FeedWriter.js:1135
var gLastBrowserCharset
function charsetLoadListener(event)
function CreateMenu(node)
function BrowserViewSourceOfDocument(aDocument)
function getBrowser()
function getStyleSheetArray(frame)
function stylesheetSwitchFrame(frame, title)
var nsIWebNavigation
_getSelectedPageStyle s i
function getWebNavigation()
Definition: browser.js:2530
function delayedStartup()
var gInPrintPreviewMode
converter charset
function SetForcedDetector(doReload)
function stylesheetSwitchAll(frameset, title)